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

Write a Java program that constructs an Applet. The Applet (JApplet) of your pro

ID: 3672811 • Letter: W

Question

Write a Java program that constructs an Applet.

The Applet (JApplet) of your program should contain five JRadioButtons which a user can use to select a color to draw a line, two buttons "Undo" and "Erase", and one drawing panel where lines can be drawn. Radio buttons and two buttons are located at the left side of the applet.

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

A user can choose a color using radio buttons to draw lines. Until the color is changed, any line will be drawn with that color. The default color is black.

A user can move a mouse in the drawing panel and press the mouse where he/she wants to start drawing a line. The first point that a user clicks will be the starting point of the line. After a user presses the mouse, a user should "drag" the mouse until the mouse reaches to a point to determine the ending point of the line.

When a user is dragging the mouse, the drawing panel should show a line using the starting point and the current point.

When a user releases the mouse to finalize the ending of the line, the final line should be shown.

A user can continue to draw more lines. If two lines are overlapped, the line drawn later will be shown on the top.

When a user pushes the "Undo" button, the last drawn line will be erased from the drawing area. Thus if a user keeps pushing the "Undo" button, eventually all lines will be erased.

When a user pushes the "Erase" button, all lines should be erased. If a user pushes the "Undo" button immediately after pushing the "Erase" button, then all erased lines should come back (re-drawn) on the drawing panel.

Line class

The Line class represents a line to be drawn in the panel. It should contain at least the following instance variable:

This class should have a constructor:

public Line(int x1, int y1, int x2, int y2, Color color)

where the parameters x1 and y1 are (x1,y1) coordinate of the starting point of the line, x2 and y2 are (x2,y2) coordinate of the ending point of the line, and color is the color of the line.

This class should also contain a method:

public void draw(Graphics page)

This method should set the color and draw the line specified by its coordinates (x1,y1) and (x2,y2).
You can utilize its Graphics object parameter and drawLine method.

CanvasPanel class

The CanvasPanel class extends JPanel defined in javax.swing package. This is where lines are drawn. The background of this panel is white. It must contain the following method.

public void paintComponent(Graphics page)

Using the parameter, the Graphics object, this method will draw lines with their selected color, and coordinates. This can be done by calling the draw method of the Line class. Remember that this method need to call paintComponent method defined in its parent class. This class can be defined as nested class within the WholePanel class.

WholePanel class

The WholePanel class organizes all components in the applet. It extends JPanel defined in javax.swing package. It should contain at least the following instance variable:

This class should have a constructor:

public WholePanel()

This is where all components are arranged. Add as many instance variable as you would like to this class, and initialize them in this constructor. One way to define this class is to contain an object of CanvasPanel, two buttons "Undo", and "Erase", and objects of JRadioButton to choose colors.

ButtonListener class

The ButtonListener class implements ActionListener interface defined in java.awt.event package. It must implement the following method:

public void actionPerformed(ActionEvent event)

In this method, the action to be performed in case one of "Undo", and "Erase" button is pushed. This listener is used with JButtons.

ColorListener class

The ColorListener class implements ActionListener interface defined in java.awt.event package. It must implement the following method:

public void actionPerformed(ActionEvent event)

In this method, the color selected by using the JRadioButtons is assigned as a color to be used to draw a line. This listener is used together with JRadioButtons.

PointListner class

The PointListener class implements MouseListener interface and MouseMotionListener interface. It must implement the following method:

public void mousePressed(MouseEvent event)

When a user presses a mouse, that point is defined as the starting point of a line to be drawn. You might call this pressed point's coordinate (x1, y1).

public void mouseDragged (MouseEvent event)

After a user presses a mouse and drags it, a line needs to be drawn using the starting point and the current coordinate as the ending point of the line. You might call this dragged point's coordinate (x2, y2). As long as a mouse is not released yet (still dragging), then the line will keep changing.

public void mouseReleased(MouseEvent event)

When a user releases a mouse, a line needs to be drawn using the released point as the final point of the line. You might call this released point (x3, x3).

Other methods from MouseListener and MouseMotionListener can be left as blank.

Note that these tree listener classes and CanvasPanel class can be defined as nested classes inside of the WholePanel class.

Assignment7

import javax.swing.*;

While Panel: (Sections of code are missing, needs to be filled in)

Applet Viewer: Assignment7.class Applet black red blue green orange Undo Erase Applet started.

Explanation / Answer

// I have done this for a much broarder case. RECTANGLE . Hope it helps.

///////////////////////////////////////////////////////   Rect.java /////////////////////////////////////////////////////////

import java.awt.*;
public class Rect
{
   private int x;
   private int y;
   private Color color;
   private int width;
   private int height;
  
   public Rect(int x1, int y1, int width, int height, Color color)
   {
       x = x1;
       y = y1;
       this.width = width;
       this.height = height;
       this.color = color;
   }
   public void draw(Graphics page)
   {
       page.setColor(color);
       page.fillRect(x, y, width, height);
   }
  

}

