Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help to develop the code under the part below titled program development.

ID: 3672429 • Letter: I

Question

I need help to develop the code under the part below titled program development. I have provided the detail to the overall program below:

Rocket Class: Keyboard Event Handling

Introduction

For this program, you will implement an interface that will allow you to move around a rocket of your own design. You will need to implement your own classes for the rocket and a test driver, but you will also be able to use the same BlobZX.jar external JAR file as for Program 4, which contains useful utilities for creating a graphics context and for supporting your calculations. The only output of the program should be your rocket displayed in the sandbox, and you should be able to move it around using the up, left, and right arrow keys on your keyboard. One example of a simple rocket is shown here:

NetBeans Project Setup

1. Create a Java application project called "RocketSim" in NetBeans. Edit the "packaging" properties so that the source (.java) files will be included in the JAR file.

2. Using the "Files" tab in NetBeans, look at your "src" folder. If it is empty, create a new Java package called "rocketsim". If NetBeans already created that package for you, then skip this step.

3. Now, create two separate Java class files inside the rocketsim package. The first file should be called Rocket.java. This will hold your Rocket class. The second file should be called RocketTest.java. This will be the test driver class. There should be a main() method in the RocketTest class, but not in the Rocket class. If NetBeans automatically created a main() method in the Rocket class for you, you should delete it.

4. In the Project Properties for your project, select the "Libraries" category. Then, press the "Add JAR/Folder" button. A dialog will appear where you will be able to find and select the BlobZX.jar file on your system. Select it. You should now see that BlobZX.jar is included in the "Compile-time Libraries" window on the dialog. Select "OK". Your project is now set up to use BlobZX.jar.

Program Development

1. The Rocket class must extend the PolyBlob class and also implement the BlobAction interface. Both of these are in the BlobZX JAR file and should be imported into your program from the blobzx package.

2. The first statement in your Rocket constructor should be: "super(0,0,0);" This will create a stationary PolyBlob in the upper left corner of the sandbox. Then, use the Rocket's setLoc() method, which is inherited from Blob, to set the location to the input location specified by the input parameters to the constructor. The constructor should have 3 input parameters: an int x-coordinate, an int y-coordinate, and the sandbox that your RocketTest test driver instantiated.

3. Your rocket shape will be defined by the polygon you set using the setPolygon() method. You can create your shape on paper and then transfer the coordinates to the x[] and y[] arrays that you will pass to setPolygon(). Remember, the coordinates are relative to the origin, which is point (0,0).

4. Since the Rocket class implements the BlobAction interface, the Rocket class must have a public void keyAction(KeyEvent e) method. This method should have separate processing blocks for handling key codes 37 (left arrow), 38 (up arrow), and 39 (right arrow).

5. The Rocket class should also have these instance variables: private double angle = 0.0; private final double delta = 0.15; and private final double speed = 5.0; .

6. You should use two sets of polygon arrays for implementing the turns. Use slightly different names for the two sets of arrays so you can tell them apart. One pair is the "reference" set. This is the set you use to compute angles. The "turn()" method should rotate each x and y pair in the reference set to new values, which you should store in the OTHER set of x[] and y[] arrays. This OTHER set of arrays should be the set that you passed to the setPolygon() method in your constructor. Please remember that you should NOT ever change any value in the reference set. To perform the rotation for a given set of x and y coordinates, you should use the BlobUtils.rotatePoint() method that takes three parameters (the x and y values, plus the final angle value in radians).

7. The rocket shape that you specify can have as many vertices as you wish. However, no value in the polygon arrays can be less than -10 or greater than +10. Also, when your rocket is first placed in the sandbox, it should be oriented so that the direction of forward motion is to the right as one looks at the screen. This is the direction that should correspond to the initial value of angle = 0.0.

8. The forward motion block of your keyAction() method should retrieve the current x and y coordinates of your rocket's location, then adjust the locations using the "speed" configuration parameter and the current value of "angle" (which was set to 0.0 initially in the rocket class constructor), and finally should update the location of the rocket.

9. The left and right rotation blocks of the keyAction() method should adjust "angle" by adding or subtracting "delta", and adjusting for values less than zero or greater than 2*Math.PI. The rotation blocks of code should then call a new "turn()" method that you must develop and which should rotate your Rocket to align it with the new value of "angle".

10. The RocketTest class must implement the BlobGUI interface and should have a main() method that contains only the line "new RocketTest()". This is a call to the constructor for the class.

