KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > khumpangame > MoveApp


1 package khumpangame;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 /** A transcription of the famous bloc moving game
9 *
10 * [1] http://www.asiaspiel.ch/
11 */

12 public class MoveApp extends JFrame
13 {
14   private final Model model = new Model();
15   private final MoveView view = new MoveView(model, 50, true);
16
17   /** @param standalone if true, exit the JVM on close
18   */

19   public MoveApp(boolean standalone)
20   {
21     super("Khun Pan Game");
22     this.setResizable(false);
23     if(standalone)
24     {
25       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26     }
27
28     // north: explain area
29
JTextArea explainArea = new JTextArea("The goal is to move the blocs below\r\nto reach the end position\r\ndepicted on the right.");
30     JPanel explainPanel = new JPanel(new BorderLayout());
31     explainPanel.setBorder(new EmptyBorder(2,2,2,4));
32     explainPanel.add(explainArea, BorderLayout.CENTER);
33     explainArea.setBackground(this.getBackground());
34     explainArea.setEditable(false);
35     explainArea.setBorder(new EmptyBorder(5,5,5,5));
36     Model endModel = new Model(Model.standardProblemEndPosition, Model.standardProblemEndPosition); // we just want a pure view of the end positions
37
MoveView endView = new MoveView(endModel, 8, false);
38     explainPanel.add(endView, BorderLayout.EAST);
39     this.getContentPane().add(explainPanel, BorderLayout.NORTH);
40
41     // center
42
view.setAlignmentX(JComponent.CENTER_ALIGNMENT);
43     this.getContentPane().add(view, BorderLayout.CENTER);
44
45     JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
46     this.getContentPane().add(controlPanel, BorderLayout.SOUTH);
47     JButton reset = new JButton("Reset");
48     controlPanel.add(reset);
49     reset.addActionListener(new ActionListener()
50     {
51       public void actionPerformed(ActionEvent ae)
52       {
53         model.reset();
54         view.repaint();
55       }
56     });
57
58     pack();
59     this.setLocationRelativeTo(null);
60     this.setVisible(true);
61   } // Constructor
62

63
64
65   public static void main(String JavaDoc[] args)
66   {
67     new MoveApp(true);
68   }
69
70
71
72 } // MoveApp
Popular Tags