Javaを使って簡単なJavaリアクションゲームを作っています。Display は、GUI を初期化する私のクラスです。
public class Display extends JFrame implements Gui{
//Connect gui to controller
//(This method will be called before ANY other methods)
public void connect(Controller controller){
}
//Initialise the gui
public Display(){
JPanel panel = new JPanel();
JButton coin = new JButton();
JButton goStop = new JButton();
JLabel prompt = new JLabel("Insert Coin", JLabel.CENTER);
setTitle("Reaction Game");
setContentPane(panel);
coin.setIcon(new ImageIcon("coin.png"));
goStop.setIcon(new ImageIcon("GoButton.png"));
//setting layout of panel
panel.setLayout(new BorderLayout(100, 20));
//adding buttons to panel
panel.add(prompt, BorderLayout.PAGE_START);
panel.add(coin, BorderLayout.LINE_START);
panel.add(goStop, BorderLayout.LINE_END);
setSize(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
ボタンが実際に行うことは、Controller1 と Controller2 (Controller クラスを実装する) の 2 つの異なるクラスで実装され、どちらを使用するかによってそれぞれが異なることを行います。
コントローラー1:
public class Controller1 は Controller{ を実装します
//Creating a constructor
public Controller1(){
}
//Connect controller to gui
//(This method will be called before ANY other methods)
public void connect(Gui gui, Random rng){
}
}
コントローラー2:
public class Controller2 は Controller{ を実装します。
//Creating a constructor
public Controller2(){
}
//Connect controller to gui
//(This method will be called before ANY other methods)
public void connect(Gui gui, Random rng){
}
}
GUIとコントローラーをどのように接続するのだろうと思っていました。