-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePanel.java
218 lines (163 loc) · 6.7 KB
/
GamePanel.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
206
207
208
209
210
211
212
213
214
215
216
217
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pong;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgproc.Moments;
/**
*
* @author Max
*/
public class GamePanel extends JPanel implements ActionListener, KeyListener {
VideoCapture webSource = new VideoCapture(0);
int LowH = 0;
int LowS = 0;
int LowV = 0;
int HighH = 179;
int HighS = 255;
int HighV = 255;
int LowH1 = 0;
int LowS1 = 0;
int LowV1 = 0;
int HighH1 = 179;
int HighS1 = 255;
int HighV1 = 255;
double iLastX=-1;
double iLastY=-1;
double iLastX1=-1;
double iLastY1=-1;
Mat frame = new Mat();
Mat prueba = new Mat();
Mat frame1 = new Mat();
Mat frame2 = new Mat();
Mat frame3 = new Mat();
MatOfByte mem = new MatOfByte();
MatOfByte mem1 = new MatOfByte();
MatOfByte mem2 = new MatOfByte();
MatOfByte mem3 = new MatOfByte();
Player player = new Player();
Player2 player2 = new Player2();
Ball ball = new Ball();
public GamePanel() {
Timer time = new Timer(50, this);
time.start();
this.addKeyListener(this);
this.setFocusable(true);
}
private void update() {
player.update();
ball.update();
player2.update();
ball.checkCollisionWith(player);
ball.checkCollisionWith(player2);
ball.hitWall();
detection();
}
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, Pong.WINDOW_WIDTH, Pong.WINDOW_HEIGHT);
player.paint(g);
player2.paint(g);
ball.paint(g);
g.setColor(Color.blue);
g.drawLine(0, 30, Pong.WINDOW_WIDTH, 30);
g.drawLine(Pong.WINDOW_WIDTH / 2, 30, Pong.WINDOW_WIDTH / 2, Pong.WINDOW_HEIGHT);
g.drawOval((Pong.WINDOW_WIDTH / 2) - 30, Pong.WINDOW_HEIGHT / 2 - 30, 60, 60);
g.setColor(Color.yellow);
}
public Ball getBall() {
return ball;
}
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
player.setYVelocity(-5);
if (player.getY() < 30) {
player.setYVelocity(0);
}
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
player.setYVelocity(5);
if (player.getY() + 40 > Pong.WINDOW_HEIGHT - 28) {
player.setYVelocity(0);
}
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_DOWN) {
player.setYVelocity(0);
}
}
public void keyTyped(KeyEvent e) {
}
public void detection(){
if (webSource.grab()) {
try {
webSource.retrieve(frame);
Imgproc.cvtColor(frame,prueba,Imgproc.COLOR_RGB2HSV);
Core.inRange(prueba,new Scalar(4,159,64),new Scalar(16,255,140),frame1);
Core.inRange(prueba,new Scalar(102,119,126),new Scalar(109,161,255),frame2);
/*
System.out.println("lh"+LowH1);
System.out.println("ls"+LowS1);
System.out.println("lv"+LowV1);
System.out.println("hh"+HighH1);
System.out.println("hs"+HighS1);
System.out.println("hv"+HighV1);
*/
Moments oMoments = Imgproc.moments(frame1);
double dM01 = oMoments.get_m01();
double dM10 = oMoments.get_m10();
double dArea = oMoments.get_m00();
if(dArea > 1000)
{
double posX = dM10 / dArea;
double posY = dM01 / dArea;
if (iLastX >= 0 && iLastY >=0 && posX >=0 && posY >=0)
{
iLastX=Math.round(iLastX);
iLastY=Math.round(iLastY);
}
iLastX = posX;
iLastY = posY;
player.setYVelocity((int)(posY*0.63));
}
Moments oMoments1 = Imgproc.moments(frame2);
double d1M01 = oMoments1.get_m01();
double d1M10 = oMoments1.get_m10();
double d1Area = oMoments1.get_m00();
if(d1Area > 1000)
{
double posX1 = d1M10 / d1Area;
double posY1 = d1M01 / d1Area;
if (iLastX1 >= 0 && iLastY1 >=0 && posX1 >=0 && posY1 >=0)
{
iLastX1=Math.round(iLastX1);
iLastY1=Math.round(iLastY1);
}
iLastX1 = posX1;
iLastY1 = posY1;
player2.setYVelocity((int)(posY1*0.63));
}
}
catch (Exception ex) {
System.out.println("Error");
}
}}}