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

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

ID: 3555205 • Letter: Y

Question

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

Assignment12.java (It is complete)

ControlPanel.java (It extends JPanel, to be completed)

BallPanel.java (It extends JPanel)

You may add more classes or more methods than they are specified.

Skills to be Applied:

Swing/AWT

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

Write a Java program that constructs an Applet.

When the balls hit the end of the panel, they reverse their direction. A user can stop, change its direction to up, down, left, or right, and change their speed by using the sliders.

(The size of the applet here is approximately 450 X 300).

You need to create the following classes.

BallPanel class

BallPanel class extends JPanel class. It is used to define a panel where a ball is moving. It has the following additional attributes:

Attribute name

Attribute type

Description

x

Int

x-coordinate of the starting point of the ball's movement.

y

Int

y-coordinate of the starting point of the ball's movement.

ballColor

Color

color of the ball.

backColor

Color

background color of the panel

timer

Timer

An object of Timer in javax.swing package. This is used to control the ball's movement.

delay

Int

delay of the timer.

stepX

Int

each step that the ball moves in the horizontal direction. It is initialize to 3.

stepY

Int

each step that the ball moves in the vertical direction. It is initialize to 0.

CIRCLE_DIAMETER

final int

the diameter of the ball. It is set to 20.

The following constructor should be provided:

public BallPanel(int x, int y, Color ballColor, Color backColor)

x, y, ballColor, and backColor are initialized to the values of four parameters. delay should be initialized to 20, stepX should be initialized to 3, and stepY should be initialized to 0. The background should be set to the color that the variable "backColor" contains. Timer should be instantiated with "delay" and the listener object created from MovingBallListener class. Then it should start by calling start() method in the constructor.

The following method should be implemented:

public void up()

Because we want the ball to start moving up, we set stepX to 0 and stepY to -3. The timer should start again using its start method.

public void down()

Because we want the ball to start moving down, we set stepX to 0 and stepY to 3. The timer should start again using its start method.

public void left()

Because we want the ball to start moving left, we set stepX to -3 and stepY to 0. The timer should start again using its start method.

public void right()

Because we want the ball to start moving right, we set stepX to 3 and stepY to 0. The timer should start again using its start method.

public void suspend()

The timer should stop using its stop method.

public void setDelay(int delayNum)

This method set the delay of the timer using its parameter.

public void paintComponent(Graphics page)

A color of the ball should be set, and a ball should be drawing using the (x,y) coordinate and CIRCLE_DIAMETER.

MovingBallListener class

This class can be defined as a private class inside the BallPanel class. It implements ActionListener interface.

public void actionPerformed(ActionEvent event)

Its actionPerformed method implements how the ball should move by adding stepX and stepY to the corresponding variable, and re-paint the BallPanel after such change. In case the ball hits the side of the panel, it needs to reverse its direction. The width of the panel can be obtained by "getSize().getWidth()". For instance, you can check if the ball hits the right hand side wall by:

if (x > getSize().getWidth()-CIRCLE_DIAMETER && stepY == 0)

You should make use of methods such as up(), down(), left(), and right() created above.

ControlPanel class

The ControlPanel extends the JPanel class. It contains 10 buttons including two up buttons, two down buttons, two left buttons, two right buttons, and two stop buttons. It also contains two labels, two sliders, and two objects of the BallPanel class, one for the red ball and one for the blue ball.
You may create as many JPanel as you need to organize components using different layout managers.

The following constructor should be provided:

public ControlPanel(int width, int height)

Its parameters are width and height of the applet. It should instantiate each components and arrange them using layout managers. Add ButtonListener to each button, and add SliderListener to each slider.

ButtonListener class

This is a private class inside the ControlPanel. It implements ActionListener interface.

public void actionPerformed(ActionEvent event)

Its actionPerformed method defines an action for each button (There are 10 buttons). To distinguish buttons, you can use getSource() method of the ActionEvent object. For example, if "stop1" is the stop button for the red ball, here is how to check if that button is pushed:

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

SliderListener class

This class is a private class inside the ControlPanel too. It implements ChangeListener interface. You need to provide code for the following method:

public void stateChanged(ChangeEvent event)
by getting the selected value of each slider, and assign it as a delay of the corresponding ball panel.

This is the assignment 12 File:

And the ControlPanel file:

Attribute name

Attribute type

Description

x

Int

x-coordinate of the starting point of the ball's movement.

y

Int

y-coordinate of the starting point of the ball's movement.

ballColor

Color

color of the ball.

backColor

Color

background color of the panel

timer

Timer

An object of Timer in javax.swing package. This is used to control the ball's movement.

delay

Int

delay of the timer.

stepX

Int

each step that the ball moves in the horizontal direction. It is initialize to 3.

stepY

Int

each step that the ball moves in the vertical direction. It is initialize to 0.

CIRCLE_DIAMETER

final int

the diameter of the ball. It is set to 20.

Explanation / Answer

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.event.*;

public class ControlPanel extends JPanel

{

// Declare variables to use throughout Project:

   private FanPanel redFan, blueFan;

   private int width, height;

   // Six Buttons

   private JButton redStop, redStart, redReverse; // Red Buttons

   private JButton blueStop, blueStart, blueReverse;// Blue Buttons

   // Two labels

   private JLabel blueSliderLabel, redSliderLabel;

   // Two Sliders

   private JSlider redSlider, blueSlider;

   // Panels to organize everything as I drew out with different layouts.

   private JPanel blueSliderPanel, redSliderPanel, blueSide, redSide, topPanel, bottomPanel, redBotPanel, blueBotPanel;

   // Split pane to make GUI look exactly like hers

   private JSplitPane splitPane;

   

   

   /* Instantiate each component and arrange them using layout managers.

* JSliders should have range from 0 to 50.

* Initial delay should be 20.

* Major tick spacing is 10, vertically.

* Add a ButtonListener to each button, and SliderListener to each slider.

* NOTE: Use copy and replace feature to easily change the red features to the blue features. */

   public ControlPanel(int width, int height)

    {

       this.width = width;

       this.height = height;

       //create two fan panels, one with read, another with blue

       redFan = new FanPanel(Color.red, width/2);

       blueFan = new FanPanel(Color.blue, width/2);

       // Set up and Create the materials that will be used in each panel before adding them to it.

       

       // Create the buttons for the red fan, which will be on the left.

       redStart = new JButton("Start Red"); // Start Button

       // add its actionListener here

       redStart.addActionListener(new ButtonListener()); // Statr Button Action Listener

       redStop = new JButton("Stop Red"); // Stop Button

       redStop.addActionListener(new ButtonListener()); // Stop Button Action Listener

       redReverse = new JButton("Reverse Red"); // Reverse Button

       redReverse.addActionListener(new ButtonListener()); // Reverse Button Action Listener

       

       // Now the Blue buttons

       blueStart = new JButton("Start Blue"); // Start Button

       blueStart.addActionListener(new ButtonListener()); // Statr Button Action Listener

       blueStop = new JButton("Stop Blue"); // Stop Button

       blueStop.addActionListener(new ButtonListener()); // Stop Button Action Listener

       blueReverse = new JButton("Reverse Blue"); // Reverse Button

       blueReverse.addActionListener(new ButtonListener()); // Reverse Button Action Listener

       

       // Set the labels for the sliders up

       redSliderLabel = new JLabel("Red Fan Delay"); // Delay Slider

       blueSliderLabel = new JLabel("Blue Fan Delay"); // Delay Slider

       

       // Now set up the sliders

       // Red slider

       redSlider = new JSlider(1, 0, 50, 20); // Red Slider

       // show its ticks

       redSlider.setPaintTicks(true); // Showing Ticks

       // Show its labels

       redSlider.setPaintLabels(true); // Showing Labels

       // set the tick spacing to 10 (as stated)

       redSlider.setMajorTickSpacing(10); // Setting Tick spacing

       // setting the minor tick spacing

       redSlider.setMinorTickSpacing(5); // Set minor tick spacing

       // Add its action listener, or change listener in this case

       redSlider.addChangeListener(new SliderListener()); // Change Listener

       

       // Blue Slider

       blueSlider = new JSlider(1, 0, 50, 20); // blue Slider

       // show its ticks

       blueSlider.setPaintTicks(true); // Showing Ticks

       // Show its labels

       blueSlider.setPaintLabels(true); // Showing Labels

       // set the tick spacing to 10 (as stated)

       blueSlider.setMajorTickSpacing(10); // Setting Tick spacing

       // setting the minor tick spacing

       blueSlider.setMinorTickSpacing(5); // Set minor tick spacing

       // Add its action listener, or change listener in this case

       blueSlider.addChangeListener(new SliderListener()); // Change Listener

       

       // Now time to set up the panels since all the elements are initialized

       // Order from left to right, top to bottom is:

       // Red Buttons, Red Slider, Blue Buttons, Blue Slider, Split pane under those, then red fan, then blue fan.

       

       // Red Fan buttons (left most)

       redSide = new JPanel();

       redSide.setLayout(new GridLayout(3,1)); // Set the layout to 3 rows, 1 column, set the buttons up vertically

       // Add the buttons

       redSide.add(redStart); // Add the redStart button

       redSide.add(redStop); // Add the redStop button

       redSide.add(redReverse); // Add the redReverse button.

       

       // Next up Red Slider Panel

       redSliderPanel = new JPanel();

       // Use borderlayout to put label North, and the Slider in the center

       redSliderPanel.setLayout(new BorderLayout());

       // add the two elements

       redSliderPanel.add(redSliderLabel, BorderLayout.NORTH); // Set the label to north

       redSliderPanel.add(redSlider, BorderLayout.CENTER); // Set the slider to Center.

       

       

       // blue Fan buttons (third panel)

       blueSide = new JPanel();

       blueSide.setLayout(new GridLayout(3,1)); // Set the layout to 3 rows, 1 column, set the buttons up vertically

       // Add the buttons

       blueSide.add(blueStart); // Add the blueStart button

       blueSide.add(blueStop); // Add the blueStop button

       blueSide.add(blueReverse); // Add the blueReverse button.

       

       // Next up blue Slider Panel

       blueSliderPanel = new JPanel();

       // Use borderlayout to put label North, and the Slider in the center

       blueSliderPanel.setLayout(new BorderLayout());

       // add the two elements

       blueSliderPanel.add(blueSliderLabel, BorderLayout.NORTH); // Set the label to north

       blueSliderPanel.add(blueSlider, BorderLayout.CENTER); // Set the slider to Center.

       

       // Now above the split pane we have the topPanel which has the buttons and sliders in order.

       topPanel = new JPanel();

       // set the layout to 1 row, 4 columns (one for each element above)

       topPanel.setLayout(new GridLayout(1,4));

       // add the panels

       topPanel.add(redSide); // Add the red buttons

       topPanel.add(redSliderPanel); // Add the red slider

       topPanel.add(blueSide); // Add the blue buttons

       topPanel.add(blueSliderPanel); // Add the blue slider

       

       /*

// Two panels for the redFan and blueFan to center them in the bottomPanel gridLayout.

redBotPanel = new JPanel();

redBotPanel.setLayout(new BorderLayout()); // set to borderlayout to add fan to center.

redBotPanel.add(redFan, BorderLayout.CENTER);

// blue

blueBotPanel = new JPanel();

blueBotPanel.setLayout(new BorderLayout()); // set to borderlayout to add fan to center.

blueBotPanel.add(blueFan, BorderLayout.CENTER);

*/

       

       // Now below the split pane we have the two sides for each fan, red on left, blue on right.

       bottomPanel = new JPanel();

       // layout is 1 row, 2 columns (for each fan)

       bottomPanel.setLayout(new GridLayout(1,2));

       // add the two fans

       bottomPanel.add(redFan); // Add the red fan.

       bottomPanel.add(blueFan); // Add the blue fan.

       

       // Now that top and bottom are set up, set up the split pane.

       splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel); // vertically split it, with toppanel on top, bottom on bottom.

