3

这是我下面的代码

import javax.swing.*;
import java.awt.*;

public class board2 {

JFrame frame;
JPanel squares[][] = new JPanel[8][8];

public board2() {
    frame = new JFrame("Simplified Chess");
    frame.setSize(500, 500);
    frame.setLayout(new GridLayout(8, 8));

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            squares[i][j] = new JPanel();

            if ((i + j) % 2 == 0) {
                squares[i][j].setBackground(Color.black);
            } else {
                squares[i][j].setBackground(Color.white);
            }   
            frame.add(squares[i][j]);
        }
    }

    squares[0][0].add(new JLabel(new ImageIcon("rookgreen.png")));
    squares[0][2].add(new JLabel(new ImageIcon("bishopgreen.png")));
    squares[0][4].add(new JLabel(new ImageIcon("kinggreen.png")));
    squares[0][5].add(new JLabel(new ImageIcon("bishopgreen.png")));
    squares[0][7].add(new JLabel(new ImageIcon("rookgreen.png")));

    squares[7][0].add(new JLabel(new ImageIcon("rookred.png")));
    squares[7][2].add(new JLabel(new ImageIcon("bishopred.png")));
    squares[7][4].add(new JLabel(new ImageIcon("kingred.png")));
    squares[7][5].add(new JLabel(new ImageIcon("bishopred.png")));
    squares[7][7].add(new JLabel(new ImageIcon("rookred.png")));

    for (int i = 0; i < 8; i++) {
        squares[1][i].add(new JLabel(new ImageIcon("pawngreen.png")));
        squares[6][i].add(new JLabel(new ImageIcon("pawnred.png")));
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public static void main(String[] args) {
    new board2();
}
}

我正在尝试创建一种国际象棋游戏,我需要帮助在棋盘的所有侧面放置标签以标记 AH 或 1-8 中的行和列。我不知道该怎么做。稍后我将添加一个拖放片段的功能。最好使用 JLabels 吗?无论如何,我会去把标签放在一边吗?谢谢!

4

6 回答 6

5

我想提交一个使用 Unicode 字符的简单棋盘绘图示例。这个小项目涉及 3 个类。

国际象棋标签.java

import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class ChessLabel extends JLabel {

    Font font     = new Font("Ariel", Font.PLAIN, 24);
    Color bgLight = new Color(222, 184, 135);
    Color bgDark  = new Color(139, 69, 19);

    ChessLabel(String s)
    {
        super(s);
    }

    void set(int idx, int row)
    {
      setFont(font);
          setOpaque(true);
          setBackground((idx+row)%2 == 0 ? bgDark : bgLight);
          setHorizontalAlignment( SwingConstants.CENTER );
    }

}

Board.java

import java.awt.*;
import javax.swing.JFrame;


public class Board extends JFrame {


   //Initialise arrays to hold panels and images of the board

    private ChessLabel[] labels = new ChessLabel[] {

    // white
    new ChessLabel("\u2656"), new ChessLabel("\u2658"), new ChessLabel("\u2657"), 
    new ChessLabel("\u2655"), new ChessLabel("\u2654"), new ChessLabel("\u2657"), 
    new ChessLabel("\u2658"), new ChessLabel("\u2656"), new ChessLabel("\u2659"), 
    new ChessLabel("\u2659"), new ChessLabel("\u2659"), new ChessLabel("\u2659"),
    new ChessLabel("\u2659"), new ChessLabel("\u2659"), new ChessLabel("\u2659"), 
    new  ChessLabel("\u2659"), 
    // empty
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "), 
    new ChessLabel(" "), new ChessLabel(" "),
    // black
    new ChessLabel("\u265F"), new ChessLabel("\u265F"), new ChessLabel("\u265F"), 
    new ChessLabel("\u265F"), new ChessLabel("\u265F"), new ChessLabel("\u265F"), 
    new ChessLabel("\u265F"), new ChessLabel("\u265F"), new ChessLabel("\u265C"), 
    new ChessLabel("\u265E"), new ChessLabel("\u265D"), new ChessLabel("\u265B"), 
    new ChessLabel("\u265A"), new ChessLabel("\u265D"), new ChessLabel("\u265E"), 
    new ChessLabel("\u265C")
    };

    public Board() 
    {

    } // Board()

