import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Game extends JFrame implements WindowListener { private static int DEFAULT_FPS = 80; private gamePanel wp; private JTextField jtfBox; // displays no.of boxes used private JTextField jtfTime; // displays time spent in game private int pWidth, pHeight; // diemensions of the panel public Game(long period) { super("The Worm Chase"); makeGUI(); pack(); // first one (the GUI doesn't include the JPanel yet) setResizable(false); // sizes may change when non-resizable calcSizes(); setResizable(true); Container c = getContentPane(); wp = new gamePanel(this, period, pWidth, pHeight); c.add(wp, "Center"); pack(); // second, after JPanel added addWindowListener( this ); addComponentListener( new ComponentAdapter() { public void componentMoved(ComponentEvent e) /* Called by the Component listener when the JFrame is moved. Put it back in its original position. */ { setLocation(0,0); } }); setResizable(false); setVisible(true); } // end of WormChase() constructor private void makeGUI() // Create the GUI, minus the JPanel drawing area { Container c = getContentPane(); // default BorderLayout used JPanel ctrls = new JPanel(); // a row of textfields ctrls.setLayout( new BoxLayout(ctrls, BoxLayout.X_AXIS)); jtfBox = new JTextField("Boxes used: 0"); jtfBox.setEditable(false); ctrls.add(jtfBox); jtfTime = new JTextField("Time Spent: 0 secs"); jtfTime.setEditable(false); ctrls.add(jtfTime); c.add(ctrls, "South"); } // end of makeGUI() public void setBoxNumber(int no) { jtfBox.setText("Boxes used: " + no); } public void setTimeSpent(int t) { jtfTime.setText("Time Spent: " + t + " secs"); } private void calcSizes() /* Calculate the size of the drawing panel to fill the screen, but leaving room for the JFrame's title bar and insets, the OS's insets (e.g. taskbar) and the textfields under the JPanel. */ { GraphicsConfiguration gc = getGraphicsConfiguration(); Rectangle screenRect = gc.getBounds(); // System.out.println("Screen size: " + screenRect); Toolkit tk = Toolkit.getDefaultToolkit(); Insets desktopInsets = tk.getScreenInsets(gc); // System.out.println("OS Insets: " + desktopInsets); Insets frameInsets = getInsets(); // only works after a pack() call // System.out.println("JFrame Insets: " + frameInsets); Dimension tfDim = jtfBox.getPreferredSize(); // size of text field // System.out.println("Box TF Size: " + tfDim ); // System.out.println("Time TF Size: " + jtfTime.getPreferredSize() ); pWidth = screenRect.width - (desktopInsets.left + desktopInsets.right) - (frameInsets.left + frameInsets.right); pHeight = screenRect.height - (desktopInsets.top + desktopInsets.bottom) - (frameInsets.top + frameInsets.bottom) - tfDim.height; // System.out.println("pWidth: " + pWidth + "; pHeight: " + pHeight); } // end of calcSizes() // ----------------- window listener methods ------------- public void windowActivated(WindowEvent e) { wp.resumeGame(); } public void windowDeactivated(WindowEvent e) { wp.pauseGame(); } public void windowDeiconified(WindowEvent e) { wp.resumeGame(); } public void windowIconified(WindowEvent e) { wp.pauseGame(); } public void windowClosing(WindowEvent e) { wp.stopGame(); } public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} // ---------------------------------------------------- public static void main(String args[]) { int fps = DEFAULT_FPS; if (args.length != 0) fps = Integer.parseInt(args[0]); long period = (long) 1000.0/fps; System.out.println("fps: " + fps + "; period: " + period + " ms"); new Game(period*1000000L); // ms --> nanosecs } } // end of WormChase class