GridLayoutおよびpaintComponentメソッドに関するJava Swingの質問
-
08-07-2019 - |
質問
このプログラムでは、「LifeCell」の8x8グリッドを作成する必要があります。ウィジェット。インストラクターは、ウィジェットが Shape
のオブジェクトでなければならないことに言及していなかったため、先に進み、 GridLayout
クラスを使用しました。 GridLayout
クラスは正常に機能します(確認する視覚的補助がないため、私も知っています)。プログラムの目的は、ユーザーがゲームの1つをクリックできるGame of Lifeをプレイすることです。 LifeCellウィジェットと、「生きている」または「死んでいる」状態の切り替え。
私の質問は、セルをペイントすることに大きく依存しています。コードに問題がある可能性がありますが、100%確信はありません。
Program2.java
public class Program2 extends JPanel implements ActionListener {
private LifeCell[][] board; // Board of life cells.
private JButton next; // Press for next generation.
private JFrame frame; // The program frame.
public Program2() {
// The usual boilerplate constructor that pastes the main
// panel into a frame and displays the frame. It should
// invoke the "init" method before packing the frame
frame = new JFrame("LIFECELL!");
frame.setContentPane(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.init();
frame.pack();
frame.setVisible(true);
}
public void init() {
// Create the user interface on the main panel. Construct
// the LifeCell widgets, add them to the panel, and store
// them in the two-dimensional array "board". Create the
// "next" button that will show the next generation.
LifeCell[][] board = new LifeCell[8][8];
this.setPreferredSize(new Dimension(600, 600));
this.setBackground(Color.white);
this.setLayout(new GridLayout(8, 8));
// here is where I initialize the LifeCell widgets
for (int u = 0; u < 8; u++) {
for (int r = 0; r < 8; r++) {
board[u][r] = new LifeCell(board, u, r);
this.add(board[u][r]);
this.setVisible(true);
}
}
LifeCell.java
public class LifeCell extends JPanel implements MouseListener {
private LifeCell[][] board; // A reference to the board array.
private boolean alive; // Stores the state of the cell.
private int row, col; // Position of the cell on the board.
private int count; // Stores number of living neighbors.
public LifeCell(LifeCell[][] b, int r, int c) {
// Initialize the life cell as dead. Store the reference
// to the board array and the board position passed as
// arguments. Initialize the neighbor count to zero.
// Register the cell as listener to its own mouse events.
this.board = b;
this.row = r;
this.col = c;
this.alive = false;
this.count = 0;
addMouseListener(this);
}
ここに paintComponent
メソッドがあります:
public void paintComponent(Graphics gr) {
// Paint the cell. The cell must be painted differently
// when alive than when dead, so the user can clearly see
// the state of the cell.
Graphics2D g = (Graphics2D) gr;
super.paintComponent(gr);
g.setPaint(Color.BLUE);
}
それを修正するのに正確な解決策は必要ありませんが、私はそれを機能させようとしています。
ありがとう。
編集:
Program2.javaクラスのセグメントを追加しました。明日も確認できます。私は就寝します。すべての助けに感謝します。
編集#2:
フレームごとに8x8の GridLayout
を挿入すると、個々の「セル」が混乱します。より良い単語がないため、タイプは LifeCell
です。各 LifeCell
の異なる色をペイントするにはどうすればよいですか?もしそれがあなたたちにとってまったく理にかなっているなら、私はできる限りそれを修正しようとすることができます。そしてcamickr、私はそのウェブサイトを見ます、ありがとう。
割り当てはこちらで見つけることができます。私の質問やコードスニペットについて。
解決
あなたは正しい軌道に乗っています。
既存のコンポーネント(JPanel、JLabel、JButtonなど)を使用する場合は、コンポーネントがすでに行っていることを尊重し、必要なものをパラメータ化することをお勧めします。
JPanelを使用している場合、これ(および他のJComponents)には、変更可能な background
プロパティがあります。そのため、コンポーネントを自分でペイントしようとするのではなく(今は失敗しています)、その値を設定し、ペイントにペイントさせるだけです。
&quot; getLifeColor&quot;を追加できます。セルの状態に応じて異なる色を返します:
private Color getLifeColor() {
return this.alive?liveColor:deadColor;
}
そして、セルにこの色で背景をペイントさせます:
public void paintComponent(Graphics gr) {
setBackground( getLifeColor() );
super.paintComponent( gr );
}
その後、セルの状態をライブまたはデッドに設定するだけで、コンポーネントは対応する色で表示されます:
こちら投稿したコードの短い自己包含正しい例(SSCCE)+ライブ/デッドカラー使用法。そこから続けられると思います。
他のヒント
JPanelには、デフォルトの優先サイズまたは表示コンテンツはありません。何らかの種類の可視コンポーネント(JLabelなど)を追加するか、適切なサイズを指定する必要があります。
これに加えて、次のように設定した場合、レイアウトは機能するはずです:
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
cp.add(new JLabel(i + "-" + j));
frame.pack();
frame.setVisible(true);
LifeCellにpaintComponent()メソッドさえあるのはなぜですか?カスタムペイントを行う必要はありません。以下を使用して、コンポーネントの背景色を変更できます。
setBackground( Color.BLUE )
それ以外の質問は私には意味がありません。最初に、Shapeオブジェクトを使用する必要があると言いますが、コードのどこにもShapeオブジェクトが表示されないので、なぜそれを言及することで質問を混乱させたのですか?
あなたの質問を本当に理解していないし、本当の提案をするのに十分なコードがありません。
さらにヘルプが必要な場合は、問題を示す SSCCE を投稿してください。