Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package itm225_proj4;
- import javax.swing.*;
- import java.awt.*;
- public class RocketDisplay extends JApplet {
- Rocket rocket; //the rocket to draw
- TakeOffText takeoff; // the take off text to draw
- ReadyState readystate; //the state of the rocket's launch procedure: either READY, NOT_READY, OR COUNTING
- //The distance to move the rocket each frame
- final int OFFSET = 2;
- //the starting position of the rocket
- final int STARTPOSITION = 550-OFFSET;
- //enum used to specify state of the rocket's launch procedure: see above
- public enum ReadyState {
- NOT_READY, READY, COUNTING;
- }
- /*this is the method that is called when the applet is first loaded.
- * it initializes any variables needed by paint() then starts a new thread
- */
- @Override
- public void init() {
- rocket = new Rocket(STARTPOSITION);
- takeoff = new TakeOffText();
- readystate = ReadyState.NOT_READY;
- /*to start a thread you have to give a new thread's constructor an object
- *of type "Runnable." Since Runnable is an interface you have to implement it
- * see the implementation class below
- */
- (new Thread(new Runner())).start();
- }
- /*This method is used by the event dispatcher thread when an event triggers
- * a window repaint
- *
- */
- @Override
- public void paint(Graphics g) {
- //Stuff that always happens first
- rocket.erase(g);
- //Stuff that might not happen
- switch (readystate) {
- case NOT_READY:
- LaunchPad.paint(g); //only draw launchpad at initial state
- break;
- case READY:
- rocket.moveRocketUp(OFFSET);//only move rocket when rocket is ready to be launched
- break;
- case COUNTING:
- takeoff.paint(g); //only draw take off text when countdown is still running
- break;
- }
- //Stuff that always happens last
- rocket.paint(g);
- }
- /*
- * This class contains a run method which is invoked upon creation of new threads
- * using objects from this class
- */
- class Runner implements Runnable {
- public void run() {
- /* Draw the rocket and launch pad */
- repaint();
- //Ask user if they would like to launch the rocket
- int input = JOptionPane.showConfirmDialog(null, "Would you like to launch the rocket?", "Entery Yes/No", JOptionPane.YES_NO_OPTION);
- //Exit if they say no
- if (input != 0)
- System.exit(0);
- //change ready state to counting so that paint method will draw take off text when called
- readystate = ReadyState.COUNTING;
- //loop until take off text's boolean method ready() returns false i.e. the countdown has reached "take off"
- while (!takeoff.ready()) {
- repaint(); //draw take off text
- try {
- //wait 1 second before looping or continuing
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- }
- }
- //change ready state to ready so that paint method will move the rocket when called
- readystate = ReadyState.READY;
- //loop until rocket is off the screen
- while(!rocket.isOffScreen()) {
- //paint the rocket at new location
- repaint();
- try {
- //wait 24 milliseconds before drawing next frame
- Thread.sleep(24);
- } catch (InterruptedException ex) {
- }
- }
- //rocket has completely launched
- //set rocket to original state (at launchpad)
- rocket = new Rocket(STARTPOSITION);
- //set take off text to original state (countdown at 5)
- takeoff = new TakeOffText();
- //set ready state to not_ready so that paint will draw rocket at current position without moving it
- readystate = ReadyState.NOT_READY;
- //start a new thread just like this one, but at the beginning
- (new Thread(new Runner())).start();
- //this thread ends.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement