1

I'm trying to create a menu screen that will have buttons like 'play' 'High-Scores' 'Instructions' and 'Close Application'. So far, i have a menu that will open and when enter is pressed, it will play the app. I'm just wondering how to create the menu, while still keeping my words of code...

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import displaypackagev1.Sea_InvadersDisplay;
import statepackagev1.Sea_InvadersStateMachine;
import statepackagev1.StateMachineInterface;

public class MenuScreen extends StateMachineInterface implements KeyListener {

    private Font titleFont = new Font("Comic Sans MS", Font.PLAIN, 64);
    private Font startFont = new Font("Comic Sans MS", Font.PLAIN, 32);
    private String title = "Space Invaders";
    private String start = "Press Enter";

    public MenuScreen(Sea_InvadersStateMachine stateMachine) {
        super(stateMachine);
    } 

    @Override
    public void update(double delta) {

    }

    @Override
    public void draw(Graphics2D g) {
        g.setFont(titleFont);
        int titleWidth = g.getFontMetrics().stringWidth(title);
        g.setColor(Color.yellow);
        g.drawString(title, ((Sea_InvadersDisplay.WIDTH/2)-(titleWidth/2))-2, (Sea_InvadersDisplay.HEIGHT/2)-123);
        g.setColor(Color.green);
        g.drawString(title, (Sea_InvadersDisplay.WIDTH/2)-(titleWidth/2), (Sea_InvadersDisplay.HEIGHT/2)-125);

        g.setFont(startFont);
        g.setColor(Color.white);
        int startWidth = g.getFontMetrics().stringWidth(start);
        g.drawString(start, (Sea_InvadersDisplay.WIDTH/2)-(startWidth/2), (Sea_InvadersDisplay.HEIGHT/2)+75);
    }

    @Override
    public void init(Canvas canvas) {
        canvas.addKeyListener(this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            getStateMachine().setState((byte) 1);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

}
Papershine
  • 4,995
  • 2
  • 24
  • 48
Kyle Mills
  • 45
  • 6
  • Nice one. Hopefully [this post](http://stackoverflow.com/questions/7560113/creating-a-interactive-gui-for-a-java-game) and [Fruit Ninja style game on Android](https://github.com/emmaguy/FruitNinja) helps you.:) – StackUseR Feb 02 '17 at 11:06

0 Answers0