Introduction

The goal of this project is to improve the display of a spinning wheel. An initial program is provided, and your task is to modify its behavior as described below.

Initial Zip File

You can start your project by downloading project2.zip. This file contains a slightly modified version of DrawingPanel.java, and this initial version of SpinningWheel.java.

public class SpinningWheel {
public static final int CENTER = 300;

public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(2 * CENTER, 2 * CENTER);
Graphics g = panel.getGraphics();
double angle = 0;
int sleepTime = 1000;
for (int j = 0; j <= 500; j++) {
drawWheel(g, angle);
panel.sleep(sleepTime);
sleepTime = 20;
angle += 2 * Math.PI / 500;
}
}

public static void drawWheel(Graphics g, double angle) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 2 * CENTER, 2 * CENTER);
g.setColor(Color.BLACK);
g.drawOval(50, 50, 2 * CENTER - 100, 2 * CENTER - 100);
for (int i = 0; i < 8; i++) {
drawSpoke(g, CENTER - 50, angle + i * 2 * Math.PI / 8);
}
}

public static void drawSpoke(Graphics g, int len, double a) {
g.drawLine(CENTER, CENTER,
(int) (CENTER + len * Math.sin(a)),
(int) (CENTER + len * Math.cos(a)));
}
}

The main method creates a DrawingPanel object, gets the Graphics object that is part of the DrawingPanel, and draws the wheel 501 times over 10 seconds at different angles. The drawWheel method first clears the window and then draws the wheel at the specified angle, The drawSpoke method draws a spoke of the wheel from the center to the rim at a specified angle.

You should study this program to understand how it works. In particular, the angle is specified in radians, where one degree = π/180 radians. For example, a right angle (90 degrees) is π/2 radians, and a full circle (360 degrees) is 2π radians.

Number of Rotations and Display of Degrees

Currently, the wheel rotates one time over 10 seconds. Change this so the wheel rotates two times over 12 seconds.

In addition, you should display the angle of the wheel in the window in degrees. For example, if degree is the angle in degrees, then code like:

g.setColor(Color.BLACK);
g.drawString((int) degree + " degrees", 30. 30);

would print this information in black in the upper left corner of the window. If you want to change the font size, use a statement like:

g.setFont("Serif", Font.BOLD, 24);

Of course, you need to add statements to convert angle, which is in radians, to degrees. Also, you should ensure that the number displayed is between 0 and 360. For example if degree is 413, then you should display 53.

Color Patterns

The user should be able to select between two color patterns. In each color pattern, the rim is one color, and the spokes are in two other colors, which switch from one spoke to the next. The same color cannot be used in both color patterns. For example, here is a wheel with a blue rim and red and green spokes. See image.

In the main method, the user should be asked to select a color pattern. For example, the user could enter a 1 or a 2 to select color pattern 1 or color pattern 2, respectively. If the user does not enter a valid input, the program should print a warning, and set the color pattern to a valid value.

The drawWheel method will need an additional parameter so it knows which color pattern to use. In addition, this method will need if statements to control which color is selected for the rim and the spokes.

Width of the Rim

The user should be able to enter the width of the rim.

In the main method, the user should be asked to enter the width of the wheel rim. This should be an integer between 1 and 10. If the user does not enter a valid input, the program should print a warning, and set the rim width to 5.

The drawWheel method will need an additional parameter so it knows the width of the rim. Printing a circle with a given rim width can be done by using fillOval twice. The first fillOval fills a bigger circle with the color of the rim. The second fillOval fills a smaller circle with white. Remember that the first two arguments are the upper left corner of the bounding box, not the center of the circle. Here is an example that draws a blue circle with radius 50 and rim width 5, centered at (150,150).

g.setColor(Color.BLUE);
g.fillOval(100,100,100,100);
g.setColor(Color.WHITE);
g.fillOval(105,105,90,90);

(Optional, no extra credit but might be fun to figure out) Draw the spokes using the same width. One way to do this is to use the Polygon class and the fillPolygon method of Graphics. Here is an example of drawing a rectangle of width about 5 from (100,100) to (200,200).

Polygon p = new Polygon();
p.addPoint(98,102);
p.addPoint(102,98);
p.addPoint(202,198);
p.addPoint(198,202);
g.fillPolygon(p);

All the coordinates need to be integers. You will need to calculate the values of the coordinates based on the angle of the spoke.

Examples of Behavior

Here is an example of user input when valid values are entered.

Enter the color pattern (1 or 2): 2
Enter the width of the rim: 7

Here is an example of user input when invalid values are entered:

Enter the color pattern (1 or 2): 3
3 is invalid, color pattern 1 will be used
Enter the width of the rim: -7
-7 is invalid, the width of the rim will be 5
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.