在面板和主框架之间发送信息

时间:2014-09-26 18:26:38

标签: java swing jframe jpanel

我的comp sci课程的中期作业是我必须创建一个tictactoe游戏。我有9个单独的JPanel对象来表示tile,所有这些对象都使用int值“turn”来确定将哪个图形放入面板中。我的问题是我需要这样做,以便当您单击面板中的按钮时,它会更改框架内的CellPanel对象的所有的转弯值,而不仅仅是该实例,所以下次你拿一个面板时,它会放一个新的图形,而不是像以前一样的图形。

我的Mid1课程采用主要方法:

import java.awt.*;
import javax.swing.*;

public class Mid1 extends JFrame {
  public Mid1() {
    setLayout(new GridLayout(3, 3));

    CellPanel panel1 = new CellPanel();
    add(panel1);
    CellPanel panel2 = new CellPanel();
    add(panel2);
    CellPanel panel3 = new CellPanel();
    add(panel3);
    CellPanel panel4 = new CellPanel();
    add(panel4);
    CellPanel panel5 = new CellPanel();
    add(panel5);
    CellPanel panel6 = new CellPanel();
    add(panel6);
    CellPanel panel7 = new CellPanel();
    add(panel7);
    CellPanel panel8 = new CellPanel();
    add(panel8);
    CellPanel panel9 = new CellPanel();
    add(panel9);
  }

  public static void main(String[] args) {
    Mid1 frame = new Mid1();
    frame.setTitle("mid1");
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setVisible(true);

  }
}

我的CellPanel课程:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class CellPanel extends JPanel implements ActionListener{
  Image cross = new ImageIcon("image/x.gif").getImage();
  Image not = new ImageIcon("image/o.gif").getImage();
  JButton button;
  int turn = 0;
  int draw;

  public CellPanel(){
      BorderLayout layout = new BorderLayout();
      setLayout(layout);
      button = new JButton("Take");
      button.setPreferredSize(new Dimension(0,20));
      button.addActionListener(this);
      this.add(button, layout.SOUTH);
  }

  public void setTurn(int t){
      turn = t;
  }

  public int getTurn(){
      return turn;
  }

  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (draw == 1)
        g.drawImage(cross, 0, 0, getWidth(), getHeight(), this);
    if (draw == 2)
        g.drawImage(not, 0, 0, getWidth(), getHeight(), this);

      System.out.println(turn);

    /*int mode = (int)(Math.random() * 3);

    if (mode == 0) {
      g.drawImage(cross, 0, 0, getWidth(), getHeight(), this);
    }
    else if (mode == 1) {
      g.drawImage(not, 0, 0, getWidth(), getHeight(), this);
    }*/
  }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(turn) {
            case 0:
                draw = 1;
                this.repaint();
                turn = 1;
                break;
            case 1: 
                draw = 2;
                this.repaint();
                turn = 0;
                break;
            default: turn = 0;
                break;
        }
        this.remove(button);
    }
}

2 个答案:

答案 0 :(得分:1)

让每个CellPanel知道游戏状态何时使用observer pattern更改;建议使用几种实现here。虽然CellPanel可以有用地扩展JPanel以建立preferred size,但它仍然可以实现Observer观察者。您的游戏模型应该会通知{1}}。

,该模型应该通知侦听细胞自己重绘

答案 1 :(得分:0)

有几种方法可以做到。

假设您只有一个tic tac toe board,最简单的方法是将turn声明为static

static int turn = 0;

然后你可以使getset静态,你只需要为其中一个CellPanel更改它,以便识别转弯已经改变。

CellPanel.setTurn(1); 
//sets turn for static int turn to 1. All CellPanels share the same turn value and recognize this as such

话虽如此,我不确定你的turn项的目的是什么。它表示玩家1,玩家2?哪个转身呢?还有别的吗?