Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. Rocket Class: Keyboard Event Handling
  2. Introduction
  3. 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:
  4.  
  5. rocket.png
  6.  
  7. Eclipse Project Setup
  8. 1. Create a Java application project called "RocketSim" in Eclipse.
  9.  
  10. 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.
  11.  
  12. 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.
  13.  
  14. 4. Edit the MANIFEST.MF file to appear exactly as follows:
  15.  
  16. rocketmanifest.png
  17.  
  18. 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).
  19.  
  20. 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.
  21.  
  22. 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.
  23.  
  24. Program Development
  25. 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:
  26.  
  27. 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.
  28.  
  29. 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).
  30.  
  31. 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).
  32.  
  33. 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; .
  34.  
  35. 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.
  36.  
  37. 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.
  38.  
  39. 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.
  40.  
  41. 2. Develop a RocketTest class that implements the BlobGUI interface and satisfies the following requirements
  42.  
  43. A. It should have a main() method that contains only the line "new RocketTest()". This is a call to the constructor for the class.
  44.  
  45. 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.
  46.  
  47. 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.
  48.  
  49. 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.
  50.  
  51. E. Once the rocket is instantiated, the generate() method should then add it to the sandbox using your sandbox's addBlob() method.
  52.  
  53. 3. Run and debug your program in Eclipse until you are satisfied with it.
  54.  
  55. 4. Export an executable JAR file to the same AsteroidsTest folder that you used for Program 3, as follows:
  56.  
  57. Highlight the project (RocketSim) in the Package Explorer
  58. Choose File Export and select "JAR file" (not "Runnable JAR file"), then Next
  59. 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
  60. Make no choices on the "JAR Packaging Options" dialog and choose Next
  61. 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.
  62. 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.
  63. Program Testing
  64. 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).
  65. 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.
  66. What to Submit
  67. For this assignment, you must submit an executable JAR file that contains your Java source files (".java" extension) as well as the bytecode files (".class") for running the program.
  68.  
  69. You should test your JAR file from the command line as described above before submitting it, as also discussed in lecture and in the Programming Resources section of this Webcourse.
  70.  
  71. Grading
  72. Your program will be graded according to the grading rubric below. Additional penalties will be applied as necessary for:
  73.  
  74. late submissions
  75. failure to include the source files
  76. failure to include proper source file headers
  77. failure for one teammate to submit
  78. teaming submission errors
  79. The penalties for the above items are described in the course Syllabus.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement