Advertisement
Guest User

First Class

a guest
Jan 21st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.JFrame;
  3.  
  4. public class MovingSquaresApplication extends JFrame implements Runnable {
  5.  
  6.     private static final Dimension WindowSize = new Dimension(600,600);
  7.     private static final int NUMGAMEOBJECTS = 30;
  8.     private  GameObject[] GameObjectsArray  = new GameObject[NUMGAMEOBJECTS];  
  9.    
  10.    
  11.    
  12.     public MovingSquaresApplication(){
  13.         //Create and set up the window.
  14.         this.setTitle("Moving Squares");
  15.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.         //Display the window, centred on the screen
  17.         Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  18.         int x = screensize.width/2 - WindowSize.width/2;
  19.         int y = screensize.height/2 - WindowSize.height/2;
  20.         setBounds(x, y, WindowSize.width, WindowSize.height);
  21.         setVisible(true);
  22.        
  23.         for (int i = 0; i < NUMGAMEOBJECTS; i++){
  24.             GameObjectsArray[i] = new GameObject();
  25.            
  26.         }
  27.        
  28.        
  29.         Thread t = new Thread(this);
  30.         t.start();
  31.        
  32.                
  33.    
  34.        
  35.     }
  36.    
  37.     public static void main(String[] args){
  38.        
  39.         MovingSquaresApplication app = new MovingSquaresApplication();
  40.        
  41.     }
  42.    
  43.    
  44.     public void run(){
  45.         while(true){
  46.            
  47.         try {
  48.             for (int a = 0; a < NUMGAMEOBJECTS; a++){
  49.                 GameObjectsArray[a].move();
  50.                 }
  51.            
  52.             Thread.sleep(16);
  53.         } catch (InterruptedException e) {
  54.  
  55.         }
  56.         this.repaint();
  57.                 }
  58.        
  59.        
  60.     }
  61.    
  62.     public void paint(Graphics g){
  63.         g.clearRect(0, 0, 600, 600);
  64.        
  65.         for(int z = 0; z < NUMGAMEOBJECTS; z++){
  66.             GameObjectsArray[z].paint(g);
  67.        
  68.         }
  69.        
  70.     }
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement