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:

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.

Grading Policy:

  • submit assignment on time
  • indicate assignment number, name, lecture number, and description of each class clearly in each submitted java file
  • your program minimally has the following functionalities:
    1. 1 point: JRadioButtons, "Undo" and "Erase" buttons appear properly at the left side of the applet.
    2. 1 point: All drawn lines are shown in the applet. (previously drawn lines should not disappear.)
    3. 2 points: A line should appear based on the points where a user pressed a mouse.
    4. 1 point: A line is drawn with a selected color.
    5. 1 point: "Erase" button erases all lines.
    6. 2 points: "Undo" button erases the last drawn line. If the last action was to push "Erase" button, then all erased lines should come back in the drawing panel.

 

 

 

 

 

    • 8 years ago
    Assignment Completed with A+ Quality..
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      assignment7.zip