创建简单的TicTacToe GUI游戏

时间:2014-07-27 14:59:20

标签: java swing user-interface paintcomponent mouselistener

我正在尝试使用GUI创建一个简单的Tic-Tac-Toe游戏。我有一个按钮,显示玩家的回合(当按下按钮本身将其文本更改为0或1(绘制X或O))。 但是我在这里遇到了一个问题,我不知道为什么我的MouseListener中的测试应用于每个Jcomponent对象(我的类TicTacToe扩展它)和GridLayout(3,3)中的所有9个面板都是充满了相同的图画...

这是我的TicTacToe课程

public class TicTacToe extends JComponent{
private int ovalOrX = 0;

public TicTacToe(){
    super.setPreferredSize(new Dimension(300,300));
}
public void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.drawLine(0, 0, getWidth(), 0);//top line
    g.drawLine(0, 0, 0, getHeight());//left line
    g.drawLine(0, 300, getWidth(), getHeight());//botton line
    g.drawLine(getWidth(),getHeight(), getWidth(), 0);//right line

    if(ovalOrX == 1){
        g.setColor(Color.RED);
        g.drawLine(0,0,100,100);
        g.drawLine(0,100,100,0);
    }
    if(ovalOrX == 2){
        g.setColor(Color.BLUE);
        g.drawOval(0,0,100,100);
    }
}
public void drawX(){
    ovalOrX = 1;
    repaint();       
}
public void drawCircle(){
    ovalOrX = 2;
    repaint();
}

}

这是我的JFrame类:

public class TicTacToeFrame extends JFrame {

    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 300;
    private JPanel mainPanel;
    private TicTacToe scene;
    private TicTacToe s00;
    private TicTacToe s01;
    private TicTacToe s02;
    //finish top raw
    private TicTacToe s10;
    private TicTacToe s11;
    private TicTacToe s12;
    //finish middle raw

    private TicTacToe s20;
    private TicTacToe s21;
    private TicTacToe s22;
    private int buttonInt = 0;
    private JButton OX;

    class AddButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            if(buttonInt == 0){
                buttonInt++;
            }
            else if(buttonInt == 1){
                buttonInt = 0;
            }
            OX.setText("Player: " + buttonInt);
        }
    }
    class MyMouseListener extends MouseAdapter{
        public void mouseClicked(MouseEvent event){
            TicTacToe[] myPanelArray = {s00,s01,s02,s10,s11,s12,s20,s21,s22};
            if(buttonInt == 0){
               for(int i = 0; i < myPanelArray.length;i++){
                   if(myPanelArray[i].contains(event.getPoint())){
                      myPanelArray[i].drawCircle();
                   }
               } 
            }
        }
    }
    public TicTacToeFrame(){
        scene = new TicTacToe();
        scene.setPreferredSize(new Dimension(300,300));
        add(scene,BorderLayout.CENTER);
        OX = new JButton("Player: " + buttonInt);
        OX.addActionListener(new AddButtonListener());
        add(OX,BorderLayout.NORTH);

        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3,3));               
        fillScene();
        add(mainPanel,BorderLayout.CENTER);
        setSize(FRAME_WIDTH,FRAME_HEIGHT);
    }

    private void fillScene(){
       s00 = new TicTacToe();
       s00.addMouseListener(new MyMouseListener());
       s01 = new TicTacToe();
       //s01.addMouseListener(new MyMouseListener());
       s02 = new TicTacToe();
       s10 = new TicTacToe();
       s11 = new TicTacToe();
       s12 = new TicTacToe();
       s20 = new TicTacToe();
       s21 = new TicTacToe();
       s22 = new TicTacToe();
        //finish bottom raw
        mainPanel.add(s00);
        mainPanel.add(s01);
        mainPanel.add(s02);
        mainPanel.add(s10);
        mainPanel.add(s11);
        mainPanel.add(s12);
        mainPanel.add(s20);
        mainPanel.add(s21);
        mainPanel.add(s22);
    }    
}

1 个答案:

答案 0 :(得分:2)

event.getPoint()的返回值是相对于源组件的,在这种情况下为TicTacToe,不是整个帧。因此,对于所有TictacToe都是如此,因为它们的大小都相同。

Istead,使用Event.getSource()来确定源对象。

此外,建议您将所有TicTacToe个对象放入TicTacToe[][]而不是单独列出。