diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 5b53aa4..22dede0 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -2,6 +2,8 @@ package de.teamteamteam.spacescooter; import java.awt.EventQueue; +import de.teamteamteam.spacescooter.entities.Player; +import de.teamteamteam.spacescooter.entities.TestEntity; import de.teamteamteam.spacescooter.gui.GameFrame; import de.teamteamteam.spacescooter.threads.PaintThread; import de.teamteamteam.spacescooter.threads.EntityUpdateThread; @@ -27,6 +29,10 @@ public class Main { } }); + //Whatever. + new TestEntity(); + new Player(); + //Initialize PaintThread PaintThread pt = new PaintThread(gf); pt.start(); diff --git a/src/de/teamteamteam/spacescooter/entities/Entity.java b/src/de/teamteamteam/spacescooter/entities/Entity.java index 596be9c..3957c36 100644 --- a/src/de/teamteamteam/spacescooter/entities/Entity.java +++ b/src/de/teamteamteam/spacescooter/entities/Entity.java @@ -1,9 +1,27 @@ package de.teamteamteam.spacescooter.entities; import java.awt.Graphics; +import java.util.ArrayList; public abstract class Entity { - + + public static ArrayList entities = null; + + /** + * We need to initialize the ArrayList, so the EntityUpdateThread won't beat us. + */ + static { + Entity.entities = new ArrayList(); + } + + /** + * Constructor. + * All entities are within a static array list for our convenience. + */ + public Entity() { + Entity.entities.add(this); + } + public abstract void update(); public abstract void paint(Graphics g); diff --git a/src/de/teamteamteam/spacescooter/entities/Player.java b/src/de/teamteamteam/spacescooter/entities/Player.java index c1f76d5..c3329b0 100644 --- a/src/de/teamteamteam/spacescooter/entities/Player.java +++ b/src/de/teamteamteam/spacescooter/entities/Player.java @@ -4,7 +4,6 @@ import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.IOException; -import java.io.InputStream; import javax.imageio.ImageIO; @@ -19,11 +18,7 @@ public class Player extends Entity { static { try { - InputStream res = Player.class.getClassLoader().getResourceAsStream("images/nyancat.png"); - if(res == null) { - System.out.println("Kein res :/"); - } - img = ImageIO.read(res); + img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/nyancat.png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -31,6 +26,7 @@ public class Player extends Entity { } public Player() { + super(); this.x = 200; this.y = 300; } @@ -38,16 +34,16 @@ public class Player extends Entity { @Override public void update() { if(Keyboard.isKeyDown(KeyEvent.VK_UP)) { - this.y--; + this.y -= 3; } if(Keyboard.isKeyDown(KeyEvent.VK_DOWN)) { - this.y++; + this.y += 3; } if(Keyboard.isKeyDown(KeyEvent.VK_LEFT)) { - this.x--; + this.x -= 3; } if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT)) { - this.x++; + this.x += 3; } } diff --git a/src/de/teamteamteam/spacescooter/gui/GameFrame.java b/src/de/teamteamteam/spacescooter/gui/GameFrame.java index e35ea44..bb7e3fd 100644 --- a/src/de/teamteamteam/spacescooter/gui/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/gui/GameFrame.java @@ -1,36 +1,25 @@ package de.teamteamteam.spacescooter.gui; import java.awt.Graphics; -import java.util.ArrayList; +import java.awt.image.BufferStrategy; import java.util.Iterator; import javax.swing.JFrame; import de.teamteamteam.spacescooter.controls.Keyboard; import de.teamteamteam.spacescooter.entities.Entity; -import de.teamteamteam.spacescooter.entities.Player; -import de.teamteamteam.spacescooter.entities.TestEntity; /** * The game will take place in this beautiful window. */ public class GameFrame extends JFrame { - private ArrayList entities; - + private static final long serialVersionUID = 1L; + public GameFrame() { super(); - this.entities = new ArrayList(); - - //TODO: Remove this! - this.entities.add(new TestEntity()); - this.entities.add(new Player()); } - - public ArrayList getEntityList() { - return this.entities; - } - + /** * Set up the GameFrame before showing it to the world. */ @@ -39,25 +28,40 @@ public class GameFrame extends JFrame { this.setSize(800, 600); this.setUndecorated(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); - - //Make sure we get the keyboard events. Use Keyboard.isKeyDown() to ask about keys status. + + // Make sure we get the keyboard events. Use Keyboard.isKeyDown() to ask + // about keys status. this.addKeyListener(new Keyboard()); - + this.setVisible(true); } - + /** - * Have the GameFrame repaint all the visible entities. + * The pain, do not underestimate it! + * @see http://content.gpwiki.org/index.php/Java:Tutorials:Double_Buffering for details. */ - @Override - public void paint(Graphics g) { - Iterator i = this.entities.iterator(); - while(i.hasNext()) { - Entity e = i.next(); - e.paint(g); + public void drawEntities() { + this.createBufferStrategy(2); + Graphics bufferedGraphics = null; + BufferStrategy bufferStrategy = this.getBufferStrategy(); + if (bufferStrategy == null) { + System.out.println("Mist"); } + try { + bufferedGraphics = bufferStrategy.getDrawGraphics(); + + //Now we can use bufferedGraphics to actually draw stuff ... + Iterator i = Entity.entities.iterator(); + while (i.hasNext()) { + Entity e = i.next(); + e.paint(bufferedGraphics); + } + } finally { + //We are done, dispose the pen and celebrate the result! + if (bufferedGraphics != null) bufferedGraphics.dispose(); + } + + bufferStrategy.show(); } - - - + } diff --git a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java index c8c1ff9..74b1b0f 100644 --- a/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java +++ b/src/de/teamteamteam/spacescooter/threads/EntityUpdateThread.java @@ -1,9 +1,7 @@ package de.teamteamteam.spacescooter.threads; -import java.util.ArrayList; import java.util.Iterator; - import de.teamteamteam.spacescooter.entities.Entity; import de.teamteamteam.spacescooter.gui.GameFrame; @@ -31,8 +29,7 @@ public class EntityUpdateThread extends Thread { } private void updateEntities() { - ArrayList entityList = this.gf.getEntityList(); - Iterator i = entityList.iterator(); + Iterator i = Entity.entities.iterator(); while(i.hasNext()) { Entity e = i.next(); e.update(); diff --git a/src/de/teamteamteam/spacescooter/threads/PaintThread.java b/src/de/teamteamteam/spacescooter/threads/PaintThread.java index 546edc9..a8d5cb8 100644 --- a/src/de/teamteamteam/spacescooter/threads/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/threads/PaintThread.java @@ -1,7 +1,12 @@ package de.teamteamteam.spacescooter.threads; +import java.awt.EventQueue; + import de.teamteamteam.spacescooter.gui.GameFrame; +/** + * This thread triggers about 60 redraws per second. + */ public class PaintThread extends Thread { private GameFrame gf; @@ -11,13 +16,19 @@ public class PaintThread extends Thread { } public void run() { + final GameFrame gf = this.gf; // :'-( while (true) { try { Thread.sleep(16); } catch (InterruptedException e) { System.err.println(e); } - this.gf.repaint(); + //Trigger redrawing the things. Important: AWT-Context needed here! + EventQueue.invokeLater(new Runnable() { + public void run() { + gf.drawEntities(); + } + }); } } }