       splitPane.setContinuousLayout(true); // Set the continous layout to true or it messes up

       setLayout(new BorderLayout()); // Set it to border layout because none of the rest worked

       add(splitPane); // add the splitpane to it.

       

       //set preferred size of this panel

       setPreferredSize(new Dimension(width,height));

    }

   

   /* Use the event and getSource() to define an action for each button using methods in FanPanel.java.

* Consider using a switch statement. // Cannot use switch on object. */

   private class ButtonListener implements ActionListener

    {

       public void actionPerformed(ActionEvent event)

         {

     // Use getSource() of the event to get which Button was pressed and act accordingly.

            Object action = event.getSource();

            // Giant if statements to see what the button is and uses the appropriate method.

            if ( action == redStart ) // if it is redStart, start the red fan

             redFan.resume();

            // else, if not that, check the next

            else if ( action == redStop ) // if it is redStop, suspend the red fan

             redFan.suspend();

            else if ( action == redReverse ) // if it is redReverse, reverse the fan

             redFan.reverse();

            // blue buttons

            else if ( action == blueStart ) // if it is blueStart, resume

             blueFan.resume();

            else if ( action == blueStop ) // if it is blueStop, suspend the blue fan

             blueFan.suspend();

            else if ( action == blueReverse ) // if it is blueReverse, reverse it

             blueFan.reverse();

         }

    } //end of ButtonListener class

   

   /* Get the selected value of each slider, and assign it as a delay of the corresponding fan. */

   private class SliderListener implements ChangeListener

    {

       public void stateChanged(ChangeEvent event)

         {

             // get the source of the event, whether it is blue or red.

     Object action = event.getSource();

    

     // Then, if the action is blue, do the following

     if ( action == blueSlider )

     blueFan.setDelay( blueSlider.getValue() ); // set the delay of fan 2 (blue fan) to the value from the slider.

     else // if not blue, it's... RED!

     redFan.setDelay( redSlider.getValue() ); // set the delay of fan 1 (red fan) to the value from the slider.

         }

     } //end of SliderListener class

} //end of ControlPanel class

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