1

このプログラム内で、「LifeCell」ウィジェットの8x8グリッドを作成する必要があります。インストラクターはウィジェットが対象である必要があるとは言わなかったShapeので、私は先に進んでGridLayoutクラスを使用しました。クラスはGridLayout正常に機能します(確認する視覚的な補助がないため、私も知っています)。プログラムの目的は、ユーザーがLifeCellウィジェットの1つをクリックして、状態を切り替えることができる人生ゲームをプレイすることです。生きている」または「死んでいる」。

私の質問は、セルをペイントすることに大きく依存しています。コードに問題がある可能性がありますが、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、私はそのウェブサイトを見ていきます、ありがとう。

私の質問やコードスニペットに関する混乱を避けるために、ここで割り当てを見つけることができます。

4

3 に答える 3

2

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);
于 2009-11-30T04:26:37.547 に答える
1

LifeCellにpaintComponent()メソッドがあるのはなぜですか?カスタムペイントを行う必要はありません。次を使用して、任意のコンポーネントの背景色を変更できます。

setBackground( Color.BLUE ) 

それ以外は、あなたの質問は私には意味がありません。最初に、Shapeオブジェクトを使用する必要があると述べましたが、コードのどこにもShapeオブジェクトが表示されないので、なぜそれについて言及して質問を混乱させたのですか?

私はあなたの質問を本当に理解していません、そして私たちはあなたのコードが実際の提案を提供するのに十分ではありません。

さらにサポートが必要な場合は、問題を示すSSCCEを投稿してください。

于 2009-11-30T04:27:24.070 に答える
1

代替テキスト

あなたは正しい方向に進んでいます。

既存のコンポーネント(JPanel、JLabel、JButtonなど)を使用する場合は、コンポーネントがすでに実行していることを尊重し、必要なものをパラメーター化する方がはるかに優れています。

したがって、JPanelを使用している場合、これ(および他のJComponents)にbackgroundは変更可能なプロパティがあります。したがって、コンポーネントを自分でペイントしようとする代わりに(現在失敗しているものです)、その値を設定して、ペイント自体にペイントさせます。

セルの状態に応じて異なる色を返す「getLifeColor」を追加できます。

   private Color getLifeColor() {
       return this.alive?liveColor:deadColor;
   } 

次に、セルにこの色で背景をペイントさせます。

  public void paintComponent(Graphics gr) {
       setBackground( getLifeColor() );
       super.paintComponent( gr );
  }

その後、セルの状態をライブまたはデッドに設定するだけで、コンポーネントが対応する色で表示されます。

代替テキスト

これは、投稿したコードの短い自己完結型の正しい例(SSCCE)とライブ/デッドカラーの使用法です。そこから続けられると思います。

于 2009-11-30T16:06:52.910 に答える