forked from ezchen/Per6-Chen-Dong-WebcamArcade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawScreen.java
127 lines (99 loc) · 2.96 KB
/
DrawScreen.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
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Stack;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.Webcam;
public class DrawScreen implements Screen {
private WebcamPanel.Painter painter;
private RegionOfInterest ROI;
private Driver driver;
private Webcam webcam;
private WebcamPanel panel;
private boolean buttonPressed;
private ObjectTracker tracker;
private LinkedList<Rectangle> paint;
private Stack<Rectangle> redo;
public DrawScreen(Webcam webcam, WebcamPanel panel, WebcamPanel.Painter painter, Driver driver, ObjectTracker tracker) {
this.webcam = webcam;
this.panel = panel;
this.painter = painter;
this.driver = driver;
buttonPressed = false;
this.tracker = tracker;
this.ROI = tracker.getROI();
paint = new LinkedList<Rectangle>();
redo = new Stack<Rectangle>();
}
public void paintPanel(WebcamPanel panel, Graphics2D g2) {
}
public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {
if (painter != null) {
int pWidth = panel.getSize().width;
int pHeight = panel.getSize().height;
int iWidth = -image.getWidth(null); // Flips the image along y axis
int iHeight = image.getHeight(null);
g2.drawImage(image, (pWidth - iWidth) / 2, (pHeight - iHeight) / 2, iWidth, iHeight , null);
image.flush();
// draw the object tracker's bounds
g2.setPaint(Color.red);
g2.drawRect(ROI.getLeft(), ROI.getTop(), ROI.getSize(), ROI.getSize());
tracker.trackObject(image);
// draw the rectangles
drawRectangles(g2);
}
}
public void update() {
}
public void drawRectangles(Graphics2D g2) {
// Iterate through all of the rectangles and draw them
g2.setPaint(Color.gray);
for (Rectangle rect : paint) {
g2.fill(rect);
}
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch(keyCode) {
case KeyEvent.VK_Z:
undo(5);
break;
case KeyEvent.VK_R:
redo(5);
break;
case KeyEvent.VK_P:
System.out.println("DrawScreen: 'P' pressed; Entering PongScreen");
driver.getScreens().pop();
// driver.getScreens().push(new DrawScreen(webcam, panel, painter, driver, tracker));
break;
case KeyEvent.VK_S:
System.out.println("DrawScreen: 'S' pressed. Entering SetupScreen");
driver.getScreens().pop();
default:
int y = ROI.getTop();
int x = ROI.getLeft();
int size = ROI.getSize();
addRectangle(size,size,x,y);
break;
}
}
public void addRectangle(int height, int width, int x, int y) {
Rectangle rectangle = new Rectangle(x, y, width, height);
paint.push(rectangle);
System.out.println(rectangle);
System.out.println("Rectangle Added");
}
public void undo(int iterations) {
if (paint.size() > 0) {
redo.push(paint.pop());
}
}
public void redo(int iterations) {
if (!redo.empty()) {
paint.push(redo.pop());
}
}
}