0

我是 Java 新手,整个使用数组 + 使用类及其方法来填充和显示数组。我花了过去 2-3 个小时,研究这个并检查谷歌的各种答案,但似乎没有任何帮助,我仍然被困住。在使用数组时,我真的不知道如何使用 rectangle1 类中的方法。

主类:

public static void main(String[] args) {
    GameBoard frame = new GameBoard();
}

游戏板类

public class GameBoard extends JFrame {

    public GameBoard() {

        JFrame frame = new JFrame();
        frame.setBounds(0, 0, 195, 215);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Rectangle1 board[][] = new Rectangle1[3][3];
        for (int row = 0; row < board.length; row++) {
            for (int col = 0; col < board[row].length; col++) {
                board[row][col] = new Rectangle1(0, 0, 195, 215);
                if ((row + col) % 2 == 0) {
                    Graphics g = null;    
                    board[row][col].paint(g);
                    g.setColor(Color.yellow);
                    g.fillRect(0, 0, 195, 215);
                } else {
                    Graphics h = null;
                    board[row][col].paint(h);
                    h.setColor(Color.red);
                    h.fillRect(0, 0, 195, 215);
                }
                frame.add(board[row][col]);
            }    
        }  

    }
}

矩形 1 类。

public class Rectangle1 extends JComponent  {

    /** post:   getX() == x  and  getY() == y
     *          and  getWidth() == w  and getHeight() == h
     *          and  getBackground() == Color.black
     */
    public Rectangle1(int x, int y, int w, int h)  {
        super();
        setBounds(x, y, w, h);
        setBackground(Color.black);
    }

    /** post:   this method draws a filled Rectangle
     *          and  the upper left corner is (getX(), getY()) 
     *          and  the rectangle's dimensions are getWidth() and getHeight()
     *          and  the rectangle's color is getBackground()
     */
    public void paint(Graphics g)  {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth()-1, getHeight()-1);
        paintChildren(g);
   }

}
4

4 回答 4

1

Firstly, you are fighting the layout manager, calling something like setBounds(x, y, w, h) in you Rectangle class is going to overridden by the layout manager of the parent container (the frame in this case)

Secondly, you are not responsible for painting, do this;

board[row][col].paint(g);
g.setColor(Color.yellow);
g.fillRect(0, 0, 195, 215);

...is wrong. If it doesn't end up with you programming crashing, then your lucky.

While I try and figure out a solution, you might like to take some time and read through

While on my rant...

This; public void paint(Graphics g) { g.setColor( getBackground() ); g.fillRect(0, 0, getWidth()-1, getHeight()-1); paintChildren(g); }

is inappropriate. You MUST call super.paint(), there's to much going on in the background for you to override this method without calling it's super.

In fact, you should avoid overriding paint where ever possible and user paintComponent instead.

Also, coordinates in Swing are relative to the component. That is, the top/left corner of any component (within the context of the component) is always 0x0

Here's a working example

public class TestBoard {

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

    public TestBoard() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                new GameBoard();

            }
        });

    }

    public class GameBoard extends JFrame {

        public GameBoard() {

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;

            Rectangle1 board[][] = new Rectangle1[3][3];
            for (int row = 0; row < board.length; row++) {

                gbc.gridx = 0;

                for (int col = 0; col < board[row].length; col++) {
                    board[row][col] = new Rectangle1(195, 215);
                    if ((row + col) % 2 == 0) {
                        board[row][col].setBackground(Color.YELLOW);
                    } else {
                        board[row][col].setBackground(Color.RED);
                    }
                    frame.add(board[row][col], gbc);
                    gbc.gridx++;
                }

                gbc.gridy++;

            }

            frame.pack();
            frame.setVisible(true);

        }
    }

    public class Rectangle1 extends JPanel {

        public Rectangle1(int w, int h) {
            setPreferredSize(new Dimension(w, h));
        }

    }
}

Because of the nature of your requirements, I've been forced to use GridBagLayout which is not the simplest of layout managers to use.

JComponents are transparent by nature and you are required to fill them, better to use JPanel

于 2012-10-05T01:54:13.270 回答
1

注释:

  1. GameBoardextendsJFrame但是你JFrame在构造函数中创建了一个实例GameBoard。你应该做一个或另一个,但不能两者都做。

  2. Rectangle.paint()应该是Rectangle.paintComponent()。现在您只需创建 Rectangle 对象并将它们添加到容器中,例如 aJFrame或 a JPanel。它们将被自动绘制。paint()如果您要paintComponent()扩展JComponent.

于 2012-10-05T01:59:54.507 回答
0

You are using the null Graphics object to paint your window. Instead of using

Graphics g = null; Graphics h = null;

Use

Graphics g = getGraphics(); Graphics h = getGraphics();

Most probably this would have given you a NullPointerException isn't it?

And more over since your rectangles are inheriting from the JComponent, you should not call paint() method yourself. Instead call repaint() once all of them are added to the frame.

于 2012-10-05T01:52:25.700 回答
0

Here is your problem:

Graphics g = null;
board[row][col].paint(g);

You calling paint() with a null Graphics which causes a NullPointerException

public void paint(Graphics g)  {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getWidth()-1, getHeight()-1);
        paintChildren(g);
}
于 2012-10-05T01:52:49.413 回答