无法让我的MouseListener为高中的神奇宝贝游戏工作

时间:2019-04-09 00:56:23

标签: java swing awt mouselistener

我有一个游戏类,其中大部分渲染和框架都已完成。我有一个用于鼠标侦听器的类。我还有一个叫做Menu的类,它在画布上绘制菜单。当我单击“开始”按钮时,我希望它实际上开始游戏,但是好像MouseListener并未收到鼠标点击。

我已经尝试在整个Game类的许多地方放置addMouseListener(new MouseInput())行,但这行不通。

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseInput implements MouseListener
{

    public void mousePressed(MouseEvent e) 
    {
    int mx = e.getX();
    int my = e.getY();
        if(Game.STATE == 0)
        {
            if(mx >= 415 && mx <= 615)
            {
                if(my >= 350 && my <= 425)  
                {
                    Game.STATE = Game.STATE + 1;
                }
            }

            if(mx >= 415 && mx <=615)
            {
                if(my >= 500 && my <= 575)
                {
                    System.exit(1);
                }

            }
        }
    }
}

//游戏类

public class Game extends JFrame implements Runnable
{

    private Canvas c = new Canvas();
    public static int STATE = 0;
    public static final int WIDTH = 1000;
    public static final int HEIGHT = 800;
    private Menu menu;
    private FightState fight;



    public Game()
    {
        //Forces program to close when panel is closed
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //sets position and size of Frame
        setBounds(0, 0, WIDTH, HEIGHT);

        //puts Frame in center of the screen
        setLocationRelativeTo(null);

        //adds canvas to game
        add(c);

        //Makes frame visible
        setVisible(true);

        //creates our object for buffer strategy
        c.createBufferStrategy(2);

//      adds the mouse listner;
        addMouseListener(new MouseInput());



    }


    public void update()
    {

    }

    //renders the graphics onto the screen
    public void render()
    {
        BufferStrategy bufferStrategy = c.getBufferStrategy();
        Graphics g = bufferStrategy.getDrawGraphics();
        super.paint(g);


        //instantiates the menu object
        menu = new Menu();
        //instantiates the FightState object
        fight = new FightState();

        //renders the menu
            if(STATE == 0)
            {
                menu.render(g);
            }
        //renders the fight stage
            if(STATE == 1)
            {
                fight.render(g);
            }   

            g.setFont(new Font("Monospaced", Font.PLAIN, 35));
            g.drawString("STATE: " + STATE, 10, 400);
            repaint();

            //checks if mouseListener is working
            System.out.print(STATE);

            g.dispose();
            bufferStrategy.show();

    }

    //game loop
    public void run()
    {
        BufferStrategy bufferStrategy = c.getBufferStrategy();

        long lastTime = System.nanoTime(); //long is an int that stores more space
        double nanoSecondConvert = 1000000000.0 / 60; //frames/sec
        double deltaSeconds = 0;




        while(true)
        {
            long now = System.nanoTime();

            deltaSeconds += (now-lastTime)/nanoSecondConvert;

            while(deltaSeconds >=1)
            {
                update();
                deltaSeconds = 0;
            }

            render();
            lastTime = now;
            System.out.println("STATE: " + STATE);
        }

    }


    //main method
    public static void main(String[] args)
    {
        Game game = new Game();
        Thread gameThread = new Thread(game);
        gameThread.start();
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用的是super.paint(g);,请不要在JFrame上致电BufferStrategy。只需直接绘制到缓冲区。您还应该将MouseListener添加到Canvas,而不是框架。

Canvas在窗口的框架边界内布置,这意味着它将偏移并小于实际框架本身。

鼠标事件会自动转换为源坐标上下文,这意味着您当前正在尝试将来自框架坐标上下文的值与Canvas使用的不同

进行比较。
  

一个问题:如果缓冲区没有诸如fillRect()之类的“图形方法”,我将如何直接绘制到缓冲区?

Graphics g = bufferStrategy.getDrawGraphics()为您提供了Graphics上下文,然后您可以直接对其进行绘制。

您不希望(永远)直接致电paint,因为系统可以调用它,您可能会遇到竞争状况和其他问题

Swing使用了不同的绘画算法,而您通过使用BufferStrategy退出了该算法,这意味着您不能再使用“常规” Swing绘画过程,而必须编写自己的< / p>