Assigment 7 2

Assigment 7 2
Assignment # 7 – CSE 205

Due: Tuesday March 23rd by 8:00PM
I needed done by today before eight pm at night so in 12 hours basically
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:

Assignment7.java (The Assignment7 class extends JApplet)
Rect.java
WholePanel.java (It extends JPanel) — to be completed.
You may add more classes or more methods as needed.

Skills to be Applied:

Swing/AWT

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:
——————————————————–

<HTML>
<HEAD>
<TITLE>Assignment 7 Applet</TITLE>
</HEAD>
<BODY>

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

</BODY>
</HTML>
——————————————————
-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
Description
x
int
x-coordinate of the rectangle to be drawn.
y
int
y-coordinate of the rectangle to be drawn.
color
Color
color of the rectangle
width
int
width of the rectangle
height
int
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
Description
rectList
ArrayList
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:
Assignment7.java

WholePanel.java

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

Leave a Reply

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