/* * http://www.javamex.com/tutorials/threads/ * Game 6 * A continuation of the game series. * This allows for automation and smooth * animation of sprite * @author JasonButka * * */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; import java.awt.image.*; import java.net.*; import java.awt.event.*; public class GameSixThreads extends Applet implements Runnable, KeyListener { static int SCREENWIDTH = 450; static int SCREENHEIGHT = 240; int keyCode; char keyChar; boolean left; PlayerSixThreads joe; PlayerSixThreads kyle; Puck6Threads puck; boolean shoot = false; Thread animationLoop; Thread thr1, thr2; Runnable r1, r2; //double buffer objects Image backbuffer; Graphics2D g2d; Image background; //sprite variables //animation variables Image dbImage; // double buffering technique Graphics dbg; // double buffering technique // find & load image private int fx; private int fy; private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch (Exception e) { // do nothing } return url; } public void init() {//OPEN init() addKeyListener(this); left=false; this.setSize(SCREENWIDTH, SCREENHEIGHT); Toolkit tk = Toolkit.getDefaultToolkit(); puck = new Puck6Threads(120,120); puck.load(); joe = new PlayerSixThreads(100, 100); joe.load(); kyle = new PlayerSixThreads(200, 100); kyle.load(); //create the back buffer for smooth graphics System.out.println("HHH Threads 161"); backbuffer= tk.getImage(getURL("HockeyRink.png")); //load the animation strip }//CLOSE init() public void start() { if(left==false) { animationLoop = new Thread(this); animationLoop.start(); System.out.println("IN START"); } if (left==true) { thr1 = new Thread(this); thr1.start(); System.out.println("1 started"); } } public void stop() { if(left==true) { animationLoop = null; } else { thr1 = null; } } public void run() { Thread t = Thread.currentThread(); while (t == thr1){ try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); joe.shoot(); if(joe.runShoot==false) { left=false; t=null; start(); } repaint(); } while (t == animationLoop) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } repaint(); } } public void update(Graphics g) {// open update() if (dbImage == null) {//open db if // this size of the background dbImage = createImage (SCREENWIDTH, SCREENHEIGHT); dbg = dbImage.getGraphics (); }//close db if // clear screen in background dbg.setColor (getBackground ()); dbg.fillRect (0, 0, SCREENWIDTH, SCREENHEIGHT); dbg.setColor (getForeground()); paint (dbg); // draw image on the screen g.drawImage (dbImage, 0, 0, this); }// close update() public void paint(Graphics g) { //opens paint() //draw the back buffer to the screen g.drawImage(backbuffer, 0, 0, this); //Draw Player/Puck Objects joe.drawPlayer(g); kyle.drawPlayer(g); puck.drawPuck(g, 0); } //closes paint() @Override public void keyPressed(KeyEvent e){ keyCode=e.getKeyCode(); switch(keyCode) { case KeyEvent.VK_RIGHT: if(joe.bounds.intersects(puck.bounds)) { puck.forward(); puck.setPosessed(true); puck.setPosition(joe.playerX+20, joe.playerY+10); } if(joe.bounds.intersects(kyle.bounds)) {//OPENS if touched //nothing happens. a collision System.out.println("BOOM!"); joe.back(); }//CLOSES if touched if(!joe.bounds.intersects(kyle.bounds)) {//OPENS not touched joe.skateForward(); }//CLOSES not touched break; case KeyEvent.VK_LEFT: if(joe.bounds.intersects(kyle.bounds)); { //nothing happens. a collision } if(!joe.bounds.intersects(kyle.bounds)) { joe.back(); } break; case KeyEvent.VK_UP: if(joe.bounds.intersects(kyle.bounds)); { //nothing happens. a collision } if(!joe.bounds.intersects(kyle.bounds)) { joe.changeYUp(); } break; case KeyEvent.VK_DOWN: if(joe.bounds.intersects(kyle.bounds)); { //nothing happens. a collision } if(!joe.bounds.intersects(kyle.bounds)) { joe.changeYDown(); } break; case KeyEvent.VK_ENTER: left=true; //boolean for thread control start(); //method for thread control break; } repaint(); } @Override public void keyReleased(KeyEvent arg0) { joe.direction=0; } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } }