Created
March 1, 2009 04:05
-
-
Save CodeOfficer/72201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment