Skills to be Applied:

Swing/AWT,
Animation/Multi-Threads
Classes may be needed:
Timer in javax.swing package, JApplet, JButton, Container, JPanel, Color, Graphics, JSlider, JLabel, JColorChooser, ActionListener, ActionEvent, ChangeListener, ChangeEvent. You may use other classes.

Program Description

Suggested Class Diagram: see image.

Write a Java program that constructs an Applet.

The Applet (JApplet) of your program should contain three buttons trajectories, "Start", "Clear", and "Color". These three buttons will be organized vertically. Next to each set of buttons, there will be a label "Initial Velocity", then below it, there will be a slider that a user can use to change the initial velocity of trajectories. Below the slider for the initial velocity, there will be another label "Initial Angle", and below it, there will be a slider that a user can use to change the initial angle for trajectories (Note that the angle is measured from 3 oclock location to counter clockwise direction). Next to the buttons and sliders, there are panels, each panel showing trajectories with their initial color. The top trajectories should be red and the bottom one should be blue initially. The picture below shows a snap shot of trajectories. Trajectories consist of multiple filled circles, and its background is black. see image.

When "Clear" button is pushed, trajectories in the corresponding panel should be erased. When "Start" button is pushed, a new trajectory should start drawing using the initial velocity, initial angle, and the color selected at that time from the left hand side of the panel. Pushing the "Color" button opens up a color chooser. Note that a pop-up blocker in your machine might need to be disable for this color chooser to pop up.

By moving each slider, a user should be able to change the initial velocity and initial angle. Note that these two sets of trajectories can have a completely independent movement of each other. see image.

(The size of the applet here is approximately 800 X 340). see image.

You need to create the following classes.

Dot class

The Dot class represents a dot to be drawn on a panel. A collection of dots will draw a trajectory. It has the following attributes:

Attribute name, Attribute type, Description
color, Color color of the dot.
x, int, x coordinate of the center of the dot
y, int, y coordinate of the center of the dot
RADIUS, int, the radius of the dot. It is 3.

The following constructor method should be provided:

public Dot(int x1, int y1, Color color1)

The color is initialized to the value of the color parameter. The x and y coordinates are also initialized using their corresponding parameter values.

public void draw(Graphics page)

It should draw a filled dot (circle) using its color, x, y coordinate, and its RADIUS.

TrajectoryPanel class

TrajectoryPanel class a subclass of JPanel class. It is used to define a panel where trajectories are moving. It has the following attributes:

Attribute name, Attribute type, Description
color, Color, color of the trajectories.
timer, Timer, An object of Timer in javax.swing package. This is used to control the movement of the trajectories.
delay, int, delay of the timer.
velocity, double, initial velocity of a trajectory to be drawn
angle, int, initial angle of a trajectory to be drawn (it is measured from 3 oclock position to upward)
initialY, int, initial y coordinate of a trajectory to be drawn.
time, double, time after a trajectory started to be drawn. It is 0.0 initially.
step, double, each step that time changes for each timer tick
ptList, ArrayList, It stores dots to draw a trajectory
GRAVITY, final double, The gravity 9.8 m/s^2

The following constructor method should be provided:

public TrajectoryPanel(Color color)

The color is initialized to the value of the color parameter. The delay should be initialized to 50, and the step should be initialized to delay*50.0, time should be initialize 0.0, initialY should be initialized to 50, angle should be initialized to 45, velocity should be initialized to 50.0. The background should be set to black color. The timer should be instantiated with "delay" and the listener object created from MoveListener class. Then it should start by calling start() method in the constructor.

The following method should be implemented:

public void resume()

The timer should start again using its start method, and time should be re-initialized to 0.0.

public void clearPanel()

The array list ptList should be cleared and refresh its panel so that trajectories will be erased.

public void changeColor(Color anotherColor)

The changeColor method sets the color of trajectories using the parameter

public void setAngle(int angle1)

It sets the initial angle using the parameter.

public void setVelocity(int velocity1)

This method set the initial velocity using the parameter

public void paintComponent(Graphics page)

It should draw all dots in the array list ptList.

MoveListener class

This class can be defined as a private class of the TrajectoryPanel. It implements ActionListener interface.

public void actionPerformed(ActionEvent event)

Its actionPerformed method defines how a trajectory is drawn by changing its time by adding "step" value to it, and re-compute the new x, y coordinate every time based on the current time, initial velocity, initial angle, initial y coordinate, and gravity.

New x and y should be computed as follows:

int x = (int) (velocity*time*Math.cos((angle/180.0)*Math.PI));
int y = initialY - ((int) (velocity*time*Math.sin((angle/180.0)*Math.PI) (0.5*GRAVITY*time*time)));

Using x and y values, and the current color, create a new object of Dot class, add it to the array list and re-paint the TrajectoryPanel after such change. Once y coordinate reaches the top or bottom of its corresponding panel, or x coordinate reaches the left side of its corresponding panel, stop the timer so that it will not keep drawing the trajectory.

TrajectoryControlPanel class

The TrajectoryControlPanel is a subclass of JPanel class. It contains 3 buttons including a start button, a clear button, and a color button. It also contains two labels and two JSlider objects. It also contains one panel -- an object of TrajectoryPanel class. Note that two objects of this class will be used, one for the red trajectories and one for the blue trajectories in the ControlPanel class.

The following constructor method is already given:

public TrajectoryControlPanel(int width, int height, Color initialColor)

Its parameters are width and height of the applet, and the initial color of the trajectories for the panel. It should instantiate each components and arrange them using layout managers. The JSlider for velocity should have its range from 10 to 100, and the initial velocity should be 50. Its major tick spacing should be 10, and it should be shown vertically. The JSlider for the initial angle should have its range from 5 to 85, with the initial value 45. Its major tick spacing should be 10, and it should be shown vertically. You can use other methods defined in JSlider to make your Slider look like the one shown in this page. An object of the ButtonListener class should be added to each button, and an object of the VelocityListener class should be added to the velocity JSlider object, and an object of the AngleListener class should be added to the angle JSlider.

ButtonListener class

This class can be defined as a private class of the TrajectoryControlPanel. It implements ActionListener interface.

public void actionPerformed(ActionEvent event)

Its actionPerformed method should define an action for each button (There are 3 buttons). To distinguish buttons, you can use getSource() method of the ActionEvent object. For instance, if "start" is the start button for the trajectories panel, we can check if that button is pushed as:

public void actionPerformed(ActionEvent event)
{
if (event.getSource() = = start)
{
...
}
}

VelocityListener class

This class can be defined as a private class of the TrajectoryControlPanel. It implements ChangeListener interface. You need to provide a definition for the following:

public void stateChanged(ChangeEvent event)

by getting the selected value of the slider, and assign it as a velocity of the corresponding trajectories.

AngleListener class

This class can be defined as a private class of the TrajectoryControlPanel. It implements ChangeListener interface. You need to provide a definition for the following:

public void stateChanged(ChangeEvent event)

by getting the selected value of the slider, and assign it as an angle of trajectories.

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.