Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 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. One example of a simple arrowhead-shaped rocket is shown here: Eclipse Project Setup 1. Create a Java application project called "RocketSim" in Eclipse. 2. Right-click on the project name in the Package Explorer and select Properties. In the dialog that appears, select "java Build Path", then the "Libraries" tab, then the "Classpath" item, and then choose "Add External JARs". In the file chooser that appears, navigate to the Blobz.jar file in the test folder you created on your desktop and select it. The contents of this Libraries tab should be similar to the Libraries tab image that was presented in the Program 3 assignment. 3. In the Package Explorer, select the "src" subfolder of the project, right-click, choose "New" and then "File". Enter "MANIFEST.MF" as the file name. Please note that this file name must be all upper case. 4. Edit the MANIFEST.MF file to appear exactly as follows: IMPORTANT NOTE: This manifest file assumes that you will develop your source files in the default package. However, if you choose to develop them within a named package, then the "Main-Class" attribute should use the fully-qualified name for your RocketTest class. For example, if your package is named "rocketsim", then the "Main-Class" attribute should read: "Main-Class: rocketsim.RocketTest" (with no quotation marks). Please also note that there are four (4) lines in this text file. The last line is empty. You will use this manifest file when you build your executable JAR file. Please also note that this manifest file is identical to the one you created for Program 3, except that main class should be the RocketTest class. Program Development 1. Develop a Rocket class that extends 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. You should design the Rocket class to satisfy the following requirements: A. The Rocket class constructor should take two input integer arguments, which correspond to the x- and y-coordinate values of your rocket's starting position in the sand box. Using those coordinate values, the first statement in your Rocket constructor should be: "super(x,y,0);" . This will create a stationary PolyBlob in the (x,y) location. B. Your rocket shape will be defined by the polygon you set using the setPolygon() method, just as you did for Program 3. 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). C. 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). D. 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; . E. 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. You do this by setting the initial value of angle to 0.0. F. The variable "angle" keeps track of the orientation of your rocket. It is initially set to zero (in step E, above), 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. Each time a key is press, you must determine what the new angle should be, and then you should update the rocket's orientation using the setAngle() method. G. The forward motion block of your keyAction() method should retrieve the current x and y coordinates of your rocket's location, then compute a new location using the "speed" configuration parameter and the current value of "angle" in the formulas described in the preview lecture. Once you compute the coordinates for the new location, you should update the location of the rocket by using the setLoc() method. 2. Develop a RocketTest class that implements the BlobGUI interface and satisfies the following requirements A. It should have a main() method that contains only the line "new RocketTest()". This is a call to the constructor for the class. B. 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. C. 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. D. The way to determine the location of the center of the sand box is as follows: (a) use your sandbox's "getPanelBounds()" instance method, which returns an object of type Dimension; (b) extract the width and height fields of this object; and (c) the center of the sand box will be the location whose x-coordinate is half the width value, and whose y-coordinate is half the height value. E. Once the rocket is instantiated, the generate() method should then add it to the sandbox using your sandbox's addBlob() method. 3. Run and debug your program in Eclipse until you are satisfied with it. 4. Export an executable JAR file to the same AsteroidsTest folder that you used for Program 3, as follows: Highlight the project (RocketSim) in the Package Explorer Choose File Export and select "JAR file" (not "Runnable JAR file"), then Next Choose the project (RocketSim) as the resource to export; check the box to "Export Java source files and resources"; and use the Browse button and text field to choose "RocketSim.jar" as the name you want for the JAR file and be sure that it will be placed inside the AsteroidsTest folder on your Desktop; then choose Next Make no choices on the "JAR Packaging Options" dialog and choose Next On the "JAR Export" dialog, do not choose a main class. Instead, check the box to "Use existing manifest from workspace" and use the "Browse" button to select the MANIFEST.MF file that you created in your RocketSim project. Choose "Finish" to complete the export of the JAR file. Your test folder should contain your JAR file at the top level. The folder will also likely contain the javadoc and lib subfolders. The "lib" subfolder should contain the Blobz.jar file. It is OK to leave your AsteroidField.jar from Program 3 also in the folder. Program Testing 1. Open a command window and navigate to the AsteroidsTest folder on your desktop. For most systems, you should be able to get there by entering: "cd Desktop\AsteroidsTest" (Windows) or "cd Desktop/AsteroidsTest" (Mac/Linux). 2. Now, run your program using the command: "java -jar RocketSim.jar". Your program should run and when you press the "Start New Game" button, your rocket should appear in the center of the sandbox and it should be pointing to the right. You should be able to move your rocket by pressing the right, left, and up arrow keys. If program does not work correctly, fix the problem and keep testing until you meet with success.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

What is intrinsic motivation? (p. 257)

Answered: 1 week ago

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago