Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1.  
  2.  
  3. package itm225_proj4;
  4.  
  5. import javax.swing.*;
  6. import java.awt.*;
  7.  
  8. public class RocketDisplay extends JApplet {
  9.  
  10.     Rocket rocket; //the rocket to draw
  11.     TakeOffText takeoff; // the take off text to draw
  12.    
  13.     ReadyState readystate; //the state of the rocket's launch procedure: either READY, NOT_READY, OR COUNTING
  14.  
  15.     //The distance to move the rocket each frame
  16.     final int OFFSET = 2;
  17.    
  18.     //the starting position of the rocket
  19.     final int STARTPOSITION = 550-OFFSET;
  20.  
  21.     //enum used to specify state of the rocket's launch procedure: see above
  22.     public enum ReadyState {
  23.         NOT_READY, READY, COUNTING;
  24.     }
  25.  
  26.     /*this is the method that is called when the applet is first loaded.
  27.      * it initializes any variables needed by paint() then starts a new thread
  28.      */
  29.     @Override
  30.     public void init() {
  31.         rocket = new Rocket(STARTPOSITION);
  32.         takeoff = new TakeOffText();
  33.         readystate = ReadyState.NOT_READY;
  34.         /*to start a thread you have to give a new thread's constructor an object
  35.          *of type "Runnable." Since Runnable is an interface you have to implement it
  36.          * see the implementation class below
  37.          */
  38.          
  39.         (new Thread(new Runner())).start();
  40.     }
  41.  
  42.     /*This method is used by the event dispatcher thread when an event triggers
  43.      * a window repaint
  44.      *
  45.      */
  46.     @Override
  47.     public void paint(Graphics g) {
  48.        
  49.         //Stuff that always happens first
  50.         rocket.erase(g);
  51.  
  52.  
  53.         //Stuff that might not happen
  54.         switch (readystate) {
  55.             case NOT_READY:
  56.                 LaunchPad.paint(g); //only draw launchpad at initial state
  57.                 break;
  58.             case READY:
  59.                 rocket.moveRocketUp(OFFSET);//only move rocket when rocket is ready to be launched
  60.                 break;
  61.             case COUNTING:
  62.                 takeoff.paint(g); //only draw take off text when countdown is still running
  63.                 break;
  64.  
  65.         }
  66.  
  67.         //Stuff that always happens last
  68.  
  69.  
  70.         rocket.paint(g);
  71.  
  72.  
  73.     }
  74.  
  75.     /*
  76.      * This class contains a run method which is invoked upon creation of new threads
  77.      * using objects from this class
  78.      */
  79.     class Runner implements Runnable {
  80.  
  81.  
  82.         public void run() {
  83.  
  84.             /* Draw the rocket and launch pad */
  85.             repaint();
  86.  
  87.             //Ask user if they would like to launch the rocket
  88.             int input = JOptionPane.showConfirmDialog(null, "Would you like to launch the rocket?", "Entery Yes/No", JOptionPane.YES_NO_OPTION);
  89.            
  90.             //Exit if they say no
  91.             if (input != 0)
  92.                 System.exit(0);
  93.  
  94.                
  95.                 //change ready state to counting so that paint method will draw take off text when called
  96.                 readystate = ReadyState.COUNTING;
  97.  
  98.                 //loop until take off text's boolean method ready() returns false i.e. the countdown has reached "take off"
  99.                 while (!takeoff.ready()) {
  100.                     repaint(); //draw take off text
  101.  
  102.                     try {
  103.                         //wait 1 second before looping or continuing
  104.                         Thread.sleep(1000);
  105.                     } catch (InterruptedException ex) {
  106.  
  107.                     }
  108.                 }
  109.                
  110.                 //change ready state to ready so that paint method will move the rocket when called
  111.                 readystate = ReadyState.READY;
  112.                
  113.                 //loop until rocket is off the screen
  114.                 while(!rocket.isOffScreen()) {
  115.  
  116.                     //paint the rocket at new location
  117.                     repaint();
  118.  
  119.                    
  120.                     try {
  121.                         //wait 24 milliseconds before drawing next frame
  122.                         Thread.sleep(24);
  123.                     } catch (InterruptedException ex) {
  124.  
  125.                     }
  126.                 }
  127.                
  128.                 //rocket has completely launched
  129.  
  130.                 //set rocket to original state (at launchpad)
  131.                 rocket = new Rocket(STARTPOSITION);
  132.                 //set take off text to original state (countdown at 5)
  133.                 takeoff = new TakeOffText();
  134.                
  135.                 //set ready state to not_ready so that paint will draw rocket at current position without moving it
  136.                 readystate = ReadyState.NOT_READY;
  137.                
  138.                 //start a new thread just like this one, but at the beginning
  139.                 (new Thread(new Runner())).start();
  140.                
  141.                 //this thread ends.
  142.  
  143.         }
  144.  
  145.     }
  146.  
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement