help, Write the proper code in Java please thank you Assignment7.java file - Who
ID: 3796640 • Letter: H
Question
help,
Write the proper code in Java please thank you
Assignment7.java file -
WholePanel.java file -
Requirements to get full credits in Documentation
The assignment number, your name, student ID, lecture number, and a class/file description need to be included at the top of each file/class.
A description of each method is also needed.
Some additional comments inside of methods to explain code 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)
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, JComboBox, JSplitPane, Color, Graphics, ActionListener, and MouseListener. 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 two buttons Clear and Undo, and a JComboBox that a user can use to select a color for a rectangle.
JButtons and a JComboBox should be on the left hand side as shown below. The right hand side is an object of CanvasPanel that will be implementing. The background of the CanvasPanel should be while, and it should have 6 lines including 3 vertical lines and 3 horizontal lines to divide the CanvasPanel evenly into four rows and four columns and these lines will be creating grids with 16 rectangle slots.
To do this, you might want to compute the grid width and height as:
gridWidth = (int)(this.getSize().getWidth()/(4.0));
gridHeight = (int)(this.getSize().getHeight()/(4.0));
Note that this is done with the paintComponent( ) method of the CanvasPanel class. It should get its size which contains width or height and divide them by 4 to create 4 rows or columns. Also, since getWidth() or getHeight( ) will return a value of double, you need to cast them to int.
Then you can draw black lines as:
for (int i=1; i< 4; i++)
{
page.drawLine(0,gridHeight*i,(int)(getSize().getWidth()),gridHeight*i);
page.drawLine(gridWidth*i,0,gridWidth*i,(int)(getSize().getHeight()));
}
(The size of the applet here is approximately 400 X 400).
Then a user can use a mouse to press one of 16 slots. If a user presses somewhere within each slot, then such rectangle slot should be filled with a chosen color from the JComboBox (it should contains black, red, blue, green, and orange).
In the picture below, a user pressed somewhere in the slot second from the top and the second from left, thus, the rectangle slot became black.
A user can choose a different color (one of black, red, blue, green, and orange) from the JComboBox, and press another rectangle slot, and such rectangle slot needs to be filled with the chosen color at that time.
A user can choose another color from the JComboBox, and press a rectangle slot with filled slots to change to another color.
A user can push the Clear button to clear all filled slots back into the background color white.
If a user pushes the Und0 button after the Clear button is pushed, then all cleared rectangles need to come back.
Is a user pushes the Undo button, then the last drawn filled rectangle should disappear.
Class description
Rectangle class
The Rectangle class represents a rectangle. It contains the following instance variables.
Attribute name
Attribute type
Description
x
int
x-coordinate of the upper left corner of a rectangle
y
int
y-coordinate of the upper left corner of a rectangle
width
int
width of a rectangle
height
int
height of a rectangle
color
Color
color of a rectangle
It contains the following methods.
public Rectangle(int x1, int y1, int width1, int height1, Color color1)
The constructor of Rectangle should assign each instance variable's corresponding parameter value.
public void draw(Graphics page)
Using its specified color, x,y coordinate, width and height, it should draw a filled rectangle.
CanvasPanel class
The CanvasPanel class extends JPanel defined in javax.swing package. This is where lines and rectangles are drawn. Its background color is white. It must contain the following method.
public void paintComponent(Graphics page)
Using the parameter, the Graphics object, this method should draw 6 black lines and rectangles with their selected colors.
This method also needs to draw all rectangle objects stored in the rectList ArrayList if it contains any.
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
canvas
CanvasPanel
A panel where lines and rectangles are drawn
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, and another panel containing two buttons and one combo box.
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 (one of black, red, blue, green, and orange) by using the JComboBox is assigned as a color to be used to draw rectangles This listener is used together with JComboBox.
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, you need to define the action to be performed in case one of the Undo button is pushed or the Clear button is pushed. This listener is used with JButtons.
Note that, to make it listen to a certain button and distinguish them, you can do something like (undo is the variable name for JButton): public void actionPerformed(ActionEvent event)
{
if (event.getSource() == undo)
{
..
}
PointListener class
The PointListener class implements MouseListener interface.
It must implement the following method:
public void mousePressed(MouseEvent event)
When a user presses a mouse, that the rectangle slot where the pressed point is located should be filled with the selected color of the JComboBox.
You will need to compute the (x, y) coordinate of such rectangle slot and its width and height, then create an object of Rectangle class, and add it to the rectList (arrayList). Then you will need to call paintComponent method of the object of CanvasPanel to re-draw all rectangles (which is done by calling repaint() method).
Other methods from MouseListener can be left as blank.
Note that these four 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 1: Complete Rectangle class
Step 2: Complete the constructor of WholePanel by following its descriptions.
Step 3: Create ButtonListener class and ColorListener class that implements ActionListener by following their description.
Step 4: Create PointListener class that implements MouseListener so that rectangles can be drawn
Step 5: Define paintComponent method of CanvasPanel class so that something can be drawn in the panel. More steps: These are just skeletons. You need to adjust your code to make it work cleanly.
Attribute name
Attribute type
Description
x
int
x-coordinate of the upper left corner of a rectangle
y
int
y-coordinate of the upper left corner of a rectangle
width
int
width of a rectangle
height
int
height of a rectangle
color
Color
color of a rectangle
JApplet defined in javax.swing Rectangle xint y:int Assignment7 width: int height: int -whole Panel Whole Panel color: Color Rectangle( Color nt nt,in nt +init(): void +draw(Graphics): void Whole Panel -rectList ArrayList +actionPerformed (ActionEvent): void +actionPerformed(ActionEvent): void Arizona State University, Assignment7 CSE205, Spring 2017 JPanel defined in javax.swing Canvas Panel +Canvas Panel() paint Component(Graphics):void Point Listener +mousePressed (MouseEvent):void +mouse Released (MouseEvent) void +mouseClicked (MouseEvent):void +mouse Entered (MouseEvent):void +mouseExisted MouseEvent):voidExplanation / Answer
A)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.