0

私はJavaを初めて使用し、配列を操作し、クラスとそのメソッドを使用して配列にデータを入力して表示します。私は過去2〜3時間かけてこれに取り組み、グーグルでさまざまな答えをチェックしましたが、何も役に立たなかったようですが、まだ行き詰まっています。配列を使用するときに、rectangle1クラスのメソッドを使用する方法がよくわかりません。

メインクラス:

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

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]);
            }    
        }  

    }
}

Rectangle1クラス。

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

コメント:

  1. GameBoard拡張しますが、コンストラクターでインスタンスJFrameを作成します。どちらか一方を実行する必要がありますが、両方を実行することはできません。JFrameGameBoard

  2. Rectangle.paint()おそらくRectangle.paintComponent()。ここで、Rectangleオブジェクトを作成し、それらをaJFrameや。などのコンテナに追加する必要がありJPanelます。それらは自動的にペイントされます。を拡張する場合は、を呼び出す必要はなくpaint()、明示的に呼び出す必要もありません。paintComponent()JComponent

于 2012-10-05T01:59:54.507 に答える
1

まず、あなたはレイアウトマネージャーと戦っていますsetBounds(x, y, w, h)。あなたRectangleのクラスのようなものを呼び出すと、親コンテナー(この場合はフレーム)のレイアウトマネージャーによってオーバーライドされます。

第二に、あなたは絵を描く責任がありません、これをしてください。

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

...間違っている。プログラミングがクラッシュしない場合は、幸運です。

私が解決策を見つけようとしている間、あなたは少し時間をかけて読んでみたいと思うかもしれません

私の暴言を吐いている間...

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

不適切です。を呼び出さなければなりません。super.paint()このメソッドを呼び出さずにオーバーライドするには、バックグラウンドで多くのことが行われていますsuper

実際、可能な限りオーバーライドを避け、代わりにpaintユーザーを使用する必要がありますpaintComponent

また、Swingの座標はコンポーネントを基準にしています。つまり、(コンポーネントのコンテキスト内の)コンポーネントの左上隅は常に0x0です。

これが実際の例です

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));
        }

    }
}

要件の性質上、私は使用を余儀なくされましたが、GridBagLayoutこれは最も単純なレイアウトマネージャーではありません。

JComponents本質的に透明であり、それらを埋める必要があります。JPanel

于 2012-10-05T01:54:13.270 に答える
0

Graphicsオブジェクトを使用しnullてウィンドウをペイントしています。使用する代わりに

Graphics g = null; Graphics h = null;

使用する

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

おそらくこれはあなたにNullPointerExceptionそれを与えたでしょう?

さらに、長方形はから継承しているため、自分でメソッドJComponentを呼び出さないでpaint()ください。代わりrepaint()に、それらすべてがフレームに追加されたら呼び出します。

于 2012-10-05T01:52:25.700 に答える
0

ここにあなたの問題があります:

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

nullのグラフィックを使用してpaint()を呼び出すと、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 に答える