From c5b76fbdf9bac3ce0dc1422a349ac78071cc9b11 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 12 Nov 2014 11:20:27 +0100 Subject: [PATCH] Cache inset offsets used to translate Graphics2D object. --- .../teamteamteam/spacescooter/GameFrame.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/de/teamteamteam/spacescooter/GameFrame.java b/src/de/teamteamteam/spacescooter/GameFrame.java index 9dc1ec9..b2b16d6 100644 --- a/src/de/teamteamteam/spacescooter/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/GameFrame.java @@ -38,6 +38,17 @@ public class GameFrame extends JFrame { */ private long frameTime; + /** + * Amount of pixels taken away from window decoration on the left side. + */ + private int insetLeft; + + /** + * Amount of pixels taken away from window decoration on the top side. + */ + private int insetTop; + + /** * Default constructor. */ @@ -78,6 +89,11 @@ public class GameFrame extends JFrame { //prepare the buffered strategy this.createBufferStrategy(2); this.bufferStrategy = this.getBufferStrategy(); + + //Measure and cache inset offsets for continuous use in the draw() method. + //This way, we translate our 0x0 to the first top left pixel that is actually visible. + this.insetLeft = this.getInsets().left; + this.insetTop = this.getInsets().top; } /** @@ -121,7 +137,7 @@ public class GameFrame extends JFrame { this.applyRenderingHints(bufferedGraphics); //Apply translation since different platforms use different window decorations that mess //up our coordinates. The new origin shall be the first usable pixel in the top left corner. - bufferedGraphics.translate(this.getInsets().left, this.getInsets().top); + bufferedGraphics.translate(this.insetLeft, this.insetTop); this.superScreen.doPaint(bufferedGraphics); //Trigger the actual paint routines. } catch (Exception e) { System.err.println("Exception in GameFrame.draw() gefangen:");