11. The constructor for the RocketTest class should take no input parameters, but it should perform the following actions: (a) create a sandbox; (b) set the sandbox to be in "flow" mode; (c) set the frame rate to 15 frames per second; and (d) pass a reference to itself to the sandbox by running the sandbox's init() method, for example, "sb.init(this)", where "sb" represents the sandbox instance you have created.

12. The RocketTest class should also have a generate() method, which it is required to have since it implements the BlobGUI interface. This generate method will be called every time the user presses the "Start New Game" button on the GUI. The generate() method should instantiate a new Rocket at the location that is at the center of the sandbox. There are several ways to do this. One way to do this is to hard-code the location of the center knowing that the sandbox is 600 x 600 pixels in size. Also, don't forget that the Rocket constructor should have three input parameters, as described in step 2 above in this section. Once the rocket is instantiated, the generate() method should then add it to the sandbox using your sandbox's addBlob() method.

Program Testing

1. Create a folder called "prog5" on your desktop and put a copy of your RocketSim.jar executable JAR file in this folder.

2. Also create a folder called "lib" inside the prog5 folder and put a copy of the BlobZX.jar file into the lib folder.

3. Open a command window and navigate to the prog5 folder on your desktop. For most systems, you should be able to get there by entering: "cd Desktopprog5" (Windows) or "cd Desktop/prog5" (Mac/Linux).

4. Now, run your program using the command: "java -jar RocketSim.jar". Your program should run. If it does not, fix the problem and keep testing until you meet with success.

What to Submit

1. Submit on Webcourses ONLY the RocketSim.jar file that you have successfully tested from the command line, as described above. Do not submit the lib folder or BlobZX.jar.

2. If you make improvements to your program, you may submit again as many times as you like, provided the submissions are prior to the deadline for this assignment. Please note that Webcourses will add a suffix number to the file name for the second and later submissions, but this is not a problem. The graders will know how to handle this situation.

3. Your JAR file must contain the Rocket.java and RocketTest.java source files. Both of these files must contain a file header in the following format, so that we know it is your code.

Explanation / Answer

Rocket.java

import blobmx.PolyBlob;

import java.awt.event.KeyEvent;
import blobmx.BlobAction;
import blobmx.BlobProximity;
import blobmx.BlobUtils;
import java.awt.*;
import blobmx.SandBox;
import blobmx.SandBoxMode;


public class Rocket extends PolyBlob implements BlobAction, BlobProximity {
  
    private final int [] x = {10,-10,-5,-10};
    private final int [] y = {0,-7,0,7};
  
  
    private double angle = 0.0;
    private final double delta = 0.15;
    private final double speed = 5.0;
  
    SandBox mySandBox;
  

    public Rocket(SandBox thesandbox) {
        super(300,300,0);
     
        mySandBox=thesandbox;
      
        setPolygon(x, y);
  
  
    }
  
    public void keyAction(KeyEvent e){
      
        int i=0;
      
      
        if (e.getKeyCode()==32){//SPACE
          
            launch(mySandBox);
            BlobUtils.playSound();

        }
      
      
        if (e.getKeyCode()==37){//LEFT
          
           angle = angle-delta;
           if (angle > (2*Math.PI)){
               angle = angle - (2 * Math.PI);
           }
         
           turn (angle,x,y);
          
        }
      
      
        if (e.getKeyCode()==38){ //MOVE FORWARD
          
            int xloc = getLoc().x;
            int yloc = getLoc().y;
            setLoc(xloc+ (int)Math.round(speed * Math.cos(angle)), yloc + (int)Math.round(speed * Math.sin(angle)) );
          
        }
      
      
      
        if (e.getKeyCode()==39){ //RIGHT
          
          
          
            angle = angle+delta;
           if (angle > (2*Math.PI)){
               angle = angle + (2 * Math.PI);
           }
         
           turn (angle,x,y);
          
          
          
        }
      
      
      
    }
  
  
    public void turn (double angle, int[] x , int []y){
      
        int [] xr = {10,-10,-5,-10};
        int [] yr = {0,-7,0,7};
      
        for (int i=0; i <x.length; i++){
          
            x[i]=(int) BlobUtils.rotatePoint(xr[i],yr[i],angle).getX();
            y[i]=(int) BlobUtils.rotatePoint(xr[i],yr[i],angle).getY();
          
        }
      
      
      
    }
  
  
    public void launch (SandBox sb){
      
        Missle themissle = new Missle(getLoc().x,getLoc().y,angle);
        mySandBox.addBlob(themissle);
      
    }
  
  
}

