Introduction For this program, you will implement an interface that will allow y
ID: 3799292 • Letter: I
Question
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 Blobz.jar external JAR file as for Program 3, 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.
Program Development
1. The Rocket class must extend the PolyBlob class and also implement the BlobAction interface. Both of these are in the Blobz.jar file and should be imported into your program from the blobz 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 use those values to initialize the coordinates of the Point[] array 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. 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.
7. The variable "angle" keeps track of the orientation of your rocket. It is initially set to zero, which represents pointing to the right as you look at the screen. Turning to the right will involve adding "delta" to the current angle. Turning to the left will involve subtracting delta from the current angle. You will need to add or subtract 2*PI as appropriate to keep the current angle within the range from 0 to 2*PI. Once you determine what the new angle should be, you should update the rocket's orientation using the setAngle() method.
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 using the setLoc() method.
9. 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.
10. 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.
11. 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.
Explanation / Answer
Asteroid.java
import blobzx.PolyBlob;
import blobzx.BlobUtils;
import java.util.Random;
import java.awt.Point;
import java.lang.Math;
import java.util.ArrayList;
public class Asteroid extends PolyBlob {
private static Random rand = new Random();
private int numSides;
private double region;
Point currentPoint;
ArrayList<Integer> polygonArrayX = new ArrayList<Integer>();
ArrayList<Integer> polygonArrayY = new ArrayList<Integer>();
public Asteroid(int x, int y, double r) {
// PolyBlob()
super(-100, -100, r);
// Blob.setDelta()
setDelta(x, y);
// Random number of sides, and set region
numSides = rand.nextInt(4) + 5;
region = 2 * (Math.PI) / numSides;
// Get location of each point
for(int i = 0; i < numSides; i++)
{
currentPoint = BlobUtils.rotatePoint( rand.nextInt(10) + 5, // Diameter between 10 and 30
(i * region) + (Math.random() * region ) // Point within region
);
polygonArrayX.add(currentPoint.x);
polygonArrayY.add(currentPoint.y);
}
//PolyBlob.setPolygon()
setPolygon(integersToInts(polygonArrayX), integersToInts(polygonArrayY));
}
//Converts an ArrayList of Integers to an Array of ints
public static int[] integersToInts(ArrayList<Integer> integers)
{
int[] ints = new int[integers.size()];
for (int i = 0; i < ints.length; i++)
ints[i] = integers.get(i).intValue();
return ints;
}
}
Rocket.java
import java.awt.event.KeyEvent;
import blobzx.BlobAction;
import blobzx.BlobProximity;
import blobzx.BlobUtils;
import blobzx.PolyBlob;
import blobzx.SandBox;
import java.awt.Point;
public class Rocket extends PolyBlob implements BlobAction, BlobProximity {
//Required from Assignment
private double angle = 0.0;
private final double delta = 0.15;
private final double speed = 5.0;
//My stuff
private final int size = 11;
private Point currentPoint, nextPoint, launchPoint;
private SandBox sb;
//Rocket Data, wide side is the front (think Batman)
private int[] referenceRocketXArray = {4, 4, 2, 4, 4, 0, -3, 0, -3, 0, 4};
private int[] referenceRocketYArray = {-10, -2, 0, 2, 10, 4, 2, 0, -2, -4, -10};
private int[] rocketXArray = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
private int[] rocketYArray = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
public Rocket(int x, int y, SandBox s) {
super(0, 0, 0);
sb = s;
//Set initial location and angle
setLoc(x, y);
turn(0, size);
//Create the rocket polygon
setPolygon(rocketXArray, rocketYArray);
}
@Override
public void keyAction(KeyEvent arg0) {
switch(arg0.getKeyCode())
{
case 37: //turn left
angle -= delta; //Angles are already modular, so can turn two full rotations.
turn(angle, 11);
break;
case 38: //boosters
currentPoint = getLoc();
nextPoint = BlobUtils.translatePoint( currentPoint.x,
currentPoint.y,
speed,
angle);
setLoc(nextPoint.x, nextPoint.y);
break;
case 39: //turn right
angle += delta; //Angles are already modular, so can turn two full rotations.
turn(angle, 11);
break;
case 32: //Spacebar
launch(sb);
BlobUtils.playSound();
break;
}
}
//Turns the rocket by angle "delta"
public void turn(double delta, int size)
{
//for each point
for(int i =0; i < size; i++)
{
//Get the rotated point
nextPoint = BlobUtils.rotatePoint( referenceRocketXArray[i],
referenceRocketYArray[i],
delta);
//Put the new point in the arrays
rocketXArray[i] = (int)nextPoint.getX();
rocketYArray[i] = (int)nextPoint.getY();
}
}
public void launch(SandBox sb)
{
//Get location of missile spawn
int launchDistance = (getSize()) / 2 + 5; // = 15
//Get Current location
currentPoint = getLoc();
//Rotate missile spawn to front of rocket
launchPoint = BlobUtils.rotatePoint(launchDistance, 0, angle);
//Create a Missile
Missile currentMissile = new Missile(currentPoint.x + launchPoint.x, currentPoint.y + launchPoint.y, angle);
//Add the Missile to the SandBox
sb.addBlob(currentMissile);
}
}
RocketTest.java
import blobzx.BlobGUI;
import blobzx.SandBox;
import blobzx.SandBoxMode;
import java.util.Random;
public class RocketTest implements BlobGUI{
private SandBox RocketTestSandBox;
private Random rand = new Random();
public static void main(String[] args) {
RocketTest testGame = new RocketTest();
}
public RocketTest() {
/*
(i) create a sandbox object;
(ii) set the sandbox to "flow" mode;
(iii) set the sandbox to run at 15 frames per second; and
(iv) Run the sandbox’s new init() method this way: <sandboxInstance>.init( this );
*/
RocketTestSandBox = new SandBox();
RocketTestSandBox.setSandBoxMode(SandBoxMode.FLOW);
RocketTestSandBox.setFrameRate(15);
RocketTestSandBox.init(this);
}
@Override
public void generate() {
//Create a rocket
Rocket testRocket = new Rocket(300,300, RocketTestSandBox);
//Add rocket to sandbox
RocketTestSandBox.addBlob(testRocket);
/*
(i) it must create 10 asteroids using the velocity components and rotational values described here;
(ii) it must randomly choose x and y velocity components for each asteroid, where the x and y components
are chosen independently of each other and where each of these values is an integer that may range
from -3 to +3, but where zero values are disallowed, all as discussed in lecture;
(iii) it must randomly choose a rotation value of either -.1 or +.1 for each asteroid, with equal
probability. Values in between -.1 and +.1 are not permitted; and
(iv) it must add each asteroid to the sandbox.
*/
int xVelocity = 0;
int yVelocity = 0;
int magnitude;
double rotation = .1;
//For each asteroid
for(int i = 0; i < 10; i++)
{
//For both vectors
for(int j = 0; j < 2; j++)
{
magnitude = rand.nextInt(2) + 1; //Get Random magnitude 0-3
if(rand.nextBoolean())
magnitude *= -1; //Point vector in random direction
if(j == 0)
yVelocity = magnitude; //Set y component vector
else
xVelocity = magnitude; //Set x component vector
}
if(rand.nextBoolean())
rotation *= -1; //Set rotation randomly
//Create an Asteroid
Asteroid currentAsteroid = new Asteroid(xVelocity, yVelocity,rotation);
//Add the Asteroid to the SandBox
RocketTestSandBox.addBlob(currentAsteroid);
}
}
}
Missile.java
import blobzx.Blob;
import blobzx.BlobProximity;
import java.lang.Math;
public class Missile extends Blob implements BlobProximity {
private double speed = 5;
public Missile(int x, int y, double r) {
super(x, y);
//Calculate Missile velocity components
int dx = (int) Math.round(speed * Math.cos(r) );
int dy = (int) Math.round(speed * Math.sin(r) );
// Blob.setDelta()
setDelta(dx, dy);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.