This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawPanel.java
205 lines (169 loc) · 5.78 KB
/
DrawPanel.java
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
/**
* Jaired Jawed
* Dec 10, 2017
* DrawPanel.java
*/
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;
import java.nio.file.FileAlreadyExistsException;
public class DrawPanel extends JPanel
{
private LinkedList<MyShape> myShapes; // dynamic stack of shapes
private LinkedList<MyShape> clearedShapes; // dynamic stack of cleared shapes from undo
// current shape variables
private String currentShapeType; // allowed values are "Line", "Rectangle", and "Oval"
private MyShape currentShapeObject; // stores the current Shape being used
private Color currentShapeColor; // current shape color
private boolean currentShapeFilled; // determines whether shape is filled or not
private JLabel statusLabel;
public DrawPanel()
{
myShapes = new LinkedList<MyShape>();
clearedShapes = new LinkedList<MyShape>();
// Initialize default Shape variables
currentShapeType = "Line";
currentShapeObject = null;
currentShapeColor = Color.BLACK;
currentShapeFilled = false;
statusLabel = new JLabel("");
setLayout(new BorderLayout());
setBackground(Color.WHITE);
add(statusLabel, BorderLayout.SOUTH);
MouseHandler handler = new MouseHandler();
addMouseListener(handler);
addMouseMotionListener(handler);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth(), height = getHeight();
ArrayList<MyShape> shapeArr = myShapes.getArray();
for (int counter = shapeArr.size() - 1; counter >= 0; counter --) {
shapeArr.get(counter).draw(g);
}
if (currentShapeObject != null) {
currentShapeObject.draw(g);
}
}
// clears the entire drawing
public void clearDrawing() {
myShapes.makeEmpty();
clearedShapes.makeEmpty();
repaint();
}
// called when undo menu item is clicked
public void clearLastShape() {
if (!myShapes.isEmpty()) {
clearedShapes.addFront(myShapes.removeFront());
repaint();
}
}
// called when redo menu item is clicked
public void redoLastShape() {
if (!clearedShapes.isEmpty()) {
myShapes.addFront(clearedShapes.removeFront());
repaint();
}
}
public void importImageToJPanel() {
try {
String fileName = JOptionPane.showInputDialog("Enter a file name:");
if (fileName.equals(null) || fileName.trim().equals("") ) {
throw new Exception("Please enter a file name!");
}
BufferedImage image = ImageIO.read(new File("./" + fileName + ".png"));
add(new JLabel(new ImageIcon(image)));
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "The file was not found!");
}
catch (IOException e) {
JOptionPane.showMessageDialog(null, "There was an error reading the file!");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void exportImageFromJPanel() {
try {
String fileName = JOptionPane.showInputDialog("Enter a file name:");
if (fileName.equals(null) || fileName.trim().equals("") ) {
throw new Exception("Please enter a file name!");
}
File file = new File("./" + fileName + ".png");
if (file.exists()) {
throw new FileAlreadyExistsException("File already exists.");
}
BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
paint(img.createGraphics());
ImageIO.write(img, "PNG", file);
}
catch (FileAlreadyExistsException e) {
JOptionPane.showMessageDialog(null, "This file already exists");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
/**
* Mutator Methods
*/
public void setCurrentShapeType(String type) {
currentShapeType = type;
}
public void setCurrentShapeColor(Color color) {
currentShapeColor = color;
}
public void setCurrentShapeFilled(boolean filled) {
currentShapeFilled = filled;
}
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
switch (currentShapeType) {
case "Line":
currentShapeObject = new MyLine( event.getX(), event.getY(),
event.getX(), event.getY(), currentShapeColor);
break;
case "Rectangle":
currentShapeObject = new MyRectangle( event.getX(), event.getY(),
event.getX(), event.getY(), currentShapeColor, currentShapeFilled);
break;
case "Oval":
currentShapeObject = new MyOval( event.getX(), event.getY(),
event.getX(), event.getY(), currentShapeColor, currentShapeFilled);
break;
}
}
public void mouseReleased(MouseEvent event)
{
currentShapeObject.setEndX(event.getX());
currentShapeObject.setEndY(event.getY());
myShapes.addFront(currentShapeObject);
currentShapeObject = null;
clearedShapes.makeEmpty();
repaint();
}
public void mouseMoved(MouseEvent event)
{
statusLabel.setText(String.format("Mouse Coordinates X: %d Y: %d", event.getX(), event.getY()));
}
public void mouseDragged(MouseEvent event)
{
currentShapeObject.setEndX(event.getX());
currentShapeObject.setEndY(event.getY());
statusLabel.setText(String.format("Mouse Coordinates X: %d Y: %d", event.getX(), event.getY()));
repaint();
}
}
}