菜单上退出游戏的按钮没有关闭游戏

时间:2019-01-12 09:13:46

标签: java awt mouselistener

我正在尝试制作游戏菜单,但无法使“退出”按钮起作用。这是一个学校项目,我并不是最擅长编码的人,我最近开始使用eclipse,但仍然不熟悉Java中的许多东西。任何帮助将不胜感激。

我试图寻找解决问题的方法,但找不到解决方法。

package com.game.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable{

/**
 * 
 */
private static final long serialVersionUID = -473349850293143017L;

public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

private Thread thread;
private boolean running = false;
private Menu menu;

public enum STATE {
    Menu,
    Game    
};

public STATE gameState = STATE.Menu;

public Game() {

    this.addMouseListener(menu);

    new Window(WIDTH, HEIGHT, "Learning About Animals", this); 

    //create handlerby replacing null: the actual game
    menu = new Menu(this);

    if(gameState == STATE.Game);
}

public synchronized void start() {
    thread = new Thread (this);
    thread.start();
    running = true;
}

public synchronized void stop() {
    try {
        thread.join();
        running = false;
    }catch(Exception e) {
        e.printStackTrace();

    }
}

public void run(){
    long lastTime = System.nanoTime();
      double amountOfTicks = 60.0;
      double ns = 1000000000 / amountOfTicks;
      double delta = 0;
      long timer = System.currentTimeMillis();
      int frames = 0;
      while(running){
          long now = System.nanoTime();
          delta += (now - lastTime) / ns;
          lastTime = now;
          while(delta >= 1){
              tick();
              delta--;

          }
          if(running)
              render();
          frames++;

          if(System.currentTimeMillis() - timer > 1000){
              timer += 1000;
              System.out.println("FPS: " + frames);
              frames = 0;

          }
      }
      stop();
}

private void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null){
        this.createBufferStrategy(3);
        return;

    }

    Graphics g = bs.getDrawGraphics();

    //changes colour
    g.setColor(Color.green);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    if(gameState == STATE.Game)
    {
        hud.render(g);
    }else if(gameState == STATE.Menu) {
        menu.render(g);
    }
    g.dispose();
    bs.show();
}

private void tick(){

    if(gameState == STATE.Game)
    {
        hud.tick();

    }else if(gameState == STATE.Menu) {
        menu.tick();
    }
}

public static void main(String args[]) {
    new Game();
}

}

package com.game.main;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.game.main.Game.STATE;

public class Menu extends MouseAdapter{

private Game game;

public Menu(Game game) {
this.game = game;
}

public void mousePressed(MouseEvent e){
    int mx = e.getX();
    int my = e.getY();

    //play button
    if (mouseOver(mx, my, 210, 150 ,200, 64)) {
        game.gameState = STATE.Game;
    }

    //Quit button
    if (mouseOver(mx, my, 210, 350 ,200, 64)) {
        System.exit(1);
    }


}

public void mouseReleased(MouseEvent e) {

}

private boolean mouseOver(int mx, int my, int x, int y, int width, int 
height) {
    if(mx > x && mx < x + width) {
        if(my > y && my < y + height) {
            return true;
        }else return false;
    }else return false;
}

public void tick() {

}

public void render(Graphics g) {
    Font fnt = new Font("arial", 1, 50);
    Font fnt2 = new Font("arial", 1, 30);

    g.setFont(fnt);
    g.setColor(Color.white);
    g.drawString("MENU", 240, 70);

    g.setFont(fnt2);
    g.drawRect(210, 150, 200, 64);
    g.drawString("PLAY", 270, 190);

    g.setFont(fnt2);
    g.drawRect(210, 250, 200, 64);
    g.drawString("HELP", 270, 290);

    g.setFont(fnt2);
    g.drawRect(210, 350, 200, 64);
    g.drawString("QUIT", 270, 390);
    }
}

正在寻找帮助以使我的退出按钮正常工作,我正在获取无法说出MouseAdapter类找不到源的信息。

0 个答案:

没有答案