Asteroid.java

import blobmx.PolyBlob;


public class Asteroid extends PolyBlob {

    public Asteroid(int x, int y, double r, int [] xs, int [] ys) {
        super(-100, -100, r);
        setDelta(x,y);
  
      
        this.setPolygon(xs, ys);
  
    }
  
}


AsteroidGame.java

import java.util.Scanner;
import blobmx.Blob;
import blobmx.BlobPanel;
import blobmx.SandBox;
import blobmx.SandBoxMode;
import blobmx.BlobUtils;
import java.awt.event.*;
import java.awt.*;
import blobmx.BlobGUI;


public class AsteroidGame implements BlobGUI {
  
    SandBox mySandBox = new SandBox();
  

    public static void main(String args[]) {
      
        AsteroidGame thegame = new AsteroidGame();
      
    }
      
    AsteroidGame(){
      
      
      
        mySandBox.setSandBoxMode(SandBoxMode.FLOW);
        mySandBox.setFrameRate(66);
        mySandBox.init(this);
      
        }
      
    @Override
    public void generate(){
      
      
        Rocket myrocket = new Rocket(mySandBox);
      
      
        Asteroid[] theasteroids = new Asteroid[10];
        int i;
        int j;
      
      
        for (i=0;i<10;i++){
      
        
           int mysides =randoms.random_sides();
           int [] distances = new int [mysides];
           double [] angles = new double [distances.length];
           Point []mypoints = new Point [mysides];
      
         
           int []x= new int [mysides];
           int []y= new int [mysides];
         
         
           for (j=0;j<mysides;j++){
             
               distances[j]=randoms.random_distance();
             
           }
         
           double region = (2*Math.PI)/(double)mysides;
         
           for (j=0;j<angles.length;j++){
             
               angles [j] = (j*region)+(Math.random()*region);
           }
         
           for (j=0;j<mypoints.length;j++){
         
            mypoints[j] = BlobUtils.rotatePoint(distances[j], angles[j]);
           }
         
           for (j=0;j<mypoints.length;j++){
         
            x[j] = mypoints[j].x;
            y[j] = mypoints[j].y;
           }
         
         
           theasteroids[i]=new Asteroid(randoms.randomxy(),randoms.randomxy(),randoms.random_rotation(), x, y);
         
           mySandBox.addBlob(theasteroids[i]);
         
        
        }
        mySandBox.addBlob(myrocket);
      
    }
  
}


Missle.java
import blobmx.Blob;
import blobmx.BlobProximity;


public class Missle extends Blob implements BlobProximity {
  
    private final double angle;
    private final int size = 5;
    private final int speed = 5;
  
    public Missle (int x, int y, double angle){
        super(x,y);
      
        setSize(size);
      
        this.angle=angle;
        int dx=(int) Math.round( speed * Math.cos(angle));
        int dy=(int) Math.round( speed * Math.sin(angle));
        setDelta(dx, dy);
      
        int xloc = getLoc().x;
        int yloc = getLoc().y;
        setLoc(xloc+ (int)Math.round(speed * Math.cos(angle)), yloc + (int)Math.round(speed * Math.sin(angle)) );
      
      
    }
  
}


randoms.java


import java.util.Random;
public class randoms {
  
    public void main (){
      

    }
  
  
    public static double random_rotation(){
      
      
       double[] rotnum = new double[2];
       rotnum[0]=-0.1;
       rotnum[1]=0.1;
       Random randomGenerator = new Random();
       int randomInt = randomGenerator.nextInt(2);
       randomInt=randomInt;
       return rotnum[randomInt];
      
    }
  
  
    public static int randomxy(){
      
       Random randomGenerator = new Random();
       int randomInt = randomGenerator.nextInt(4);
       randomInt=randomInt-3;
       if (randomInt==0){
           randomInt=randomInt+1;
       }
       return randomInt;
      
    }
  
  
    public static int random_sides(){
      
       Random randomGenerator = new Random();
       int randomInt = randomGenerator.nextInt(5);
       randomInt=randomInt+5;
       return randomInt;
      
    }
  
  
  
     public static int random_distance(){
      
       Random randomGenerator = new Random();
       int randomInt = randomGenerator.nextInt(11);
       randomInt=randomInt+5;
       return randomInt;
      
    }
  
  
}

  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote