Assignment 7

Assignment 7
Assignment # 7 – CSE 205

Due: Tuesday March 23rd by 8:00PM

Requirements to get full credits in Documentation

The assignment number, your name, StudentID need to be included at the top of each file/class.
An overview/purpose of each class needs to be described at the top of each class.
A description of each method is also needed.
Some additional comments inside of methods to explain codes that are hard to follow should be written.
You can look at the Java programs in the text book to see how comments are added to programs.

Minimal Submitted Files

You are required, but not limited, to turn in the following source files: (The Assignment7 class extends JApplet) (It extends JPanel) — to be completed.
You may add more classes or more methods as needed.

Skills to be Applied:


Some of the classes needed:
JApplet, JButton, Container, JPanel, JSplitPane, JComboBox, Color, Font, Graphics, ActionListener, KeyListener, and MouseListener. You may use other classes too.
How to run an applet program:

-Create an html file, say “hw7.html” with the following content:

<TITLE>Assignment 7 Applet</TITLE>

<applet code=”Assignment7.class” width=550 height=250>

-Compile your java program as usual.

-In a console, type:

appletviewer hw7.html

(instead of typing “java Assignment7”).

-In the TextPad, choose Tool->Run Java Applet or
press Ctrl-3 (press control key and 3 at the same time).

-In the jGrasp,
choose Run->Run as Applet.

Program Description

Suggested Class Diagram: (.ppt file is available for this figure)

Write a Java program that constructs an Applet.

The Applet (JApplet) of your program should contain two buttons “Undo”, “Erase” , a JRadioButton , and ButtonGroup where a user can select a color to draw a rectangle on a panel where all rectangles are drawn.

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

A user can move a mouse into the drawing area and drag the mouse to draw a rectangle. The default color is black, so the first time, the rectangle will appear in the drawing area with black color.

A user can continue drawing more rectangles. Note that the rectangles remain on the panel.

A user can choose another color from the radio Buttons located on the left corner. After selecting a color, a user can press somewhere in the drawing panel again, and draw another rectangle on the screen with the chosen color.

When a user pushes the “Undo” button, the last drawn rectangle will be erased from the drawing area. Thus if a user keeps pushing the “Undo” button, eventually all rectangles will be erased.

Class description

Rect class
The Rect class represents a rectangle to be drawn in the panel. It contains at least the following instance variable:

Attribute name
Attribute type
x-coordinate of the rectangle to be drawn.
y-coordinate of the rectangle to be drawn.
color of the rectangle
width of the rectangle
height of the rectangle
This class should have a constructor:

public Rect(int x1, int y1, int width, int height, Color color)

where the parameters x and y are (x,y) coordinate of where the rectangle is drawn, and color is the color of the rectangle.

This class should also contain the method:

public void draw (Graphics page)

In this method the fillRect is called by passing the x, y coordinates, the width and the height.

CanvasPanel class
The CanvasPanel class extends JPanel defined in javax.swing package. This is where rectangles 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, will draw rectangle with a selected color. This can be done by calling the draw method in the class as well. Remember that this method need to call paintComponent method defined in its parent class.

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

Attribute name
Attribute type
A list of rectangles drawn by the user.
This class should have a constructor:

public WholePanel()

This is where all components are arranged. Add as many instance variables as you need, and instantiate them in this constructor. One way is to instantiate a CanvasPanel and canvas panel is where rectangles will be drawn, thus it will be listening to mouse.

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, based on the “Undo” button or “Erase” the last drawn rectangle or all the rectangles should be erased. Note in case there is no rectangle drawn in the drawing panel, nothing will happen even when the “Undo” button is pushed. You should make sure that your program does not crash in this case. The “Erase” button it clears the list and repaint the canvas..

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 chosen by a user using JRadioButton is assigned as a color to be used to draw the rectangle.

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

public void mousePressed (MouseEvent event)
public void mouseReleased (MouseEvent event)

// 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, and call paintComponent method.

public void mouseDragged(MouseEvent event) //mouseDragged method takes the point where a mouse is dragged, and call paintComponent nethod

Other methods from MouseListener can be left as blank.
Note that these listener classes and CanvasPanel class are 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:

Step 1: Create PointListener class that implements MouseListener to get the point that a mouse is pointing. Use the point where the mouse is pushed instead of the fixed point (200,200) so that a rectangle can appear in different locations.

Step 2: Create Rect class so that each rectangle at a certain location can be stored as an object. Then create an ArrayList and store each Rect object in it. Rewrite paintComponent method so that it goes through each object in the ArrayList and draw them one by one.

Step 3: Add the “Undo” button, and create ButtonListener class to implement actionPerformed method. Also CanvasPanel needs to be created and move all functionality described so far for the WholePanel to CanvasPanel. If the”Undo” button is pushed, delete the last element of the ArrayList and re-draw.

Step 4: Then, create ColorListener class to change selected colors. The next rectangle will be drawn with the newly selected color.

Grading Policy:

submit assignment on time
indicate assignment number, name, lecture number, section id, email address, description of each class clearly in each submitted java file
your program minimally has the following functionalities:
1 point: “Undo” and “Erase” button, and JRadioButton appear properly on the applet.
2 points: The drawn rectangles are shown in the applet. (previous drawn rectangles should not disappear.)
1 point: The rectangle should appear at the same location as a mouse is pressed.
2 points: The rectangle is drawn with a selected color.
1 point: “Undo” button erases the last drawn letter.
1 point: “Erase” button erases all the rectangles

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

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


WholePanel Class

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

public class WholePanel extends JPanel
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;

public WholePanel()
//default color to draw rectangles is black

//create buttons

//create radio buttons for 5 colors
//at the bginning, black will be chosen by default

//store 5 colors in an array

//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++)

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

//primary panel contains all radiobuttons
primary = new JPanel(new GridLayout(5,1));
for (int i=0; i<colorRButtons.length; i++)

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

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

setLayout(new BorderLayout());

//ButtonListener defined actions to take in case “Create”,
//”Undo”, or “Erase” is chosed.
private class ButtonListener implements ActionListener
public void actionPerformed (ActionEvent event)

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

//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)
//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)
//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)


//mouseDragged method takes the point where a mouse is dragged
//and call paintComponent nethod
public void mouseDragged(MouseEvent event)


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

Leave a Reply

Your email address will not be published. Required fields are marked *