/* GamePanel class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called Child of JPanel because JPanel contains methods for drawing to the screen Implements KeyListener interface to listen for keyboard input Implements Runnable interface to use "threading" - let the game do two things at once */ import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import javax.swing.*; public class MenuPanel extends JPanel implements Runnable, KeyListener{ //dimensions of window public static final int GAME_WIDTH = 1225; public static final int GAME_HEIGHT = 630; public final static int TOTAL_BOXES = 2; public JPanel gameFrame; public static Camera camera = new Camera(0); public Thread gameThread; public Image image; public Graphics graphics; public BackgroundImage background; public TextBox title, enter, settings; public ArrayList textBoxArray = new ArrayList(); public Font standardFont = new Font(Font.MONOSPACED, Font.BOLD, 60); public int playerFrame, enemyFrame; // keeps track of how many ticks has elapsed since last frame change public int currentBox = 0; // image imports begin here public BufferedImage backgroundImage = GamePanel.getImage("img/backgrounds/pointyMountains.png"); public MenuPanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { this.gameFrame = gameFrame; title = new TextBox(100, 400, 100, GAME_WIDTH, standardFont, "Detroit", null); enter = new TextBox(300, 600, 100, GAME_WIDTH, standardFont, "Start Game", "game"); settings = new TextBox(400, 600, 100, GAME_WIDTH, standardFont, "Settings", "menu"); textBoxArray.add(enter); textBoxArray.add(settings); background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10); // 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 // request focus when the CardLayout selects this game this.addComponentListener(new ComponentAdapter() { @Override public void componentShown(ComponentEvent cEvt) { Component src = (Component) cEvt.getSource(); src.requestFocusInWindow(); } }); //add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { //player.mousePressed(e); } }); this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); //make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time! gameThread = new Thread(this); gameThread.start(); } //paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself public void paint(Graphics g){ //we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed. image = createImage(GAME_WIDTH, GAME_HEIGHT); //draw off screen graphics = image.getGraphics(); draw(graphics, playerFrame, enemyFrame);//update the positions of everything on the screen g.drawImage(image, 0, 0, this); //move the image on the screen } //call the draw methods in each class to update positions as things move public void draw(Graphics g, int playerFrame, int enemyFrame){ background.draw(g); title.draw(g,null, Color.black); for (TextBox t: textBoxArray) { t.draw(g, null, Color.cyan); } textBoxArray.get(currentBox).draw(g, Color.gray, Color.blue); } //call the move methods in other classes to update positions //this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy public void move(){ } //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen public void run(){ //the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen. long lastTime = System.nanoTime(); double amountOfTicks = 60; double ns = 1000000000/amountOfTicks; double delta = 0; long now; while(true){ //this is the infinite game loop now = System.nanoTime(); delta = delta + (now-lastTime)/ns; lastTime = now; //only move objects around and update screen if enough time has passed if(delta >= 1){ move(); camera.x += 10; repaint(); delta--; } } } //if a key is pressed, we'll send it over to the Player class for processing public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { // logic for different screens starts here 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; } } //if a key is released, we'll send it over to the Player class for processing public void keyReleased(KeyEvent e){ } //left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface public void keyTyped(KeyEvent e){ } }