-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvasPanel.java
63 lines (57 loc) · 1.51 KB
/
CanvasPanel.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class CanvasPanel extends JPanel {
private Color col;
private int x1, y1, x2, y2;
private ArrayList<Pair> allCords;
public CanvasPanel() {
allCords = new ArrayList<>();
col = Color.BLACK;
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
allCords.add(new Pair(x1, y1, x2, y2, col));
repaint();
}
});
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!allCords.isEmpty()) {
for (Pair p : allCords){
g.setColor(p.getCol());
g.drawLine(p.getCords()[0], p.getCords()[1], p.getCords()[2], p.getCords()[3]);
}
}
}
void clearPanel() {
allCords = new ArrayList<>();
repaint();
}
void changeColor(Color col) {
this.col = col;
repaint();
}
}
class Pair {
private int[] cords;
private Color col;
Pair(int x1, int y1, int x2, int y2, Color col) {
cords = new int[] {x1, y1, x2, y2};
this.col = col;
}
Color getCol() {
return col;
}
int[] getCords() {
return cords;
}
}