-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawPanel.java
More file actions
184 lines (153 loc) · 6.54 KB
/
DrawPanel.java
File metadata and controls
184 lines (153 loc) · 6.54 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
package twodimensionaldrawingapplication;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DrawPanel extends JPanel {
private JLabel statusBar; //status label to display current x,y coordinates of mouse
private ArrayList<MyShape> shapeList = new ArrayList<MyShape>(); //array list to store created shapes
private String myShape = ""; //initializing myShape to avoid errors
int x1;
int y1;
int x2;
int y2;
private Color firstColor;
private Color secondColor;
private int lineWidth;
private boolean dashed = false; //initializing dashed to avoid errors
private float dashLength = 1; //initializing dash length to avoid errors
private boolean gradient = false; //initializing gradient to avoid errors
private boolean filled = false; //initializing filled to avoid errors
private MyShape myCurrentShape; //MyShape object to store the new shape created every time
//constructor
public DrawPanel() {
setPreferredSize(new Dimension(600, 450));
setBackground(Color.WHITE);
setLayout(new BorderLayout());
statusBar = new JLabel("Mouse outside panel");
statusBar.setSize(new Dimension(100, 20));
add(statusBar, BorderLayout.PAGE_END);
MouseHandler mouseHandler = new MouseHandler();
addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
}
public void setMyShape(String myShape) {
this.myShape = myShape;
}
public String getMyShape() {
return myShape;
}
public Color getFirstColor() {
return firstColor;
}
public void setFirstColor(Color firstColor) {
this.firstColor = firstColor;
}
public Color getSecondColor() {
return secondColor;
}
public void setSecondColor(Color secondColor) {
this.secondColor = secondColor;
}
public int getLineWidth() {
return lineWidth;
}
public void setLineWidth(int lineWidth) {
this.lineWidth = lineWidth;
}
public void setDashed(boolean dashed) {
this.dashed = dashed;
}
public boolean isDashed() {
return dashed;
}
public void clearPanel() { //function to clear array list in order to clear draw panel
shapeList.clear(); //clear array points
repaint(); //repaint the draw panel to be clear
}
public void undo() { //function to undo last shape drawn on panel
if (shapeList.isEmpty() == false) {
shapeList.remove(shapeList.size() - 1); //deleting last element from the shape array list
repaint(); //repainting the draw panel to remove the last shape from panel
}
else { //nothing to be undone
JOptionPane.showMessageDialog(null, "There is nothing to be undone", "Button Error", JOptionPane.ERROR_MESSAGE);
}
}
public boolean isGradient() {
return gradient;
}
public void setGradient(boolean gradient) {
this.gradient = gradient;
}
public void setDashLength(float dashLength) {
this.dashLength = dashLength;
}
public float getDashLength() {
return dashLength;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public boolean isFilled() {
return filled;
}
//Private inner class MouseHandler to handle mouse actions on draw panel
private class MouseHandler implements MouseMotionListener, MouseListener {
@Override
public void mouseDragged(MouseEvent event) {
statusBar.setText("(" + event.getX() + "," + event.getY() + ")"); //setting status bar to display coordinates of mouse
shapeList.get(shapeList.size() - 1).setX2(event.getX()); //setting x2 position of current shape
shapeList.get(shapeList.size() - 1).setY2(event.getY()); //setting y2 position of current shape
repaint();
}
@Override
public void mousePressed(MouseEvent event) {
//creating shape objects to be drawn based off users combo box choice
if (getMyShape().equals("Line")) {
myCurrentShape = new MyLine(event.getX(), event.getY(), event.getX(), event.getY(), getFirstColor(), getSecondColor(), getLineWidth(), isGradient(), isDashed(), getDashLength());
}
else if (getMyShape().equals("Rectangle")) {
myCurrentShape = new MyRect(event.getX(), event.getY(), event.getX(), event.getY(), getFirstColor(), getSecondColor(), getLineWidth(), isGradient(), isDashed(), getDashLength(), isFilled());
}
else if (getMyShape().equals("Oval")) {
myCurrentShape = new MyOval(event.getX(), event.getY(), event.getX(), event.getY(), getFirstColor(), getSecondColor(), getLineWidth(), isGradient(), isDashed(), getDashLength(), isFilled());
}
else {
JOptionPane.showMessageDialog(null, "You must select a shape to draw", "Draw Error", JOptionPane.ERROR_MESSAGE);
}
if (myCurrentShape != null) { //if a shape has been selected and user starts to draw it add it to the array list
shapeList.add(myCurrentShape);
}
}
@Override
public void mouseReleased(MouseEvent event) {
shapeList.get(shapeList.size() - 1).setX2(event.getX()); //setting x2 and y2 coordinates of final position
shapeList.get(shapeList.size() - 1).setY2(event.getY());
myCurrentShape = null;
repaint();
}
@Override
public void mouseEntered(MouseEvent event) {
}
@Override
public void mouseExited(MouseEvent event) {
statusBar.setText("Mouse outside panel");
}
@Override
public void mouseClicked(MouseEvent event) {
}
@Override
public void mouseMoved(MouseEvent event) {
statusBar.setText("(" + event.getX() + "," + event.getY() + ")");
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (MyShape myShape : shapeList) { //enhanced for loop to draw all shape objects created by user
myShape.draw(g2d);
}
}
}