From 9542131e4a52613db32e20a05fd4a3d1c01642ba Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 6 Nov 2014 17:34:20 +0100 Subject: [PATCH 1/2] Avoid creating a new anonymous Runnable each frame. Since the Runnable that is passed to the EventQueue does never change, it now is created in the constructor. This should avoid a lot of overhead. --- .../spacescooter/thread/PaintThread.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/thread/PaintThread.java b/src/de/teamteamteam/spacescooter/thread/PaintThread.java index 0adfd20..b8bc5ca 100644 --- a/src/de/teamteamteam/spacescooter/thread/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/thread/PaintThread.java @@ -5,23 +5,33 @@ import java.awt.EventQueue; import de.teamteamteam.spacescooter.gui.GameFrame; /** - * This thread triggers about 60 redraws per second. + * This thread triggers the redrawing on the GameFrame. */ public class PaintThread extends TimedThread { - private GameFrame gf; - + /** + * Runnable that is passed to the EventQueue for later invocation. + */ + private final Runnable paintRunnable; + + /** + * Constructor. Sets the name of the Thread and creates the paintRunnable. + */ public PaintThread(GameFrame gf) { - this.gf = gf; + final GameFrame gameFrame = gf; this.setName("PaintThread"); + this.paintRunnable = new Runnable() { + public void run() { + gameFrame.draw(); + } + }; } + /** + * The work method invoked by the TimingThread. + */ public void work() { //Trigger redrawing the things. Important: AWT-Context needed here! - EventQueue.invokeLater(new Runnable() { - public void run() { - gf.draw(); - } - }); + EventQueue.invokeLater(this.paintRunnable); } } From c4ab7c3530f6666de9a84d984a01b576c1f22e6b Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 6 Nov 2014 18:19:35 +0100 Subject: [PATCH 2/2] Replace LinkedList with List, so it is possible to change its implementation later - if needed. --- src/de/teamteamteam/spacescooter/background/item/Items.java | 4 ++-- src/de/teamteamteam/spacescooter/entity/HealthBar.java | 4 ++-- src/de/teamteamteam/spacescooter/entity/LivingEntity.java | 4 ++-- .../teamteamteam/spacescooter/entity/enemy/EnemyThree.java | 4 ++-- src/de/teamteamteam/spacescooter/screen/GameOverScreen.java | 6 +++--- .../teamteamteam/spacescooter/screen/GamePausedScreen.java | 6 +++--- src/de/teamteamteam/spacescooter/screen/GameScreen.java | 6 +++--- src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java | 6 +++--- src/de/teamteamteam/spacescooter/screen/Screen.java | 5 +++-- 9 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/background/item/Items.java b/src/de/teamteamteam/spacescooter/background/item/Items.java index 7e916c2..627f91a 100644 --- a/src/de/teamteamteam/spacescooter/background/item/Items.java +++ b/src/de/teamteamteam/spacescooter/background/item/Items.java @@ -1,6 +1,6 @@ package de.teamteamteam.spacescooter.background.item; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.entity.Entity; import de.teamteamteam.spacescooter.entity.LivingEntity; @@ -20,7 +20,7 @@ public abstract class Items extends LivingEntity{ this.remove(); }; if(!this.isAlive()){ - LinkedList entities = Screen.currentScreen.getEntities(); + List entities = Screen.currentScreen.getEntities(); for (Entity e : entities) { if(e instanceof Player){ itemCollected((Player) e); diff --git a/src/de/teamteamteam/spacescooter/entity/HealthBar.java b/src/de/teamteamteam/spacescooter/entity/HealthBar.java index 6999602..53144f0 100644 --- a/src/de/teamteamteam/spacescooter/entity/HealthBar.java +++ b/src/de/teamteamteam/spacescooter/entity/HealthBar.java @@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity; import java.awt.Color; import java.awt.Graphics2D; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.screen.Screen; @@ -18,7 +18,7 @@ public class HealthBar extends Entity { } public void paint(Graphics2D g) { - LinkedList entities = Screen.currentScreen.getEntities(); + List entities = Screen.currentScreen.getEntities(); for (Entity e : entities) { if(e instanceof Player){ this.width = ((Player) e).getHealthPoints(); diff --git a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java index 91050d2..058e19e 100644 --- a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java @@ -1,7 +1,7 @@ package de.teamteamteam.spacescooter.entity; import java.awt.Rectangle; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.background.item.Items; import de.teamteamteam.spacescooter.entity.enemy.Enemy; @@ -25,7 +25,7 @@ public abstract class LivingEntity extends Entity implements Collidable { public void update() { if(!(this instanceof ShootingEntity)) return; //Only check collisions for ShootingEntity. - LinkedList entities = Screen.currentScreen.getEntities(); + List entities = Screen.currentScreen.getEntities(); for (Entity e : entities) { if (e.equals(this)) //Do not collide with myself! continue; diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java index 05c2646..b01d0cb 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java @@ -1,7 +1,7 @@ package de.teamteamteam.spacescooter.entity.enemy; import java.util.Iterator; -import java.util.LinkedList; +import java.util.List; import java.util.Random; import de.teamteamteam.spacescooter.background.item.Items; @@ -41,7 +41,7 @@ public class EnemyThree extends Enemy{ if(random.nextInt(10) < 5) Items.create(getX(), getY()); new EnemyThree(0, 0); } - LinkedList list = Screen.currentScreen.getEntities(); + List list = Screen.currentScreen.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { Entity entity = i.next(); diff --git a/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java b/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java index 0665b1f..8c3bd6d 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameOverScreen.java @@ -6,7 +6,7 @@ import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.Iterator; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.control.Keyboard; import de.teamteamteam.spacescooter.entity.Entity; @@ -38,7 +38,7 @@ public class GameOverScreen extends Screen { @Override protected void paint(Graphics2D g) { g.drawImage(this.img, 0, 0, null); - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().paint(g); @@ -54,7 +54,7 @@ public class GameOverScreen extends Screen { @Override protected void update() { - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().update(); diff --git a/src/de/teamteamteam/spacescooter/screen/GamePausedScreen.java b/src/de/teamteamteam/spacescooter/screen/GamePausedScreen.java index 1cf3100..3f836cb 100644 --- a/src/de/teamteamteam/spacescooter/screen/GamePausedScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GamePausedScreen.java @@ -6,7 +6,7 @@ import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.Iterator; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.control.Keyboard; import de.teamteamteam.spacescooter.entity.Entity; @@ -38,7 +38,7 @@ public class GamePausedScreen extends Screen { @Override protected void paint(Graphics2D g) { g.drawImage(this.img, 0, 0, null); - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().paint(g); @@ -54,7 +54,7 @@ public class GamePausedScreen extends Screen { @Override protected void update() { - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().update(); diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java index a19547f..9d187a6 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -5,7 +5,7 @@ import java.awt.Point; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.background.StarBackground; import de.teamteamteam.spacescooter.background.item.ItemChance; @@ -36,7 +36,7 @@ public class GameScreen extends Screen { @Override protected void paint(Graphics2D g) { - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().paint(g); @@ -45,7 +45,7 @@ public class GameScreen extends Screen { @Override protected void update() { - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().update(); diff --git a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java index 2b5c562..19d4532 100644 --- a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java @@ -6,7 +6,7 @@ import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.util.Iterator; -import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.background.StarBackground; import de.teamteamteam.spacescooter.control.Keyboard; @@ -40,7 +40,7 @@ public class MainMenuScreen extends Screen { @Override public void paint(Graphics2D g) { - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().paint(g); @@ -59,7 +59,7 @@ public class MainMenuScreen extends Screen { @Override public void update() { - LinkedList list = this.getEntities(); + List list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().update(); diff --git a/src/de/teamteamteam/spacescooter/screen/Screen.java b/src/de/teamteamteam/spacescooter/screen/Screen.java index 02e213c..c0f8c93 100644 --- a/src/de/teamteamteam/spacescooter/screen/Screen.java +++ b/src/de/teamteamteam/spacescooter/screen/Screen.java @@ -2,6 +2,7 @@ package de.teamteamteam.spacescooter.screen; import java.awt.Graphics2D; import java.util.LinkedList; +import java.util.List; import de.teamteamteam.spacescooter.entity.Entity; @@ -32,7 +33,7 @@ public abstract class Screen { /** * List of entities this screen is taking care of */ - protected LinkedList entities; + protected List entities; /** * Initialize parent, overlay and the Entity list @@ -61,7 +62,7 @@ public abstract class Screen { this.entities.remove(e); } - public LinkedList getEntities() { + public List getEntities() { return new LinkedList(this.entities); }