Assignment7.java (The Assignment7 class extends JApplet) Line.java WholePanel.ja
ID: 3674730 • Letter: A
Question
Assignment7.java (The Assignment7 class extends JApplet)
Line.java
WholePanel.java (It extends JPanel. It needs to be completed.)
You may add more classes or more methods than they are specified. (You might need them.)
Skills to be Applied:
Swing/AWT
Classes may be needed:
JApplet, JButton, Container, JPanel, JRadioButton, ButtonGroup, JSplitPane, Color, Graphics, ActionListener, Point, MouseListener, and ArrayList. You may use other classes.
Program Description
Suggested Class Diagram:
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.
Click here to view the applet
Class description
Line class
The Line class represents a line to be drawn in the panel. It should contain at least the following instance variable:
Attribute name
Attribute type
Description
x1
int
x-coordinate of the starting point of the line to be drawn.
y1
int
y-coordinate of the starting point of the line to be drawn.
x2
int
x-coordinate of the ending point of the line to be drawn.
y2
int
x-coordinate of the ending point of the line to be drawn.
color
Color
color of the line.
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:
Attribute name
Attribute type
Description
lineList
ArrayList
A list of line objects that are drawn so far.
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.
How to get started:
Download the following files and use them as a base of your program:
Assignment7.java
WholePanel.java
Step 0: Define Line class in the Line.java file. There are only two methods in this class, and they are straight forward.
Step 1: Complete WholePanel.java file. Define the mousePressed method to get the point that a mouse is pointing. Use the point where the mouse is pushed to get its (x,y)-coordinate.
Step 2: Define the mouseDragged method. At this point, you will have two coordinate (x1,y1) and (x2,y2), one is a pressed point and the other is a dragged point. Using these two points, draw a line in the paintComponent method, then again, call it by calling repaint() method using an object of the class that has the paintComponent method.
Step 3: Define the mouseReleased method. At this point, you will have the third coordinate (x3, y3), the point where a mouse is released. Instantiate an object of Line class, by passing its starting point (x1,y1), its ending point (x3,y3), and its color. At this point, the color is just black by default.
Since there will be many lines created, we can use an ArrayList called "lineList" to add an object of Line.
Step 4: Define the paintComponent method to draw all lines in the lineList. You need a loop to do this.
Step 5: Add the "Undo" and "Erase" buttons, and create ButtonListener class to implement actionPerformed method. "Undo" will delete the last line, so you need to do something with the lineList and re-draw. "Erase" will delete all lines.
Step 6: Add the JRadioButtons to select colors and also create ColorListener to implement actionPerformed method.
More steps: These are just skeletons. You need to adjust your code to make it work cleanly.
Attribute name
Attribute type
Description
x1
int
x-coordinate of the starting point of the line to be drawn.
y1
int
y-coordinate of the starting point of the line to be drawn.
x2
int
x-coordinate of the ending point of the line to be drawn.
y2
int
x-coordinate of the ending point of the line to be drawn.
color
Color
color of the line.
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.