Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Applet (JApplet) of your program should contain three buttons for beams, Start, Stop, and Color. These three buttons will be organized vertically. Next to

The Applet (JApplet) of your program should contain three buttons for beams, "Start", "Stop", and "Color". These three buttons will be organized vertically. Next to each set of buttons, there will be a label "Delay", then below it, there will be a slider that a user can use to change the delay (speed) of beams. Note than if the delay (interval) of beams decreases, its speed will be faster. Next to the slider for delay, there will be another label "Beam Num", and below it, there will be a slider that a user can use to change the number of beams for beams. Below the buttons and sliders, there are panels, each panel showing beams with their initial color. The left beams should be red and the right one should be blue initially. The picture below shows a snap shot of beams. Beams consist of multiple filled arcs determined by the number of beams, and its background is black.

image text in transcribed

When "Stop" button is pushed, corresponding beams stop. When "Start" button is pushed, the beams start moving, i.e., their beams will start extending from its center towards outside. 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 delay interval of beams or their number of beams. When a delay decreases, the corresponding beams should move faster, and when a delay increases, the beams should move slower. Note that these two sets of beams can have a completely independent movement of each other.

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

You need to create the following classes.

BeamsPanel class

BeamsPanel class a subclass of JPanel class. It is used to define a panel where beams are moving. It has the following additional attributes: image text in transcribed

The following constructor method should be provided:

public BeamsPanel(Color color, int width)

The color is initialized to the value of the color parameter. The delay should be initialized to 20, and the step should be initialized to 3. The background should be set to black color. The centerX and centerY should be initialized to (width/2). The diameterLimit should be initialized to (width-10)/2, and the diameter should be initialized to 0 since the beams will spread from inside to outside. The beamNum should be initialized to 8. The angleSize should be initialized to 360/(beamNum*2). 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.

public void suspend()

The timer should stop using its stop method.

public void changeColor(Color anotherColor)

The changeColor method sets the color of beams using the parameter

public void setBeamNumber(int beam)

It sets the number of beams using the parameter.

public void setDelay(int delayValue)

This method set the delay of the timer using its parameter. (Hint: setDelay method of the Timer class)

public void paintComponent(Graphics page)

A color of the beams (arcs) should be set, and beams should be drawing using the coordinate (centerX, centerY) and diameter. Since the beamNum might have been changed, the angleSize should be re-set to 360/(beamNum*2) (-- example, to have 8 beams, each angle size will be 360 degree/(8*2) since there will be a space between beams.)

The fillArc method should be used, but the first two parameters are the upper left corner of the bounding rectangle, so its radius (half of diameter) should be subtracted as:

page.fillArc(centerX-diameter/2, centerY-diameter/2, diameter, diameter, currentAngle, angleSize);

where "currentAngle" starts with 0 and goes up to 360 degrees, and increased by 2*angleSize in each step. Thus this line should be repeated in a loop to draw all beams.

MoveListener class

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

public void actionPerformed(ActionEvent event)

Its actionPerformed method defines how the beams (arcs) should move next, by changing its diameter by adding "step" value to it, and re-paint the BeamsPanel after such change. Note that once the diameter reaches the diameterLimit value, then it should be re-set to 0 so that the beams start again from its center to outside.

BeamsControlPanel class

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

The following constructor method is already given:

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

Its parameters are width and height of the applet, and the initial color of the beams 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 50, and the initial delay should be 20. Its major tick spacing should be 10, and it should be shown vertically. The JSlider for beam number should have its range from 4 to 36, with the initial value 8. Its major tick spacing should be 4, 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 SpeedListener class should be added to the delay JSlider object, and an object of the BeamListener class should be added to the beam number JSlider.

ButtonListener class

This class can be defined as a private class of the BeamsControlPanel. 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 beams panel, we can check if that button is pushed as: public void actionPerformed(ActionEvent event) { if (event.getSource() = = start) { ... } }

SpeedListener class

This class can be defined as a private class of the BeamsControlPanel. 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 beams.

BeamListener class

This class can be defined as a private class of the BeamsControlPanel. 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 number of beams.

-----------------------------------------------------------------------------

These files provided by the instructor:

Assignment12: (Is Completed)

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); } } 

-----------------------------------------------------------------------------

ControlPanel: (Is Completed)

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  

-----------------------------------------------------------------------------

BeamsControlPanel: (Needs to be completed)

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(); /***to be completed***/ 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) { /***to be completed***/ } } //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) { /***to be completed***/ } } //end of BeamListener } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago