0

私は人生のゲームを書こうとしています。しかし、いくら頑張っても、ボタンのあるフレームにCellコンポーネントを追加できないようです。新しいフレームを作成して追加すると表示されますが、それでも複数のコンポーネントを追加できないようです。

では、すべてのものを載せたフレームにコンポーネントを追加する方法と、複数のコンポーネントを追加する方法を教えてください。ありがとう!

これが私のコードです:

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Visuals extends JFrame implements ActionListener{ 

    private static Board b;
    private GameOfLife g;
    private JButton nextGen;
    private JButton random;

    public Visuals(Board b){
        super("The Game of Life");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JPanel panel=new JPanel();
        nextGen = new JButton("Next Generation");
        panel.add(nextGen);
        nextGen.addActionListener(this);

        random = new JButton("Random");
        panel.add(random);
        random.addActionListener(this);
        this.add(panel);

        this.setSize(800, 700);
        this.setVisible(true);


        /*for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){
            for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){
                Cell c= new Cell(xShift,yShift,i,j);
                this.add(c);
            }
        }*/
        Cell c= new Cell(100,100,1,1);
        Cell d= new Cell(200,200,1,1);
        this.add(c);
        this.add(d);

        JFrame frame=new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 700);
        frame.setVisible(true);

        frame.add(c);
        frame.add(d);

    }

    public void paint(Graphics g){
        super.paint(g);
        for(int i=0, yShift=80; i<b.getRow(); i++, yShift+=20){
            for (int j=0, xShift=30; j<b.getCol(); j++, xShift+=20){    
                if(b.getSpot(i, j)==1){
                    g.setColor(Color.red);
                    g.fillRect(xShift, yShift, 10, 10);
                }
                else{
                    g.setColor(Color.white);
                    g.fillRect(xShift, yShift, 10, 10);
                }
            }   
        }
    }   

    public static void main(String[] cnbe){
        b= new Board (30,30);
        Visuals v = new Visuals(b);
    }

    public void actionPerformed(ActionEvent event) {
        Object cause = event.getSource();
        if(cause == nextGen){
            GameOfLife g=new GameOfLife(b);
            g.nextGeneration();
            System.out.println(g);
            this.repaint();
        }
        if(cause == random){
            GameOfLife g=new GameOfLife(b);
            g.random();
            System.out.println(g);
            this.repaint();
        }   
    }

    public class Cell extends JComponent{
        int row;
        int col;
        int x;
        int y;
        Rectangle r;

        public Cell(int x, int y, int row, int col){
            r= new Rectangle(x,y,10,10);
                this.row=row;
                this.col=col;
                this.x=x;
                this.y=y;

        }   


        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawString("DRAW ME",200,200);
            Graphics2D g2= (Graphics2D) g;
            g2.setColor(Color.green);
            r= new Rectangle(x,y,100,100);
            g2.fill(r);

            /*if(b.getSpot(row, col)==1){

                g.setColor(Color.red);
                g.fillRect(x, y, 10, 10);
            }
            else{

                g.setColor(Color.white);
                g.fillRect(x, y, 10, 10);
            }*/
        }

        class MousePressListener implements MouseListener{

            public void mousePressed(MouseEvent event) {
                /*int x= event.getX();
                int y= event.getY();
                if(x>r.getX() && x<r.getX()+10 && y>r.getY() && y<r.getY()+10){
                    if(b.getSpot(row, col)==1){
                        b.setSpot(row, col, 0);
                        repaint();
                    }
                    else{
                        b.setSpot(row, col, 1);
                        repaint();
                    }
                }*/
            }

            public void mouseReleased(MouseEvent arg0) {
            }
            public void mouseClicked(MouseEvent arg0) {
            }
            public void mouseEntered(MouseEvent arg0) {
            }
            public void mouseExited(MouseEvent arg0) {
            }

        }


    }

}
4

1 に答える 1

0

レイアウト マネージャーに問題があります。コンポーネントを JFrame に追加するたびに、既存のものを上書きします。私は Game of Life にはあまり詳しくありませんが、セルを JComponents にしたいとは思わないでしょう。ボードをブール値の配列として表し、生きている状態と死んでいる状態を表すことができるため、Visual コンストラクターは次のようになります。

public Visuals(Board b) {
    super("The Game of Life");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container container = getContentPane(); // 1
    JPanel panel = new JPanel(new FlowLayout()); // 2
    nextGen = new JButton("Next Generation");
    panel.add(nextGen);
    nextGen.addActionListener(this);

    random = new JButton("Random");
    panel.add(random);
    random.addActionListener(this);
    container.add(panel); // 3

    setSize(800, 700);
    setVisible(true);

}

Board クラスでは、ボードを配列として表します。

public class Board{
    private boolean[][] board;

    public Board(int x, int y) {

        board = new boolean[x][y];
    }
}

次に、nextGenerationGameOfLife クラスのメソッドで、実行している計算を実行し、ボード上の位置の真/偽 (または生きている/死んでいる) 状態を設定します。

Visuals.paint メソッドは次のようになります。

public void paint(Graphics g) {
    super.paint(g);
    for (int i = 0, yShift = 80; i < b.getRow(); i++, yShift += 20) {
        for (int j = 0, xShift = 30; j < b.getCol(); j++, xShift += 20) {
            if (b.isAlive(i, j)) {
                g.setColor(Color.red);
                g.fillRect(xShift, yShift, 10, 10);
            } else {
                g.setColor(Color.white);
                g.fillRect(xShift, yShift, 10, 10);
            }
        }
    }
}

iaAliveその位置でボード配列の値を返すだけです:

public boolean isAlive(int i, int j) {
    return board[i][j];
}

しかし、私が言うように、私はゲームに慣れていません。

于 2012-12-04T12:34:17.913 に答える