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

No late assignment will be accepted. Make sure that you write every line of your

ID: 3717762 • Letter: N

Question

No late assignment will be accepted.

Make sure that you write every line of your code. Using code written by someone else will be considered a violation of the academic integrity and will result in a report to the Dean's office.

Requirements to get full credits in Documentation

The assignment number, your name, student ID, lecture number, and a class/file description need to be included at the top of each file/class.

A description of each method is also needed.

Some additional comments inside of methods to explain code that are hard to follow should be written.

You can look at Java programs in the text book to see how comments are added to programs.

Minimal Submitted Files

You are required, but not limited, to turn in the following source files:

Assignment12.java (The Assignment12 class extends JApplet -- no need to change)
ControlPanel.java (It extends JPanel -- no need to change)
WaveControlPanel.java (It extends JPanel, to be completed)
WavePanel.java (It extends JPanel)
Dot.java
You may add more classes or more methods than the specified ones. (You might need them.)

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:

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.

Assignment12.java

ControlPanel.java

WaveControlPanel.java

WavePanel.java

Dot.java

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 WaveControlPanel extends JPanel
{
    //components of the panel
    private WavePanel wPanel;
    private JButton start, clear, colorButton;
    private JSlider waveWidth, delay;
    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 WaveControlPanel(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 waves, with the specified color
        wPanel = new WavePanel(initialColor);

        //create 3 buttons, start, clear, and color chooser for waves.
        start = new JButton("Start");
        clear = new JButton("Clear");
        colorButton = new JButton("Color");


        //add a listener to each button
        start.addActionListener(new ButtonListener());
        clear.addActionListener(new ButtonListener());
        colorButton.addActionListener(new ButtonListener());

        buttons1 = new JPanel();
        buttons1.setLayout(new GridLayout(3,1));

        buttons1.add(start);
        buttons1.add(clear);
        buttons1.add(colorButton);


        //create a slider for the delay with major tick spacing 10,
        //minor tick spacing 5. The minimum value is 0, the maximum
        //is 40, and the initial set value is 20.
        delay = new JSlider(JSlider.HORIZONTAL, 0, 40, 20);
        delay.setMajorTickSpacing(10);
        delay.setMinorTickSpacing(5);
        delay.setPaintTicks(true);
        delay.setPaintLabels(true);
        delay.setAlignmentX(Component.LEFT_ALIGNMENT);
        delay.addChangeListener(new DelayListener());

        //create a label for the delay
        label1 = new JLabel("Initial Wave Delay");

        JPanel panel3 = new JPanel();
        panel3.setLayout(new BorderLayout());
        panel3.add(label1, BorderLayout.NORTH);
        panel3.add(delay, BorderLayout.CENTER);


        //create a slider for the wave width with major tick spacing 10,
        //minor tick spacing 5. The minimum value is 5, the maximum
        //is 100, and the initial set value is 50.
        waveWidth = new JSlider(JSlider.HORIZONTAL, 5, 100, 50);
        waveWidth.setMajorTickSpacing(10);
        waveWidth.setMinorTickSpacing(5);
        waveWidth.setPaintTicks(true);
        waveWidth.setPaintLabels(true);
        waveWidth.setAlignmentX(Component.LEFT_ALIGNMENT);
        waveWidth.addChangeListener(new WaveWidthListener());

        //create a label for the wave width
        label2 = new JLabel("Initial Wave Width");

        JPanel panel4 = new JPanel();
        panel4.setLayout(new BorderLayout());
        panel4.add(label2, BorderLayout.NORTH);
        panel4.add(waveWidth, BorderLayout.CENTER);


        JPanel panel6 = new JPanel();
        panel6.setLayout(new GridLayout(2,1));
        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());
        wPanel.setPreferredSize(new Dimension((width*2)/3, height));
        add(wPanel, BorderLayout.CENTER);
        add(panel5, BorderLayout.WEST);

    }

    //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 == colorButton)
            {
                color = chooser.showDialog(null, "Change Wave color", color);
                wPanel.changeColor(color);
            } else if (action == start) {
                wPanel.resume();
            } else if (action == clear) {
                wPanel.clearPanel();
            }
        }
    } //end of ButtonListener

    //DelayListener adjusts the delay of the timer based on
    //the chosen integer in the corresponding slider.
    private class DelayListener implements ChangeListener
    {
        public void stateChanged(ChangeEvent event)
        {
            wPanel.setDelay(delay.getValue());
        }

    }


    //WaveWidthListener adjusts the value of waveWidth based on
    //the chosen integer in the corresponding slider.
    private class WaveWidthListener implements ChangeListener
    {
        public void stateChanged(ChangeEvent event)
        {
            wPanel.setWaveWidth(waveWidth.getValue());
        }
    }

}
--------------------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Dot {

    private Color color;
    int x,y;
    private final int RADIUS = 3;

    public Dot (int x1, int y1, Color color1) {
        color = color1;
        x = x1;
        y = y1;
    }

    public void draw (Graphics page) {
        page.fillOval(x,y,RADIUS,RADIUS);
    }
}
------------------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class WavePanel extends JPanel {

    private Color color;
    private Timer timer;
    private Dimension d;
    private int delay, waveWidth, waveHeight, time, step, maxX, maxY;
    private ArrayList<Dot> ptList;

    public WavePanel (Color color) {
        this.color = color;
        ptList = new ArrayList<>();
        waveHeight = 72;
        waveWidth = 50;
        delay = 20;
        step = 1;
        time = 0;
        Dimension d = this.getSize();
        maxX = (int)d.getWidth();
        maxY = (int)d.getHeight();
        timer = new Timer(delay, new WaveListener());
        timer.start();

    }

    public void resume() {
        time = 0;
        timer.start();
    }

    public void clearPanel() {
        ptList.clear();
        repaint();
        time = 0;
    }

    public void changeColor (Color anotherColor) {
        color = anotherColor;
    }

    public void setWaveWidth (int newWidth) {
        waveWidth = newWidth;
    }

    public void setDelay (int delayNum) {
        delay = delayNum;
    }

    public void paintComponent (Graphics page) {
        for (int i = 0; i < ptList.size(); i++)
            ptList.get(i).draw(page);
    }

    private class WaveListener implements ActionListener {

        public void actionPerformed (ActionEvent event) {
            int x = (waveWidth*time)/50;
            int y = (int)(waveHeight*Math.sin((0.0174533)*time)+85);
            Dot newDot = new Dot(x,y,color);
            ptList.add(newDot);
            repaint();
            if (y < 0 || y > maxY || x > maxX)
                timer.stop();
        }
    }
}

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