-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaintPanel.java
More file actions
211 lines (186 loc) · 7.86 KB
/
PaintPanel.java
File metadata and controls
211 lines (186 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Name: Thomas Cheng
*
* Date: July 22, 2018
*
* Dsscription: This is the PaintPanel class that will draw all the shapes.
*/
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.GradientPaint;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PaintPanel extends JPanel {
// instance variables
private LinkedList<Shape> shapeList;
private DynamicStack<Shape> redoStack;
private JLabel statusLabel;
private Shape currentShape = null;
private int shapeType;
private Color colourOne;
private Color colourTwo;
private boolean isFilled;
private boolean isGradient;
private double lineSize;
private boolean isDashLine;
private double dashLineLength;
/* This is the constructor method for the class. This method accepts 1 parameter: a reference to a JLabel object.
*/
public PaintPanel( JLabel statusLabel ) {
super();
setLayout( new BorderLayout() );
shapeList = new LinkedList<Shape>();
redoStack = new DynamicStack<Shape>();
this.statusLabel = statusLabel;
// Create and register listener for mouse and mouse motion events
MouseEventListener drawFrameListener = new MouseEventListener();
addMouseListener( drawFrameListener );
addMouseMotionListener( drawFrameListener );
}
/* This inner class handles mouse events.
*/
class MouseEventListener extends MouseAdapter {
/* This method checks for a mouse press to indicate a new shape has been started. This method accepts 1
* parameter: a reference to a MouseEvent object. This method returns no values.
*/
@Override
public void mousePressed( MouseEvent event ) {
// draws the specificed shape
// draw a line
if (currentShape == null){
if ( shapeType == 0 ){
currentShape = new Line( event.getX(), event.getY(), event.getX(), event.getY(), colourOne,
colourTwo, isGradient, lineSize );
}
// draw a rectangle
else if ( shapeType == 1 ){
currentShape = new Rectangle( event.getX(), event.getY(), event.getX(), event.getY(),
colourOne, colourTwo, isGradient, isFilled, lineSize );
}
// draw an oval
else if ( shapeType == 2 ){
currentShape = new Oval( event.getX(), event.getY(), event.getX(), event.getY(),
colourOne, colourTwo, isGradient, isFilled, lineSize);
}
// Tell JVM to call paintComponent( g )
repaint();
}
}
/* This method checks for a mouse release to indicate the shape has been finished. This method accepts 1
* parameter: a reference to a MouseEvent object. This method returns no values.
*/
@Override
public void mouseReleased( MouseEvent event ) {
// Update ending coordinates
if (currentShape != null){
currentShape.setX2( event.getX() );
currentShape.setY2( event.getY() );
currentShape.setColourOne( colourOne );
// add the new shape to the END of the LinkedList
shapeList.addLast( currentShape );
// Get ready for the next line to be drawn
currentShape = null;
repaint();
// clear the redo stack
redoStack.clear();
}
}
/* This method updates the ending coordinates of currentShape and statusLabel as the mouse is dragged.
* This method accepts 1 parameter: a reference to a MouseEvent object. This method returns no values.
*/
@Override
public void mouseDragged( MouseEvent event ) {
if (currentShape != null){
currentShape.setX2( event.getX() );
currentShape.setY2( event.getY() );
statusLabel.setText( String.format( "(%d, %d)", event.getX(), event.getY() ) );
repaint();
}
}
/* This method updates the statusLabel as the mouse is moved. This method accepts 1 parameter: a reference to
* a MouseEvent object. This method returns no values.
*/
@Override
public void mouseMoved( MouseEvent event ) {
statusLabel.setText( String.format( "(%d, %d)", event.getX(), event.getY() ) );
}
}
/* This method is called automatically by the JVM when the window needs to be (re)drawn. This methid accepts 1
* parameter: a reference to a Graphics object. This method returns no values.
*/
@Override
public void paintComponent( Graphics g ) {
super.paintComponent( g );
removeAll();
// Call the draw() method for each Line object in the array
for ( int index = 0; index < shapeList.size(); index++ ){
Shape shape = shapeList.removeFirst();
shape.draw( g );
shapeList.addLast( shape );
}
// If a line is in progress, draw it on top of all others
if ( currentShape != null )
currentShape.draw( g );
}
/* This method will remove the last element in shapeList if it isn't empty, push it to redoStack,
* and call repaint(). This method accepts and returns no values.
*/
public void undo() {
if ( !( shapeList.isEmpty() ) ){
redoStack.push( shapeList.removeLast() );
repaint();
}
}
/* This method will pop the first element in redoStack if it isn't empty, add it to the end of shapeList,
* and call repaint(). This method accepts and returns no values.
*/
public void redo() {
if ( !( redoStack.isEmpty() ) ){
shapeList.addLast( redoStack.pop() );
repaint();
}
}
/* This method will call the clear method for both shapeList and redoStack, and call repaint().
* This method accepts and returns no values.
*/
public void clear() {
shapeList.clear();
redoStack.clear();
repaint();
}
/* This is the mutator method for shapeType. This method accepts 1 parameter: an integer value representing the
* type of shape the user wants to draw. This method returns no values.
*/
public void setShapeType( int shapeType ) {
this.shapeType = shapeType;
}
/* This is the mutator method for currentColor. This method accepts 1 parameter: a reference to a Color object.
* This method returns no values.
*/
public void setColourOne( Color colour ) {
colourOne = colour;
}
public void setColourTwo( Color colour ) {
colourTwo = colour;
}
/* This is the mutator method for isFilled. This method accepts 1 parameter: an boolean value representing whether
* the shape is filled or not. This method returns no values.
*/
public void setFill( boolean isFilled ) {
this.isFilled = isFilled;
}
public void setIsGradient( boolean isGradient ) {
this.isGradient = isGradient;
}
public void setLineSize( double lineSize ){
this.lineSize = lineSize;
}
public void setLineStyle( ) {
}
public void setDashLineLength( double dashLineLength ) {
this.dashLineLength = dashLineLength;
}
}