CodeOfficer (owner)

Revisions

gist: 72201 Download_button fork
public
Public Clone URL: git://gist.github.com/72201.git
Embed All Files: show embed
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
/*
NAME: Russell Jones
DATE: February 21, 2007
PROJECT: LinSprite
FILE: GamePanel.java
*/
 
package linsprite;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.swing.JPanel;
 
public class GamePanel extends JPanel implements
Runnable,
MouseListener, MouseMotionListener,
KeyListener {
 
private static final long serialVersionUID = 1L;
private static final int SCREEN_WIDTH = 800;
private static final int SCREEN_HEIGHT = 600;
private long startTime, elapsedTime, currentTime;
private boolean gameRunning = true;
private boolean debuggerOn = false;
private Point point = null;
private Thread thread;
 
private Image testImage;
private boolean imagesLoaded;
 
public GamePanel() {
setBackground(Color.white);
setMinimumSize(new Dimension (SCREEN_WIDTH, SCREEN_HEIGHT));
setPreferredSize(new Dimension (SCREEN_WIDTH, SCREEN_HEIGHT));
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setFocusable(true);
thread = new Thread(this);
thread.start();
}
 
public void loadImages() {
try {
testImage = ImageIO.read(new File("images/0-8-000.bmp"));
} catch (IOException e) {
e.printStackTrace();
}
imagesLoaded = true;
}
 
public void run() {
loadImages();
startTime = System.currentTimeMillis();
currentTime = startTime;
while (gameRunning) {
try {
Thread.sleep(20);
} catch (Exception ex) {}
repaint();
elapsedTime = System.currentTimeMillis() - currentTime;
currentTime += elapsedTime;
}
}
 
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
 
if (imagesLoaded) {
g.drawImage(testImage, getWidth()/2-testImage.getWidth(null), getHeight()/2-testImage.getHeight(null), null);
}
 
if (debuggerOn==true) {
g2d.drawString("startTime: " + Long.toString(startTime), 10, 20);
g2d.drawString("currentTime: " + Long.toString(currentTime), 10, 40);
g2d.drawString("elapsedTime: " + Long.toString(elapsedTime), 10, 60);
if (point != null)
g2d.drawString("x: " + point.x + " y: " + point.y, 10, 80);
}
 
}
 
public void mouseClicked(MouseEvent e) {
point = e.getPoint();
}
 
public void mouseEntered(MouseEvent e) {
}
 
public void mouseExited(MouseEvent e) {
}
 
public void mousePressed(MouseEvent e) {
}
 
public void mouseReleased(MouseEvent e) {
}
 
public void mouseDragged(MouseEvent e) {
point = e.getPoint();
}
 
public void mouseMoved(MouseEvent e) {
}
 
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_D) debuggerOn = (debuggerOn ? false : true);
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) System.exit(0);
}
 
public void keyReleased(KeyEvent e) {
}
 
public void keyTyped(KeyEvent e) {
}
 
}