Swing/AWT, Animation/Multi-Threads Classes may be needed: Timer in javax.swing package
Question
Swing/AWT,
Animation/Multi-Threads
Classes may be needed:
Timer in javax.swing package,
JApplet, JButton, Container, JPanel, Color, Graphics, JSlider, JLabel, JTextField ActionListener, ActionEvent, ChangeListener, ChangeEvent. You may use other classes.
source files: (see end of post)
Assignment12.java (The Assignment12 class extends JApplet — no need to change)
ControlPanel.java (It extends JPanel — no need to change)
WordControlPanel.java (It extends JPanel, to be completed)
WordPanel.java (It extends JPanel)
Word.java
**You may add more classes or more methods than the specified ones. (You might need them.)Program Description
Suggested Class Diagram: (attached – hw12Diagram)
Write a Java program that constructs an Applet.
The Applet (JApplet) of your program should contain two sets of four buttons for a word, “Start”, “Stop”, “Clear” and “Change the word”. The first three buttons will be organized horizontally, and the forth one under the other three (see the snapshot of the applet). Under each set of buttons, there will be a textfield with some initial word. Under the textfield, there should be a label “Delay”, then below it, there will be a slider that a user can use to change the delay (speed) of a word. Note than if the delay (interval) of a word decreases, its speed will be faster. Right hand side of the buttons, a label, and sliders, there are panels, each panel showing a word with their initial color. The top word should be red and the bottom one should be blue with their background black. The picture below shows a snap shot of the applet. Each panel should keep printing the given word.
(see attached image- stop TOP)
When “Stop” button is pushed, their corresponding word printing movement stops. When “Start” button is pushed, the word printing movement should start printing again.
(see attached image- change the word TOP and change the word BOTTOM)
By entering another word in each textfield and pushing the button for “Change the word”, will make the panel to start printing the newly entered word in their corresponding drawing panel.
(see attached image- clear BOTTOM)
Pushing the “Clear” button should clear all words printed in the corresponding panel.
In the following snapshot, the clear button for the bottom panel was pushed, thus only the bottom panel was cleared.
(see attached image- different delay)
The size of the applet here is approximately 550 X 340).
By moving each slider, a user should be able to change the delay interval of word printing. When a delay decreases, the corresponding word printing is done faster, and when a delay increases, it is done slower. Note that these two sets of word printings can have a completely independent movement of each other.
You can click on this site to see how the applet should work:
You need to create the following classes.Word class
Word class represents one word to be drawn in a panel. It has the following additional attributes:
word | String | A word or a string to be drawn in a panel |
color | Color | color that is used to draw the word/string. |
x | int | x coordinate of the word/string to be drawn |
y | int | y coordinate of the word/string to be drawn |
The following constructor method should be provided:
public Word(String word1, int x1, int y1, Color color1)
The member variables, word, x, y, and color are initialized by the values of their corresponding parameter.
The following method should be implemented:
public void drawWord(Graphics page)
Using the parameter Graphics object, it should draw a string using the member variables word, and x and y coordinates and color.WordPanel class
WordPanel class a subclass of JPanel class. It is used to define a panel where words are drawn. It has the following additional attributes:
word | String | current word or a string to be drawn in a panel |
color | Color | color that is used to draw the word/string. |
currentX | int | current x coordinate of the word/string to be drawn |
currentY | int | current y coordinate of the word/string to be drawn |
stepX | int | each step that the current x coordinate moves for the word/string for each timer tick |
stepY | int | each step that the current y coordinate moves for the word/string for each timer tick |
wordList | ArrayList | an arraylist to store all words drawn so far, i.e., objects of Word class |
timer | Timer | An object of Timer in javax.swing package. This is used to control the beams’ movement. |
delay | int | delay of the timer. |
The following constructor method should be provided:
public WordPanel(Color color, String initialWord)
The color is initialized to the value of the color parameter, and the word is initialized to the value of the initialWord parameter.
The delay should be initialized to 400, and the stepX and stepY should be initialized to 30. The background should be set to black color. The currentX should be initialized to 0 and currentY should be initialized to 10. The array list should be instantiated. 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 clear()
It should remove all elements in the wordList array list so that all the words in the panel disappear.
public void setWord(String word2)
It sets the member variable word 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)
It should draw all words in the array list wordList.
MoveListener class
This class can be defined as a private class of the WordPanel. It implements ActionListener interface.
public void actionPerformed(ActionEvent event)
Its actionPerformed method defines how words are drawn in the panel. stepX should be added to currentX and stepY should be added to currentY then a new object of Word should be created using the updated currentX and currentY coordinate, and it should be added to the wordList array list before calling repaint() to update the panel.
Also the drawn words should not go outside of the panel. If currentX+stepX < 0 or currentX+stepX > getSize().getWidth()-word1.length()*7,
then the sign (+ or -) of stepX should be changed.
If currentY+stepY < 10 or currentY+stepY > getSize().getHeight(),
then the sign (+ or -) of stepY should be changed.WordControlPanel class
The WordControlPanel is a subclass of JPanel class. It contains 4 buttons including a start button, a stop button, a clear button, and a button to change the word. It also contains one JTextField, one JLabel and one JSlider. It also contains one panel — an object of WordPanel class. Note that two objects of this class will be used, one for the red words and one for the blue words in the ControlPanel class.
The following constructor method is already given:
public WordControlPanel(int width, int height, Color initialColor, String initialWord)
Its parameters are width and height of the applet, and the initial color of the word, and an initial word. It should instantiate each components and arrange them using layout managers. The JSlider for delay should have its range from 200 to 600, and the initial delay should be 400. Its major tick spacing should be 100, and minor tick spacing should be 25 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 SpeedListener class should be added to the delay JSlider object.
ButtonListener class
This class can be defined as a private class of the WordControlPanel. It implements ActionListener interface.
public void actionPerformed(ActionEvent event)
Its actionPerformed method should define an action for each button (There are 4 buttons). To distinguish buttons, you can use getSource() method of the ActionEvent object. For instance, if “start” is the start button for the word panel, we can check if that button is pushed as:
public void actionPerformed(ActionEvent event)
{
if (event.getSource() = = start)
{
…
}
}
If “stop” button is pushed, then the drawing words movement should stop. If the “clear” button is pushed, then all words in the corresponding panel should disappear. If a user types a new word in the JTextField and push “Change the word button”, then the new word should be the one to be drawn (do not change the words that are already drawn before.
SpeedListener class
This class can be defined as a private class of the WordControlPanel. 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.
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