/////////////////////////////////////////   WholePanel.java//////////////////////////////////////////////


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class WholePanel extends JPanel
{ //instance variables
   private Color currentColor;
   private CanvasPanel canvas;
   private JPanel primary, buttonPanel, leftPanel;
   private JButton erase, undo;
   private ArrayList rectList, tempList;
   private JRadioButton[] colorRButtons;
   private Color[] colors;
   private int x1, y1, x2, y2, x3, y3;
   private boolean mouseDragged = false;

   //Constructor to instantiate components
   public WholePanel()
   {
      //default color to draw rectangles is black
          currentColor = Color.black;
      rectList = new ArrayList();

             //create buttons
      erase = new JButton("Erase");
      undo = new JButton("Undo");
      ButtonListener listener2 = new ButtonListener();
      erase.addActionListener(listener2);
      undo.addActionListener(listener2);


      //create radio buttons for 5 colors
      //black will be chosen by default
      colorRButtons = new JRadioButton[5];
      colorRButtons[0] = new JRadioButton("black", true);
      colorRButtons[1] = new JRadioButton("red");
      colorRButtons[2] = new JRadioButton("blue");
      colorRButtons[3] = new JRadioButton("green");
      colorRButtons[4] = new JRadioButton("orange");
      // store 5 colors in an array
      colors = new Color[5];
      colors[0] = Color.black;
      colors[1] = Color.red;
      colors[2] = Color.blue;
      colors[3] = Color.green;
      colors[4] = Color.orange;
      //group radio buttons so that when one is selected,
      //others will be unselected.
      ButtonGroup group = new ButtonGroup();
      for (int i=0; i<colorRButtons.length; i++)
        group.add(colorRButtons[i]);

      //add ColorListener to radio buttons
      ColorListener listener = new ColorListener();
      for (int i=0; i<colorRButtons.length; i++)
           colorRButtons[i].addActionListener(listener);

      //primary panel contains all radio buttons
      primary = new JPanel(new GridLayout(5,1));
      for (int i=0; i<colorRButtons.length; i++)
           primary.add(colorRButtons[i]);
      buttonPanel = new JPanel(new GridLayout(2,1));
      buttonPanel.add(undo);
      buttonPanel.add(erase);
    
      leftPanel = new JPanel(new GridLayout(2,1));
      leftPanel.add(primary);
      leftPanel.add(buttonPanel);

      //canvas panel is where rectangles will be drawn, thus
      //it will be listening to a mouse.
      canvas = new CanvasPanel();
      canvas.setBackground(Color.white);
      canvas.addMouseListener(new PointListener());
      canvas.addMouseMotionListener(new PointListener());

      JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvas);

      setLayout(new BorderLayout());
      add(sp);
    }

   //ButtonListener class that takes action depending on what button is
   //pushed
   private class ButtonListener implements ActionListener
    {
      public void actionPerformed (ActionEvent event)
      {
          if (event.getSource() == undo)
          {
              if (rectList.size() > 0)
              {
              rectList.remove(rectList.size()-1);
              mouseDragged = false;
              canvas.repaint();
              }
          }
          if (event.getSource() == erase)
          {
              if (rectList.size() > 0)
              {
              rectList.clear();
              mouseDragged = false;
              canvas.repaint();
              }
          }
      }
   } // end of ButtonListener

   // listener class to set the color chosen by a user using
   // the radio buttons.
   private class ColorListener implements ActionListener
    {
       public void actionPerformed(ActionEvent event)
       {
            if (event.getSource() == colorRButtons[0])
             currentColor = colors[0];
            else if (event.getSource() == colorRButtons[1])
             currentColor = colors[1];
            else if (event.getSource() == colorRButtons[2])
             currentColor = colors[2];
            else if (event.getSource() == colorRButtons[3])
             currentColor = colors[3];
            else if (event.getSource() == colorRButtons[4])
             currentColor = colors[4];
         }
    }


//CanvasPanel is the panel where rectangles will be drawn
private class CanvasPanel extends JPanel
{
     //this method draws all rectangles specified by a user
   public void paintComponent(Graphics page)
      {
        super.paintComponent(page);

          //draw all rectangles
          for (int i=0; i < rectList.size(); i++)
               {
              ((Rect) rectList.get(i)).draw(page);
            }

          //draw an outline of the rectangle that is currently being drawn.
          if (mouseDragged == true)
           {
           page.setColor(currentColor);
           //Assume that a user will move a mouse only to left and down from
           //the first point that was pushed.
           page.drawRect(x1, y1, x3-x1, y3-y1);
           }
      }
    } //end of CanvasPanel class

   // listener class that listens to the mouse
   public class PointListener implements MouseListener, MouseMotionListener
    {
   //in case that a user presses using a mouse,
   //record the point where it was pressed.
     public void mousePressed (MouseEvent event)
      {
       x1 = event.getX();
         y1 = event.getY();
       //after "create" button is pushed.
      }

     //mouseReleased method takes the point where a mouse is released,
     //using the point and the pressed point to create a rectangle,
     //add it to the ArrayList "rectList", and call paintComponent method.
     public void mouseReleased (MouseEvent event)
      {
       x2 = event.getX();
       y2= event.getY();
       Rect rectangle = new Rect(x1, y1, (x2-x1), (y2-y1), currentColor);
       rectList.add(rectangle);
       canvas.repaint();
      }

     //mouseDragged method takes the point where a mouse is dragged
     //and call paintComponent method
   public void mouseDragged(MouseEvent event)
      {
       x3 = event.getX();
       y3= event.getY();
       mouseDragged = true;
         canvas.repaint();
      }

     public void mouseClicked (MouseEvent event) {}
     public void mouseEntered (MouseEvent event) {}
     public void mouseExited (MouseEvent event) {}
     public void mouseMoved(MouseEvent event) {}

    } // end of PointListener

} // end of Whole Panel Class

////////////////////////////////////////////////         Assignment7.java////////////////////////////

import javax.swing.*;

public class Assignment7 extends JApplet
{
public void init()
{
// create a WholePanel object and add it to the applet
WholePanel wholePanel = new WholePanel();
getContentPane().add(wholePanel);

//set applet size to 400 X 400
setSize (400, 400);
}
}

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