Introduction to Computer Science

Program 4 (rubric)

Create a Graphical Application to race Bugs. There will be 3 Bugs drawn on the screen. Each Bug will advance a random amount when requested to simulate a race. There will be no "finish line" and no winner detection or announcement. The intent here is to practice OO Design and Graphics.

You will develop a class for a Bug. The constructor will require initial coordinates and a color. The Bug will be displayed in the specified color, but the legs (your Bugs can have four to six legs) will always be black. Build your Bug out of Ellipses and Rectangles. A translate method will allow the x and y coordinates to be modified; in this application, only changes to x will be needed. A draw method will be used to draw the Bug on the screen.

You will also design a RaceTrack class to create a window in which the Bugs will race. The window should be divided into three horizontal lanes (using lines to separate the lanes. Create 3 Bug's for the race. One bug will be placed at the left of each lane. Each time the window is repainted, the Bugs will advance to the right a random amount. The paintComponent method must recalculate the lane positions and y coordinates of the Bugs in case the window is resized during play.

Your application will need to use a class named RaceController that will help to advance the bugs. This will be provided to you as a class file (no source code). This class should be used to instantiate a RaceController object that will display a button on the screen. Clicking the button will cause a repaint message to be sent to your racetrack, causing the Bugs to advance. You can see the documentation for this class here. Download the class file and place it in the folder with your source files. In jGrasp you need to extract the class file from the zip archive after downloading.

Eclipse instructions: Leave the class file inside the zip archive! Place this zip file in the folder with the other source files. Go to "Project Properties" and click on the "Java Build Path" option on the left. Click the button on the right "Add External JARs". Navigate to the project folder where you will see "RaceController.zip" (this is actually a JAR file). Select it an click Open. Click OK. Now all references to RaceController should be resolved.

Note: Sometimes the RaceController button appears behind the racetrack window... just move the RaceTrack over so you can find it.

You are required to create the following classes:

public class Bug: The Bug constructor needs an initial x and y position (int) and a color (Color). The draw method requires a Graphics2D object, and will draw the bug in that graphic context at its current coordinates. Your bugs should have a default size of approximately 40 pixels (height or width). Model this class after the Car class in the text. Include a translate method that requires two arguments, the amount to change the Bug's x and y coordinates. You should also provide a public setY method; this will be needed during the race.

public class RaceTrack: This is an extension of a JComponent. Model it after the CarComponent class in the text, except the Bugs will need to be instance variables, not locals. The constructor should instantiate the three Bugs that will be racing. You need to override paintComponent so it determines the positions of the lanes, the y-coordinates of the Bugs, and then draws the lane dividers and bugs. After drawing each Bug, translate it to the right by a random amount, say between 0 and 20 (make this a static constant in your Bug class and access it from the RaceTrack class to control the size of the random number). To determine the lane positions, you can get the height of the drawing window by request: this.getHeight() (this refers to the JComponent that is doing he drawing). This would be divided into three equal parts for the lanes. The bugs should be positioned appropriately between the lane markers using their setY methods.

public class RaceTrackViewer: This provides the Frame for the RaceTrack. Model it after the CarViewer class. This is where the RaceTrack and RaceController should be created. You should pass it your RaceTrack object so it can be repainted each time the button is clicked. Make the title on the window meaningful.

Submission instructions:

Be sure to follow the Documentation Standards (for this and every program). When you are ready to submit, obtain a printed copy of your program. Remember to sign and attach the required Academic Integrity Pledge cover sheet. The printed program must be turned in to your instructor by the start of class on the date the program is due. You must also email a zipped copy of the program to your instructor. Identify the email with the subject: ICS Program 4 and be sure that your name appears inside the message. The attached zip file must be named as your last name and the program number (example: smith_4.zip). The zip file must contain a folder having the same name (smith_4). The program source files and the required class file (RaceTrackController.class) should be inside this folder. Be sure to email your working solution before the due date! Do not submit non-working programs.

Email addresses: