diff --git a/src/BackgroundImage.java b/src/BackgroundImage.java index 9f11859..0cdaa8d 100644 --- a/src/BackgroundImage.java +++ b/src/BackgroundImage.java @@ -7,8 +7,9 @@ public class BackgroundImage { public int width, height; public int parallaxRatio; public BufferedImage backgroundImage; + public Camera camera; - public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio) { + public BackgroundImage(int x, int y, BufferedImage backgroundImage, int width, int height, int parallaxRatio, Camera camera) { this.x = x; this.y = y; this.width = width; @@ -16,10 +17,12 @@ public class BackgroundImage { this.backgroundImage = backgroundImage; this.parallaxRatio = parallaxRatio; + + this.camera = camera; } public void draw(Graphics g){ - g.drawImage(backgroundImage, x-GamePanel.camera.x/parallaxRatio % width, y, width, height, null); + g.drawImage(backgroundImage, x-camera.x/parallaxRatio % width, y, width, height, null); // added to prevent the background image from disappearing - g.drawImage(backgroundImage, x-GamePanel.camera.x/parallaxRatio % width + width - 1, y, width, height, null); + g.drawImage(backgroundImage, x-camera.x/parallaxRatio % width + width - 1, y, width, height, null); } } diff --git a/src/GamePanel.java b/src/GamePanel.java index 2db79e1..f85a12c 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -55,7 +55,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { this.gameFrame = gameFrame; camera = new Camera(0); - background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10); + background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera); try { // load player sprites from disk here for (int i = 0; i < 11; i++) { diff --git a/src/MenuPanel.java b/src/MenuPanel.java index 9bf55b1..b165df2 100644 --- a/src/MenuPanel.java +++ b/src/MenuPanel.java @@ -49,7 +49,7 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{ textBoxArray.add(enter); textBoxArray.add(settings); - background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10); + background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera); // the height of 35 is set because it is half of the original tile height (i.e., 70px) this.setFocusable(true); //make everything in this class appear on the screen this.addKeyListener(this); //start listening for keyboard input @@ -132,28 +132,12 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{ CardLayout cardLayout = (CardLayout) gameFrame.getLayout(); cardLayout.show(gameFrame, textBoxArray.get(currentBox).id); } else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) { - // sleep to prevent excessively fast scrolling - try { - Thread.sleep(100); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } currentBox = (currentBox + 1) % textBoxArray.size(); } else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) { - try { - Thread.sleep(100); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } // if currentBox > 0, subtract one // else, set to TOTAL_BOXES-1 currentBox = currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1; } else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) { - try { - Thread.sleep(100); - } catch (InterruptedException ex) { - ex.printStackTrace(); - } // if currentBox > total box amount - 1, set to 0 // else, set to TOTAL_BOXES-1 currentBox = currentBox < textBoxArray.size() - 1 ? currentBox + 1:0;