ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 12 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 08 May 2017 My Price 9.00

Write a Java program that constructs an Applet

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.

 

When "Stop" button is pushed, their corresponding word printing movement stops. When "Start" button is pushed, the word printing movement should start printing again.

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.

 

 

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.

 

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.

 

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


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.

public class Assignment12 extends JApplet
 {
  private final int WIDTH = 550;
  private final int HEIGHT = 340;

  public void init()
   {
       ControlPanel panel = new ControlPanel(WIDTH,HEIGHT);
       getContentPane().add(panel);
       setSize(WIDTH,HEIGHT);
   }
 }
public class ControlPanel extends JPanel
 {
  private int width, height;
  private int panelNum;

  //The constructor creates creates 2 word 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
       WordControlPanel[] wordPanels;
       wordPanels = new WordControlPanel[panelNum];

       wordPanels[0] = new WordControlPanel(width, height/panelNum, Color.RED, "Hello");
       wordPanels[1] = new WordControlPanel(width, height/panelNum, Color.BLUE, "Bye");

       //add two beams panels into this control panel using GridLayout
       setLayout(new GridLayout(panelNum, 1));
       for (int i = 0; i < panelNum; i++)
            add(wordPanels[i]);

       setPreferredSize(new Dimension(width,height));
    }
}
public class WordControlPanel extends JPanel
 {
      //components of the panel
      private WordPanel wPanel;
      private JButton start, stop, clear, change;
      private JSlider speed;
      private JLabel label1;
      private JPanel buttons1;
      private JTextField wordField;

      private int width, height;
      private Color color;

      //Constructor to create all components, add their listener to them,
      //and arrange them using a layout.
      public WordControlPanel(int width, int height, Color initialColor, String initialString)
       {
           color = initialColor;
           this.width = width;
           this.height = height;

           //create a panel displaying words, with the specified color
           wPanel = new WordPanel(initialColor, initialString);

           //create 3 buttons, start, stop, and clear
           start = new JButton("Start");
           stop = new JButton("Stop");
           clear = new JButton("Clear");

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

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

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

           //create a button to change the word
           change = new JButton("Change the word");
           change.addActionListener(new ButtonListener());
           
           //create a textfield to enter a word
           wordField = new JTextField(initialString);
           
           //create a label for the delay
           label1 = new JLabel("Delay");

           //create a slider for the delay with major tick spacing 100,
           //minor tick spacing 25. The minimum value is 200, the maximum
           //is 600, and the initial set value is 400.
           speed = new JSlider(JSlider.HORIZONTAL, 200, 600, 400);
           speed.setMajorTickSpacing(100);
           speed.setMinorTickSpacing(25);
           speed.setPaintTicks(true);
           speed.setPaintLabels(true);
           speed.setAlignmentX(Component.LEFT_ALIGNMENT);
           speed.addChangeListener(new SpeedListener());
           

           JPanel panel5 = new JPanel();
           panel5.setLayout(new GridLayout(5,1));
           panel5.add(buttons1);
           panel5.add(change);
           panel5.add(wordField);
           panel5.add(label1);
           panel5.add(speed);

           setLayout(new BorderLayout());
           panel5.setPreferredSize(new Dimension(width/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();

            /***to be completed***/

         }
     } //end of ButtonListener

   //SpeedListener adjust the delay of words drawing movement 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
 }

 

Attachments:

Answers

(11)
Status NEW Posted 08 May 2017 08:05 AM My Price 9.00

-----------

Attachments

file 1494232108-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)