Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.93 KB | None | 0 0
  1.  
  2. /**
  3.  * Class Rocket -
  4.  * This class causes a representation of a rocket to be created in the Shapes
  5.  * Window, and for it to move up the screen
  6.  *
  7.  * @author (M250 Course Team)
  8.  * @version (2.0)
  9.  */
  10. import ou.*;
  11. public class Rocket
  12. {  
  13.    private Triangle capsule;      // represents the rocket's capsule
  14.    private Square stage3;         // represents the top section of the rocket's fuselage
  15.    private Square stage2;         // represents the middle section of the rocket's fuselage
  16.    private Square stage1;         // represents the bottom section of the rocket's fuselage
  17.    private Triangle fin1;         // represents the rocket's left fin
  18.    private Triangle fin2;         // represents the rocket's right fin
  19.    private Circle jet1;           // represents the blast from the rocket's left engine
  20.    private Circle jet2;           // represents the blast from the rocket's right engine
  21.  
  22.    
  23.    
  24.    /**
  25.     * Constructor for objects of class Rocket
  26.     */
  27.    public Rocket(Triangle c, Square s1, Square s2, Square s3, Triangle f1, Triangle f2, Circle j1, Circle j2)
  28.    {
  29.       super();
  30.       this.capsule = c;
  31.       this.stage1 = s1;
  32.       this.stage2 = s2;
  33.       this.stage3 = s3;
  34.       this.fin1 = f1;
  35.       this.fin2 = f2;
  36.       this.jet1 = j1;
  37.       this.jet2 = j2;
  38.       this.makeRocket(50,260);
  39.    }
  40.    
  41.    /**
  42.     * Moves the capsule of the receiver to  
  43.     * its initial position.
  44.     */
  45.    private void setInitialPosCapsule(int anXPos, int aYPos)
  46.    {
  47.       this.getCapsule().setXPos(anXPos);
  48.       this.getCapsule().setYPos(aYPos);  
  49.    }
  50.    
  51.    /**
  52.     * Moves the stage3 of the receiver Rocket object to
  53.     * its position underneath the capsule.
  54.     */
  55.    private void setPosStage3()
  56.    {
  57.       this.getStage3().setYPos(this.getCapsule().getYPos() + 20);
  58.       this.getStage3().setXPos(this.getCapsule().getXPos());
  59.    }
  60.    
  61.    
  62.    
  63.    /**
  64.     * Moves the stage2 of the receiver Rocket object to
  65.     * its position underneath the stage3.
  66.     */
  67.    private void setPosStage2()
  68.    {
  69.       this.getStage2().setYPos(this.getStage3().getYPos() + 20);
  70.       this.getStage2().setXPos(this.getCapsule().getXPos());
  71.    }
  72.    
  73.    
  74.    /**
  75.     * Moves the stage1 of the receiver Rocket object to
  76.     * its position underneath the stage2.
  77.     */  
  78.    private void setPosStage1()
  79.    {
  80.       this.getStage1().setYPos(this.getStage2().getYPos() + 20);
  81.       this.getStage1().setXPos(this.getCapsule().getXPos());  
  82.    }
  83.    
  84.  
  85.    
  86.    /**
  87.     * Moves the left fin of the receiver Rocket object to
  88.     * its position alongside the stage1.
  89.     */  
  90.    private void setPosFin1()
  91.    {
  92.       this.getFin1().setYPos(this.getStage1().getYPos());
  93.       this.getFin1().setXPos(this.getCapsule().getXPos() - 10);
  94.    }
  95.    
  96.    
  97.    /**
  98.     * Moves the right fin of the receiver Rocket object to
  99.     * its position alongside the stage1.
  100.     */  
  101.    private void setPosFin2()
  102.    {
  103.       this.getFin2().setYPos(this.getStage1().getYPos());
  104.       this.getFin2().setXPos(this.getCapsule().getXPos() + 10);
  105.    }
  106.    
  107.    
  108.    /**
  109.     * Moves the left jet of the receiver Rocket object to
  110.     * its position underneath the stage1 and sets its diameter
  111.     * to 4 and its colour to black.
  112.     */
  113.    private void setPosJet1()
  114.    {
  115.    this.jet1.setDiameter(4);
  116.    this.jet1.setColour(OUColour.BLACK);
  117.    this.jet1.setYPos(this.stage1.getYPos() + 20);
  118.    this.jet1.setXPos(this.stage1.getXPos() - 2);  
  119.    }
  120.    
  121.  
  122.    /**
  123.     * Moves the right jet of the receiver Rocket object to
  124.     * its position underneath the stage1 and sets its diameter
  125.     * to 4 and its colour to black.
  126.     */  
  127.    private void setPosJet2()
  128.    {
  129.    this.jet2.setDiameter(4);
  130.    this.jet2.setColour(OUColour.BLACK);
  131.    this.jet2.setYPos(this.stage1.getYPos() + 20);
  132.    this.jet2.setXPos(this.stage1.getXPos() + 17);  
  133.      
  134.    }    
  135.    
  136.    
  137.  
  138.    /**
  139.     * Returns the Triangle object representing the capsule of the receiver Rocket object
  140.     */  
  141.    private Triangle getCapsule()
  142.    {
  143.       return this.capsule;
  144.    }
  145.    
  146.    
  147.    /**
  148.     * Returns the Square object representing the stage1 of the receiver Rocket object
  149.     */  
  150.    private Square getStage1()
  151.    {
  152.       return this.stage1;
  153.    }
  154.    
  155.    
  156.    /**
  157.     * Returns the Square object representing the stage2 of the receiver Rocket object
  158.     */  
  159.    private Square getStage2()
  160.    {
  161.       return this.stage2;
  162.    }
  163.    
  164.      
  165.    
  166.    /**
  167.     * Returns the Square object representing the stage3 of the receiver Rocket object
  168.     */  
  169.    private Square getStage3()
  170.    {
  171.       return this.stage3;
  172.    }
  173.    
  174.    
  175.    /**
  176.     * Returns the Triangle object representing the left fin of the receiver Rocket object
  177.     */
  178.    private Triangle getFin1()
  179.    {
  180.       return this.fin1;
  181.    }
  182.    
  183.    
  184.    /**
  185.     * Returns the Triangle object representing the right fin of the receiver Rocket object
  186.     */
  187.    private Triangle getFin2()
  188.    {
  189.       return this.fin2;
  190.    }
  191.    
  192.    
  193.    
  194.    /**
  195.     * Returns the Circle object representing the left jet of the receiver Rocket object
  196.     */
  197.    private Circle getJet1()
  198.    {
  199.       return this.jet1;
  200.    }
  201.    
  202.    
  203.    /**
  204.     * Returns the Circle object representing the right jet of the receiver Rocket object
  205.     */
  206.    private Circle getJet2()
  207.    {
  208.       return this.jet2;
  209.    }
  210.  
  211.    
  212.  
  213.    /**
  214.     * Moves all the component parts of the rocket into their initial positions
  215.     */
  216.    private void makeRocket(int anXPos, int aYPos)
  217.    {
  218.      
  219.        this.setInitialPosCapsule(anXPos, aYPos);
  220.        this.setPosStage3();
  221.        this.setPosStage2();
  222.        this.setPosStage1();
  223.        this.setPosFin1();
  224.        this.setPosFin2();
  225.        this.setPosJet1();
  226.        this.setPosJet2();
  227.                  
  228.    }
  229.    
  230.    
  231.    /**
  232.     * Moves the capsule of the receiver and the jets
  233.     * by anInt units.
  234.     * Moves other rocket components relative to the
  235.     * position of the capsule.
  236.     */
  237.    private void moveRocketBy(int anInt)
  238.    {
  239.  
  240.       this.getCapsule().setYPos(this.getCapsule().getYPos() - (anInt));
  241.        this.setPosStage3();
  242.        this.setPosStage2();
  243.        this.setPosStage1();
  244.        this.setPosFin1();
  245.        this.setPosFin2();
  246.        this.setPosJet1();
  247.        this.setPosJet2();
  248.        
  249.    }
  250.  
  251.    
  252.    
  253.    
  254.    /**
  255.     * Sets the diameters of the receiver's jets to
  256.     * 6 and sets their colour to red.
  257.     */
  258.    private void pulse1()
  259.    {
  260.    this.jet1.setDiameter(6);
  261.    this.jet2.setDiameter(6);
  262.    this.jet1.setColour(OUColour.RED);
  263.    this.jet2.setColour(OUColour.RED);  
  264.    }
  265.    
  266.    
  267.    /**
  268.     * Sets the diameters of the receiver's jets to
  269.     * 12, decrements their xPos by 3 and sets
  270.     * their colour to orange.
  271.     */      
  272.    private void pulse2()
  273.    {
  274.     this.jet1.setColour(OUColour.ORANGE);
  275.     this.jet2.setColour(OUColour.ORANGE);
  276.     this.jet1.setDiameter(12);
  277.     this.jet2.setDiameter(12);
  278.     this.jet1.setXPos(this.jet1.getXPos() - 3);
  279.     this.jet2.setXPos(this.jet2.getXPos() - 3);
  280.      
  281.    }
  282.    
  283.  
  284.    /**
  285.     * Sets the diameters of the receiver's jets to
  286.     * 24, decrements their xPos by 6 and sets
  287.     * their colour to red.
  288.     */    
  289.    private void pulse3()
  290.    {
  291.     this.jet1.setColour(OUColour.RED);
  292.     this.jet2.setColour(OUColour.RED);
  293.     this.jet1.setDiameter(24);
  294.     this.jet2.setDiameter(24);
  295.     this.jet1.setXPos(this.jet1.getXPos() - 6);
  296.     this.jet2.setXPos(this.jet2.getXPos() - 6);
  297.      
  298.    }
  299.      
  300.    
  301.    /**
  302.     * Simulates the ignition of the rocket's jets
  303.     */
  304.    private void ignition()
  305.    {
  306.    
  307.       for(int i = 0; i < 5; i++)
  308.       {      
  309.          this.pulse1();
  310.          this.delay(200);
  311.          this.pulse2();
  312.          this.delay(200);
  313.          this.pulse3();
  314.          this.delay(200);  
  315.          this.setPosJet1();
  316.          this.setPosJet2();
  317.       }
  318.        
  319.    }
  320.  
  321.  
  322.  
  323.    /**
  324.     * Moves the entire rocket numberOfUnits, one unit at a time
  325.     */
  326.    private void animateRocket(int numberOfUnits)
  327.    {
  328.      for(int i = 0; i < 15; i++)
  329.      {
  330.         ignition();
  331.         if (i % 3 == 0)
  332.         {
  333.            moveRocketBy(numberOfUnits);  
  334.         }
  335.      }    
  336.    }
  337.      
  338.    
  339.    /**
  340.     * Prompts the user to enter the number of units they want the  
  341.     * rocket to move upwards.
  342.     *
  343.     * If the number the units provided would cause the tip of the  
  344.     * capsule to go past the top of the Graphical Display, the user
  345.     * is informed via a dialogue box that the rocket will not launch.  
  346.     *
  347.     * Otherwise the rocket launches as required.
  348.     */
  349.    public void launch()
  350.    {
  351.      this.ignition();
  352.      this.animateRocket(50);
  353.        
  354.    }
  355.  
  356.      
  357.    
  358.    
  359.    
  360.    
  361.    
  362.    /**
  363.     * Causes execution to pause by time number of milliseconds
  364.     */
  365.    public void delay(int time)
  366.    {
  367.       try
  368.       {
  369.          Thread.sleep(time);
  370.       }
  371.       catch (Exception e)
  372.       {
  373.          System.out.println(e);
  374.       }
  375.    }
  376.    
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement