-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputer.java
More file actions
81 lines (53 loc) · 1.2 KB
/
Computer.java
File metadata and controls
81 lines (53 loc) · 1.2 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
package pong;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Computer extends JPanel {
Pong pon;
int x = 673;
int y = 250;
BufferedImage img;
private boolean move = true;
public Rectangle rectCom;
Ball ba;
public Computer(Pong pon, Ball ba) {
this.pon = pon;
this.ba = ba;
Timer time = new Timer(15, new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
});
time.start();
rectCom = new Rectangle(x, y, 20, 90);
getImage();
}
public void getImage() {
try {
img = ImageIO.read(getClass().getResourceAsStream("/player/player2.png"));
} catch(IOException e) {
e.printStackTrace();
}
}
public void update() {
if(ba.y < y) {
y-=3;
}
if(ba.y > y) {
y+=2;
}
rectCom.setBounds(x, y, 20, 90);
}
public void draw(Graphics2D g) {
g.setColor(Color.white);
g.drawImage(img, x, y, 20, 90, null);
}
}