    void display()
    {
        setTitle("Chess board with unicode images");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        Container contentPane = getContentPane();
        GridLayout gridLayout = new GridLayout(8, 8);

        contentPane.setLayout(gridLayout);

        int row = -1;
        for (int i = 0; i < labels.length; i++) 
        {
            if(i % 8 == 0) row ++; // increment row number
            labels[i].set(i, row);
            contentPane.add(labels[i]);
        } // i

        setSize(600, 600);
        setLocationRelativeTo(null);
        setVisible(true);
     } // display()

} // class Board

ChessBoardTest.java

public class ChessBoardTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Board board = new Board();

        board.display();
    }

}
于 2013-01-28T16:09:35.047 回答
3

这里。这显示了您可以使用的一些不同布局。您可能想要研究的一件事是网格布局。这将使您可以轻松地为正方形添加 JPanel。您也可以使用它在电路板上添加标签,但这只是一种方法。浏览网站上的示例,也有示例代码。

于 2010-03-29T02:11:45.960 回答
1
public class Pieces {

}

class Pawn_1 extends JComponent {

        private BufferedImage img;
        private Point imgPoint = new Point(0, 65);

        public Pawn_1() {
            try {
                img = ImageIO.read(new File("C:\\imgs\\b_pawn.png"));

            } catch (IOException ex) {
                ex.printStackTrace();
            }

            MouseAdapter ma = new MouseAdapter() {

                private Point offset;

                @Override
                public void mousePressed(MouseEvent e) {
                    Rectangle bounds = getImageBounds();
                    Point mp = e.getPoint();
                    if (bounds.contains(mp)) {
                        offset = new Point();
                        offset.x = mp.x - bounds.x;
                        offset.y = mp.y - bounds.y;
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    offset = null;
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    if (offset != null) {
                        Point mp = e.getPoint();
                        imgPoint.x = mp.x - offset.x;
                        imgPoint.y = mp.y - offset.y;
                        repaint();
                    }
                }

            };
            addMouseListener(ma);
            addMouseMotionListener(ma);
        }

        protected Rectangle getImageBounds() {
            Rectangle bounds = new Rectangle(0, 0, 0, 0);
            if (img != null) {
                bounds.setLocation(imgPoint);
                bounds.setSize(img.getWidth(), img.getHeight());
            }
            return bounds;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(65, 65);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(img, imgPoint.x, imgPoint.y, this);
                g2d.dispose();
            }
        }
    }
于 2014-10-31T19:51:33.567 回答
0
 public void paintComponent(Graphics g) {                
    Graphics2D g2 = (Graphics2D)g;
    g2.setColor(Color.WHITE);
    int row = 0; 
    int col = 0;
    int sq = 65;

    while(row != (sq * 8)){
        if(row % 10 != 0 && col % 10 == 0)g2.setColor(Color.BLACK);
        if(row % 10 != 0 && col % 10 != 0)g2.setColor(Color.WHITE);
        if(row % 10 == 0 && col % 10 == 0)g2.setColor(Color.WHITE);
        if(row % 10 == 0 && col % 10 != 0)g2.setColor(Color.BLACK);

        g2.fillRect(row, col, sq, sq);
        row = row + sq;
        if(row == (sq * 8)){
            row = 0;
            col = col + sq;
            if(col == (sq * 8))break;
        }
    }


}
于 2014-10-31T19:47:02.730 回答
0
import java.awt.*;
import java.applet.*;
public class MyChes extends Applet
{
    public void paint(Graphics g)
    {
        for(int i=50;i<=400;i+=50)
        {
           for(int j=50;j<=400;j+=50)
           {
               if((i+j)%100==0)
               {
                  g.setColor(Color.black);
                  g.fillRect(i,j,50,50);
               }
           }
           g.drawRect(50,50,400,400);
         }
     }
}

于 2016-11-06T18:38:29.303 回答
-1
import java.applet.*;
import java.lang.*;
import java.awt.*;

public class E10_2 extends Applet
{
    public void paint(Graphics g)
    {
        for(int x = 10; x < 330; x+=80)
        {
            for(int y = 10; y < 330; y+=80)
            {
                g.drawRect(8,8,322,322);
                g.drawRect(9,9,322,322);
                g.fillRect(x,y,40,40);
                g.fillRect(x+40,y+40,40,40);
            }
        }
    }
}
于 2015-08-28T14:03:36.493 回答