Advertisement
Guest User

Game

a guest
Aug 22nd, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6.  
  7. public class Game extends JFrame implements WindowListener
  8. {
  9.   private static int DEFAULT_FPS = 80;
  10.  
  11.   private gamePanel wp;
  12.   private JTextField jtfBox;   // displays no.of boxes used
  13.   private JTextField jtfTime;  // displays time spent in game
  14.  
  15.   private int pWidth, pHeight;   // diemensions of the panel
  16.  
  17.  
  18.   public Game(long period)
  19.   { super("The Worm Chase");
  20.  
  21.     makeGUI();
  22.  
  23.     pack();   // first one (the GUI doesn't include the JPanel yet)
  24.     setResizable(false);   // sizes may change when non-resizable
  25.     calcSizes();
  26.     setResizable(true);
  27.  
  28.     Container c = getContentPane();
  29.     wp = new gamePanel(this, period, pWidth, pHeight);
  30.     c.add(wp, "Center");
  31.     pack();  // second, after JPanel added
  32.  
  33.     addWindowListener( this );
  34.  
  35.     addComponentListener( new ComponentAdapter() {
  36.       public void componentMoved(ComponentEvent e)
  37.       /* Called by the Component listener when the JFrame is
  38.          moved. Put it back in its original position. */
  39.       {  setLocation(0,0);  }
  40.     });
  41.  
  42.     setResizable(false);
  43.     setVisible(true);
  44.   }  // end of WormChase() constructor
  45.  
  46.  
  47.   private void makeGUI()
  48.   // Create the GUI, minus the JPanel drawing area
  49.   {
  50.     Container c = getContentPane();    // default BorderLayout used
  51.  
  52.     JPanel ctrls = new JPanel();   // a row of textfields
  53.     ctrls.setLayout( new BoxLayout(ctrls, BoxLayout.X_AXIS));
  54.  
  55.     jtfBox = new JTextField("Boxes used: 0");
  56.     jtfBox.setEditable(false);
  57.     ctrls.add(jtfBox);
  58.  
  59.     jtfTime = new JTextField("Time Spent: 0 secs");
  60.     jtfTime.setEditable(false);
  61.     ctrls.add(jtfTime);
  62.  
  63.     c.add(ctrls, "South");
  64.   }  // end of makeGUI()
  65.  
  66.  
  67.   public void setBoxNumber(int no)
  68.   {  jtfBox.setText("Boxes used: " + no);  }
  69.  
  70.   public void setTimeSpent(int t)
  71.   {  jtfTime.setText("Time Spent: " + t + " secs");  }
  72.  
  73.  
  74.   private void calcSizes()
  75.   /* Calculate the size of the drawing panel to fill the screen, but
  76.      leaving room for the JFrame's title bar and insets, the OS's insets
  77.      (e.g. taskbar) and the textfields under the JPanel.
  78.   */
  79.   {
  80.     GraphicsConfiguration gc = getGraphicsConfiguration();
  81.     Rectangle screenRect = gc.getBounds();
  82.     // System.out.println("Screen size: " + screenRect);
  83.  
  84.     Toolkit tk = Toolkit.getDefaultToolkit();
  85.     Insets desktopInsets = tk.getScreenInsets(gc);
  86.     // System.out.println("OS Insets: " + desktopInsets);
  87.  
  88.     Insets frameInsets = getInsets();     // only works after a pack() call
  89.     // System.out.println("JFrame Insets: " + frameInsets);
  90.  
  91.     Dimension tfDim = jtfBox.getPreferredSize();   // size of text field
  92.     // System.out.println("Box TF Size: " + tfDim );
  93.     // System.out.println("Time TF Size: " + jtfTime.getPreferredSize() );  
  94.  
  95.  
  96.     pWidth = screenRect.width - (desktopInsets.left + desktopInsets.right)
  97.                               - (frameInsets.left + frameInsets.right);
  98.  
  99.     pHeight = screenRect.height - (desktopInsets.top + desktopInsets.bottom)
  100.                                 - (frameInsets.top + frameInsets.bottom)
  101.                                 - tfDim.height;
  102.  
  103.     // System.out.println("pWidth: " + pWidth + "; pHeight: " + pHeight);
  104.   }  // end of calcSizes()
  105.  
  106.  
  107.   // ----------------- window listener methods -------------
  108.  
  109.   public void windowActivated(WindowEvent e)
  110.   { wp.resumeGame();  }
  111.  
  112.   public void windowDeactivated(WindowEvent e)
  113.   {  wp.pauseGame();  }
  114.  
  115.  
  116.   public void windowDeiconified(WindowEvent e)
  117.   {  wp.resumeGame();  }
  118.  
  119.   public void windowIconified(WindowEvent e)
  120.   {  wp.pauseGame(); }
  121.  
  122.   public void windowClosing(WindowEvent e)
  123.   {  wp.stopGame();  }
  124.  
  125.   public void windowClosed(WindowEvent e) {}
  126.   public void windowOpened(WindowEvent e) {}
  127.  
  128.  
  129.   // ----------------------------------------------------
  130.  
  131.   public static void main(String args[])
  132.   {  
  133.     int fps = DEFAULT_FPS;
  134.     if (args.length != 0)
  135.       fps = Integer.parseInt(args[0]);
  136.  
  137.     long period = (long) 1000.0/fps;
  138.     System.out.println("fps: " + fps + "; period: " + period + " ms");
  139.  
  140.     new Game(period*1000000L);    // ms --> nanosecs
  141.   }
  142.  
  143. } // end of WormChase class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement