Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that constructs an Applet. The Applet (JApplet) of your pro

ID: 3713261 • Letter: W

Question

Write a Java program that constructs an Applet.

The Applet (JApplet) of your program should contain three buttons, "Start", "Clear", and "Color". These three buttons will be organized vertically. Next to each set of buttons, there will be a label "Initial Wave Delay", then below it, there will be a slider that a user can use to change the initial delay of waves. Below the slider for the initial delay, there will be another label "Initial Wave Width", and below it, there will be a slider that a user can use to change the initial wave width (cycle) for waves. Next to the buttons and sliders, there are panels, each panel showing waves with their initial color. The top waves should be red and the bottom one should be blue initially. The picture below shows a snap shot of waves, and its background is white.

When "Clear" button is pushed, waves in the corresponding panel should be erased. When "Start" button is pushed, a new wave should start drawing using the initial delay, width (cycle), 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 disabled for this color chooser to pop up.

By moving each slider, a user should be able to change the initial delay and initial wave width (cycle). Note that these two sets of waves can have a completely independent movement of each other.

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

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 wave. It has the following attributes:

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.

WavePanel class

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

The following constructor method should be provided:

public WavePanel(Color color)

The color is initialized to the value of the color parameter. The array list should be instantiated. The waveHeight should be initialized to 72, and the waveWidth should be initialized to 50. The delay should be initialized to 20, and the step should be initialized to 1, time should be initialized 0. The background should be set to white color.
The timer should be instantiated with "delay" and the listener object created from WaveListener 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.

public void clearPanel()

The array list ptList should be cleared and refresh its panel so that waves will be erased, and set time to 0.

public void changeColor(Color anotherColor)

The changeColor method sets the color of the wave using the parameter

public void setWaveWidth(int newWidth)

It sets the waveWidth using the parameter.

public void setDelay(int delayNum)

This method changes the delay of the timer object.

public void paintComponent(Graphics page)

It should draw all dots in the array list ptList.

WaveListener class

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

public void actionPerformed(ActionEvent event)

Its actionPerformed method defines how a wave 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, waveWidth, waveHeight.

New x and y should be computed as follows:

int x = (waveWidth*time)/50;
int y = (int) (waveHeight * Math.sin((0.0174533)*time) + 85);

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 WavePanel 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 wave.

WaveControlPanel class

The WaveControlPanel 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 WavePanel class. Note that two objects of this class will be used, one for the red wave and one for the blue wave in the ControlPanel class.

The following constructor method is already given:

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

Its parameters are width and height of the applet, and the initial color of the waves for the panel. It should instantiate each components and arrange them using layout managers. The JSlider for delay should have its range from 0 to 40, and the initial delay should be 20. Its major tick spacing should be 10 and minor tick spacing should be 5, and it should be shown horizontally. The JSlider for the initial wave width should have its range from 5 to 100, with the initial value 50. Its major tick spacing should be 10 and minor tick spacing should be 5, and it should be shown horizontally. 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 DelayListener class should be added to the delay JSlider object, and an object of the WaveWidthListener class should be added to the waveWidth JSlider.

ButtonListener class

This class can be defined as a private class of the WaveControlPanel. 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 waves panel, we can check if that button is pushed as:

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

DelayListener class

This class can be defined as a private class of the WaveControlPanel. 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 delay of the corresponding wave.

WaveWidthListener class

This class can be defined as a private class of the WaveControlPanel. 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 the wave width (cycle) of the corresponding wave.

Given Code:

Assignment 12.java //NO CHANGE NEEDED IN THIS CLASS

ControlPanel.java //NO CHANGE NEEDED IN THIS CLASS

