尝试使用按钮创建菜单屏幕

时间:2017-02-02 10:52:56

标签: java

我正在尝试创建一个菜单屏幕,其中包含“播放”'高分''指令'和'关闭应用程序'等按钮。到目前为止,我有一个菜单将打开,当按下输入时,它将播放应用程序。我只是想知道如何创建菜单,同时仍然保留我的代码......

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) {

    }

}

0 个答案:

没有答案