
Board
By: a guest on
Oct 11th, 2012 | syntax:
Java | size: 1.36 KB | hits: 7 | expires: Never
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JPanel;
public class Board extends JPanel implements Runnable, KeyListener {
Balls balls = new Balls();
private Boolean shoot = false;
// ////////////////////////////////////////////////////CONSTRUCTOR
public Board() {
setFocusable(true);
Thread t = new Thread(this);
addKeyListener(this);
t.start();
}
// ///////////////////////////////////////////////////PAINT
public void paint(Graphics g) {
super.paint(g);
if (shoot) {
balls.draw(g);
}
}
// //////////////////////////////////////////////////KEY LSITENER
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
shoot = true;
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
// ///////////////////////////////////////////////////UPDATER
public void update() {
if (shoot) {
balls.move(this.getHeight(), this.getWidth());
}
}
// //////////////////////////////////////////////////MAIN THREAD
@Override
public void run() {
while (true) {
update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}