Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Object-Oriented Design 
COMP 2000 
Spring 2022 
Lab 8 Supplement 1 
 
First, some modifications to the starter code you were provided: 
• It might be helpful for you to add a toString() method to class Point to make debugging 
easier. 
• In the Asteroids class replace the single line in main() that instantiates an Asteroids object 
with the following two lines of code: 
Asteroids a = new Asteroids(); 
a.repaint(); 
• In the constructor for Asteroids, add the following two lines after the call to super(): 
this.setFocusable(true); 
this.requestFocus(); 
 
Now for some early guidance: 
• You should look at the code in class Game and try to understand what it is doing as best you can. 
Ask questions! However, you should NOT edit this class in any way. 
• Asteroids is a subclass of Game. You will be adding instance (state) variables, modifying its 
constructor and paint() method, and possibly adding additional utility methods. 
• The main() method calls a.repaint(), which calls the method a.paint(...). There are 
some details about how a.repaint() calls a.paint() which could be important for debugging, 
so they are explained in the next paragraph, but at a high level, you can think of what is happening 
in the code as: 
Asteroids a = new Asteroids(); 
a.paint(...) 
a.paint(...) 
a.paint(...) 
a.paint(...) 
. 
. 
. 
• This paragraph will explain the details of what is actually happening when the main method calls 
a.repaint(), which is a method defined by Java in the class Component, which is the 
superclass of Canvas (which is the superclass of Game). The method a.repaint() calls the 
method a.update() over and over again. The method update() is defined in the superclass 
Game (overriding the original definition in Component), and it first calls the method 
a.paint(...), which you will fill in in Asteroids. An instance of Graphics is passed into 
a.paint(...), but this is not the instance that holds the game Canvas. Instead, this instance 
of Graphics acts as a buffer (and hence is called buffer). So when your code in a.paint(...) 
draws something, it is actually drawing it on this buffer Graphics instance. Everything in the 
buffer is then drawn to the screen all at once, making the animation less choppy. 
Now we are ready to outline the first steps: 
• The first thing you should do is create the class Ship that is a subclass of Polygon. 
o It is ok to import java.awt.event.*, but do not import java.awt.* (instead import 
each class from java.awt that you need individually as you need it). 
o You will need a constructor that (minimally) has the same signature as Polygon’s 
constructor. It should call super() as its first instruction. 
o You will also need a paint() method that has the same signature as paint in Asteroids. 
This is the method that will display the ship on the canvas. 
o Before you draw the ship, make sure the paint method is set up correctly by using the 
drawLine method in Graphics to draw a single line between the coordinates (100,200) 
and  (300,400). To do this, type brush.drawLine(100,200,300,400); in the paint 
method in Ship. Once you see that it works you can comment out this line. 
o Drawing the ship will require three steps. First you will have to call getPoints() to 
retrieve the array of points that make up the ship’s polygon. You will then need to convert 
that array of points into a the right format so that you can call brush.fillPolygon(). 
Finally you will call setColor() and fillPolygon() on the brush to draw the ship. 
• In order to test your ship class you will need to modify Asteroids to act as a client of Ship. 
o Declare a Ship instance variable. 
o In the constructor for Asteroid, build a ship object referenced by this variable. This will 
require you to instantiate and populate an array of points with the coordinates that you 
want to use to represent the shape of the ship. The initial position of the ship should be 
centered on the screen (so a point with coordinates (400, 300)), and should be oriented 
facing east. 
o In the paint() method for Asteroid, call the paint() method on the ship. 
 
All work must be done individually. Never look at someone else's code. Please refer 
to the course policies if you have any questions about academic integrity. If you have 
trouble with the assignment, I am always available for assistance.