Rect class The Rect class represents a rectangle to be drawn in the panel. It co
ID: 3553557 • Letter: R
Question
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 has the following 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 also contains 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(this is a private class inside WholePanel 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 page you can draw rectangle with a selected color. This can be done by calling the draw method. 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.
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
Explanation / Answer
Find the code here : http://ideone.com/a8NdKK
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.