-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
149 lines (126 loc) · 4.99 KB
/
Copy pathMain.java
File metadata and controls
149 lines (126 loc) · 4.99 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
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
class MyPanel extends JPanel {
Shape currentShape;
private Point startPoint;
public MyPanel() {
setBackground(Color.WHITE);
currentShape = createShape(Ui.currentShape);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
currentShape.setCoordinates(startPoint.x, startPoint.y, startPoint.x, startPoint.y);
currentShape.setColor(Ui.currentColor);
currentShape.setFilled(Ui.currentFilled);
handleMouseClick(e.getPoint()); // Handle UI element clicks
}
@Override
public void mouseReleased(MouseEvent e) {
currentShape.setCoordinates(startPoint.x, startPoint.y, e.getX(), e.getY());
Shape.history.add(currentShape);
repaint();
currentShape = createShape(Ui.currentShape);
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (("FreeHand".equals(Ui.currentShape)) || ("Eraser".equals(Ui.currentShape))) {
currentShape.setCoordinates(startPoint.x, startPoint.y, e.getX(), e.getY());
repaint();
Shape.history.add(currentShape);
currentShape = createShape(Ui.currentShape);
startPoint = e.getPoint();
}else{
currentShape.setCoordinates(startPoint.x, startPoint.y, e.getX(), e.getY());
repaint();
}
}
});
}
private Shape createShape(String shapeType) {
return switch (shapeType) {
case "Oval" -> new Oval();
case "Rectangle" -> new Rectangle();
case "Line" -> new Line();
case "FreeHand" -> new FreeHand();
case "Eraser" -> new Eraser();
default -> new Line();
};
}
private boolean isWithinBounds(Point point, int x1, int y1, int x2, int y2) {
return point.x >= x1 && point.x <= x2 && point.y >= y1 && point.y <= y2;
}
private void handleMouseClick(Point point) {
// Check if the click is within the "Fill Shape" button
if (isWithinBounds(point, 650, 360, 670, 380)) {
Ui.currentFilled = !Ui.currentFilled;
}
// Check if the click is within the "Red Color" circle
else if (isWithinBounds(point, 30, 400, 80, 450)) {
Ui.currentColor = Color.RED;
}
// Check if the click is within the "Blue Color" circle
else if (isWithinBounds(point, 110, 400, 160, 450)) {
Ui.currentColor = Color.BLUE;
}
// Check if the click is within the "Green Color" circle
else if (isWithinBounds(point, 190, 400, 240, 450)) {
Ui.currentColor = Color.GREEN;
}
// Check if the click is within the "Rectangle" shape button
else if (isWithinBounds(point, 290, 400, 360, 450)) {
Ui.currentShape = "Rectangle";
}
// Check if the click is within the "Oval" shape button
else if (isWithinBounds(point, 380, 400, 450, 450)) {
Ui.currentShape = "Oval";
}
// Check if the click is within the "Line" shape button
else if (isWithinBounds(point, 470, 400, 500, 450)) {
Ui.currentShape = "Line";
}
// Check if the click is within the "FreeHand" button
else if (isWithinBounds(point, 530, 360, 600, 380)) {
Ui.currentShape = "FreeHand";
}
// Check if the click is within the "Eraser" button
else if (isWithinBounds(point, 530, 400, 600, 420)) {
Ui.currentShape = "Eraser";
}
// Check if the click is within the "Clear" button
else if (isWithinBounds(point, 530, 440, 600, 460)) {
Graphics g = getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 800, 350);
Shape.history.add(new Rectangle(0, 0, 800, 350, Color.WHITE, true));
}
repaint(); // Refresh the UI
}
@Override
public void paint(Graphics g) {
super.paint(g);
new Ui(g);
for (Shape s : currentShape.history){
s.draw(g);
}
currentShape.draw(g);
String colorName = Ui.getColorName(Ui.currentColor);
g.drawString(colorName, 15, 15);
g.drawString(Ui.currentShape, 15, 30);
}
}
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new MyPanel());
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(800, 500);
frame.setVisible(true);
}
}