WaveControlPanel.java //Code Needed

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.

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class BeamsControlPanel extends JPanel
{
//components of the panel
private BeamsPanel beamsPanel;
private JButton start, stop, colorButton;
private JSlider speed, beam;
private JLabel label1, label2;
private JColorChooser chooser;
private JPanel buttons1;
private int width, height;
private Color color;
//Constructor to create all components, add their listener to them,
//and arrange them using a layout.
public BeamsControlPanel(int width, int height, Color initialColor)
{
//create a color chooser with the specified initial color
chooser = new JColorChooser(initialColor);
color = initialColor;
this.width = width;
this.height = height;
//create a panel displaying beams, with the specified color
beamsPanel = new BeamsPanel(initialColor, width);
//create 3 buttons, start, stop, and color chooser for beams.
start = new JButton("Start");
stop = new JButton("Stop");
colorButton = new JButton("Color");

//add a listener to each button
start.addActionListener(new ButtonListener());
stop.addActionListener(new ButtonListener());
colorButton.addActionListener(new ButtonListener());
buttons1 = new JPanel();
buttons1.setLayout(new GridLayout(3,1));
buttons1.add(start);
buttons1.add(stop);
buttons1.add(colorButton);

//create a slider for the delay with major tick spacing 10,
//minor tick spacing 1. The minimum value is 0, the maximum
//is 50, and the initial set value is 20.
speed = new JSlider(JSlider.VERTICAL, 0, 50, 20);
speed.setMajorTickSpacing(10);
speed.setMinorTickSpacing(1);
speed.setPaintTicks(true);
speed.setPaintLabels(true);
speed.setAlignmentX(Component.LEFT_ALIGNMENT);
speed.addChangeListener(new SpeedListener());
//create a label for the delay
label1 = new JLabel("Delay");
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(label1, BorderLayout.NORTH);
panel3.add(speed, BorderLayout.CENTER);

//create a slider for the number of beams with major tick spacing 4,
//minor tick spacing 1. The minimum value is 4, the maximum
//is 36, and the initial set value is 8.
beam = new JSlider(JSlider.VERTICAL, 4, 36, 8);
beam.setMajorTickSpacing(4);
beam.setMinorTickSpacing(1);
beam.setPaintTicks(true);
beam.setPaintLabels(true);
beam.setAlignmentX(Component.LEFT_ALIGNMENT);
beam.addChangeListener(new BeamListener());
//create a label for the number of beams
label2 = new JLabel("Beam Num");
JPanel panel4 = new JPanel();
panel4.setLayout(new BorderLayout());
panel4.add(label2, BorderLayout.NORTH);
panel4.add(beam, BorderLayout.CENTER);

JPanel panel6 = new JPanel();
panel6.setLayout(new GridLayout(1,2));
panel6.add(panel3);
panel6.add(panel4);
JPanel panel5 = new JPanel();
panel5.setLayout(new BorderLayout());
panel5.add(buttons1, BorderLayout.CENTER);
panel5.add(panel6, BorderLayout.EAST);

setLayout(new BorderLayout());
panel5.setPreferredSize(new Dimension(width, height/3));
add(beamsPanel, BorderLayout.CENTER);
add(panel5, BorderLayout.NORTH);
}
//ButtonListener defines actions to be performed when each button
//is pushed.
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object action = event.getSource();
if(action == stop) {
beamsPanel.suspend();
  
}
if(action == start) {
beamsPanel.resume();
}
if (action == colorButton)
{
color = chooser.showDialog(null, "Change beam color", color);
beamsPanel.changeColor(color);
}

}
} //end of ButtonListener
//SpeedListener adjust the delay of beams based on
//the chosen integer in the corresponding slider.
private class SpeedListener implements ChangeListener
{
public void stateChanged(ChangeEvent event)
{
beamsPanel.setDelay(speed.getValue()); // sets the delay for our drawing
}
} //end of SpeedListener

//BeamListener adjust the delay of beams based on
//the chosen integer in the corresponding slider.
private class BeamListener implements ChangeListener
{
public void stateChanged(ChangeEvent event)
{
beamsPanel.setBeamNumber(beam.getValue()); // sets the amount of beams we are drawing
}
} //end of BeamListener
}


ControlPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ControlPanel extends JPanel
{
private int width, height;
private int panelNum;
//The constructor creates creates 2 beams panels and
//control panels that control their movement, and organize them using a layout
public ControlPanel(int width, int height)
{
this.width = width;
this.height = height;
panelNum = 2; //the number of beams panels is 2
//create 2 panels to control the movement of beams
BeamsControlPanel[] beamsPanels;
beamsPanels = new BeamsControlPanel[panelNum];
beamsPanels[0] = new BeamsControlPanel(width/panelNum, height, Color.RED);
beamsPanels[1] = new BeamsControlPanel(width/panelNum, height, Color.BLUE);
//add two beams panels into this control panel using GridLayout
setLayout(new GridLayout(1, panelNum));
for (int i = 0; i < panelNum; i++)
add(beamsPanels[i]);
//set preferred size of this panel
setPreferredSize(new Dimension(width,height));
}
}

Assignment12.java
import javax.swing.*;
public class Assignment12 extends JApplet
{
private final int WIDTH = 450;
private final int HEIGHT = 340;
public void init()
{
ControlPanel panel = new ControlPanel(WIDTH,HEIGHT);
getContentPane().add(panel);
setSize(WIDTH,HEIGHT);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote