Skills to be Applied:

Swing/AWT

Classes may be needed:
JApplet, JButton, Container, JPanel, JComboBox, JSplitPane, Color, Graphics, ActionListener, and MouseListener. You may use other classes.

Program Description

Suggested Class Diagram: see image.

Write a Java program that constructs an Applet.

The Applet (JApplet) of your program should contain one buttons, "Undo", 3 JComboBoxes, and one JLabel at the top with a message of Choose Circle or Square, its size and its color. A user can choose Circle or Square

using a combo box, its size using a combo box, and its color using a combo box. In addition, a user can push the Undo button to erase the last drawn shape. Those components should be located at the top of the applet.

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

A user can choose to draw a circle or a square using the left most combo box. Until it is changed, the chose shape will not be changed. The default shape is circle. see image.

A user can choose a size using its combo box. Until the size is changed, any shape will be draw with that size as its diameter. The default size is 10 pixels. The size options should be: 10, 20, 30, 40, or 50. see image.

A user can choose a color using its combo box. Until the color is changed, any shape will be drawn with that color. The default color is black. The color options should be: black, red, blue, green, or orange. see image.

A user can move a mouse in the drawing area and press the mouse where he/she wants to draw a circle or a square. The point that a user clicks will be the center of a circle or a square, and selected shape with its selected size and color will be drawn. see image.

A user can continue to draw shapes. If two shapes are overlapped, the shape drawn later will be shown on the top. see image.

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

Class description

Shape class

The Shape class is an abstract class and represents a shape to be drawn in the panel. It should contain at least the following instance variable (they should be protected):

Attribute name, Attribute type, Description
x, int, x-coordinate of the upper left corner of the bounding rectangle of a shape to be drawn
y, int, y-coordinate of the upper left corner of the bounding rectangle of a shape to be drawn
width, int, width of the upper left corner of the bounding rectangle of a shape to be drawn
height, int, heightof the upper left corner of the bounding rectangle of a shape to be drawn
color, Color, color of the shape

This class should have a constructor:

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

where the parameters x1 and y1 are (x1,y1) coordinate of the upper left corner of the bounding rectangle of a shape to be drawn and width and height are the ones of the upper left corner of the bounding rectangle of a shape to be drawn, and color is the color of the shape. The constructor should assign each parameter value to its corresponding instance variable.

This class should also contain the following abstract method:

public abstract void draw(Graphics page);

This method will be defined its child classes, Circle and Square.

Circle class

The Circle class is a child class of the Shape class. It does not have any additional variables, but its constructor and draw method need to be defined.

The constructor has the following header:

public Circle(int x1, int y1, int diameter, Color color)

It should calls the constructor of its parent, and its height and width will be the diameter.

It should also define the draw method inherited from the parent by setting the color and draw the shape specified by its coordinate (x1,y1) and width (= height)

You can utilize its Graphics object parameter and fillOval method.

CanvasPanel class

The CanvasPanel class extends JPanel defined in javax.swing package. This is where shapes 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 shapes with their selected color, size, and coordinates. This can be done by calling the draw method of the Shape class. Remember that this method need to call paintComponent method defined in its parent class first. 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
shapeList, ArrayList, A list of shape (circle or square) 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, a button "Undo", a label, and three combo boxes to choose a shape, a size, and a color.

ComboListener class

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

public void actionPerformed(ActionEvent event)

In this method, any time one of three combo boxes are used, something should be updated. If shape combo box is used, then its chose shape should be recorded in a variable. If color combo box is used, then its chosen color should be recorded in a Color variable. If size combo box is used, then its chosen size should be recorded in a variable. This listener is used together with JComboBox. Note that, to make it listen to three combo boxes and distinguish them, you can do something like (colorCombo is the variable name for JComboBox to choose a color):

public void actionPerformed(ActionEvent event) {i
f (event.getSource() == colorCombo)
{
.. }

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" button is pushed. This listener is used with JButton.

PointListener class

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

public void mousePressed(MouseEvent event)

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

Other methods from MouseListener 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 Shape class in the Shape.java file, then define its child classes Circle and Square classes. They have only two methods in this class, and they are straight forward.

Step 1: Complete WholePanel.java file. Create its class level variables to store a selected shape (circle or square), a selected size, and a selected color, and set them to their default values. 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.

Since there will be many shapes created, within the mousePressed, we can use an ArrayList called "shapeList" to add an object of Circle or Square. It Circle is selected at that time, instantiate an object of Circle class using chosen size and color and add it to shapeList. It Square is selected at that time, instantiate an object of Square class using chosen size and color and add it to shapeList.

Step 2: Define the paintComponent method to draw all shapes in the shapeList. You need a loop to do this (and utilize Polymorphism).

Step 3: Create three combo boxes in the constructor of WholePanel, a button, and a label and organize its layout.

Step 4: Define ComboListener and implement actionPerformed method so that each selected item is reflected in the variables that you created in the step 1. Make sure that you add the listener object to each combo box so that they will listen to a user input.

Step 5: Add the "Undo" button, and create ButtonListener class to implement actionPerformed method. "Undo" will delete the last shape, so you need to do something with the shapeList and re-draw.

More steps: These are just skeletons. You need to adjust your code to make it work cleanly.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.