MouseListener中的getX和getY问题

时间:2015-05-23 16:25:21

标签: java swing mouselistener

我正在编写Tower Defense游戏,现在我已经遇到了与MouseListener相关的一些问题。我已经创建了类Mouse Hanlder(它实现了Mouse Listener)和第二个类,其中全屏由paint(Graphics g)制作。我想检查玩家是否按下塔来购买它。不起作用的是,getX()和getY()从我的屏幕返回0而不是右坐标。我应该改变什么?

public class MouseHandler implements MouseListener, MouseMotionListener {

    private GameScreen screen;
    private GameScreen.MouseHeld mouseHeld;

    public MouseHandler(GameScreen screen){
        this.screen = screen;
        this.mouseHeld = this.screen.new MouseHeld();
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {
        this.mouseHeld.mouseDown(e);
    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseDragged(MouseEvent e) {
        this.mouseHeld.mouseMove(e);
    }

    public void mouseMoved(MouseEvent e) {
        this.mouseHeld.mouseMove(e);
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

}

用屏幕上课:

public class GameScreen extends JPanel implements Runnable {

    private static final long serialVersionUID = 1L;

    public Thread thread = new Thread(this) 

    public GameScreen(GameFrame frame) {
        this.frame = frame;
        this.setSize(1280, 800);
        this.addMouseListener(new MouseHandler(this));

        thread.start();

    }

    public void paint(Graphics g) {
        // Code with my painted screen

    }

    // Some code with running game

    public class MouseHeld {


        boolean mouseDown = false;


        int hand = 0;
        int handXPos;
        int handYPos;

        public void mouseDown(MouseEvent e) {
            mouseDown = true;

            if(hand != 0){

                hand = 0;
            }
            System.out.println(handYPos);
            System.out.println(handXPos);
            uptadeMouse(e);
        }

        public void mouseMove(MouseEvent e) {
            handXPos = e.getX();
            handYPos = e.getY();
        }

        public void uptadeMouse(MouseEvent e) {
            if (mouseDown && hand == 0) {

                if (handYPos <= 680 && handYPos >= 640) {

                    // Tower 1
                    if (handXPos >= 300 && handXPos <= 340) {
                        if (player.money >= Tower.allTowers[0].cost) {
                            System.out.println("You bought a tower!");
                            hand = 1;
                        }
                    }
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

您只能在mouseMove中更新handXPos和handYPos,而不能更新其他鼠标方法。您永远不会将处理程序添加为MouseMotionListener,因此永远不会调用mouseMove。

如,

public GameScreen(GameFrame frame) {
    this.frame = frame;
    this.setSize(1280, 800); // avoid this

    MouseHandler mouseHandler = new MouseHandler(this);     
    addMouseListener(mouseHandler);
    addMouseMotionListener(mouseHandler);
}

另一个更好的解决方案是在所有使用的鼠标方法中更新handXPos和handYPos。

这是我的最小测试计划,您应该为将来的问题提供这些计划:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;

import javax.swing.*;

public class FooGui {
   private static void createAndShowGui() {
      FooGui mainPanel = new FooGui();

      GameFrame frame = new GameFrame("Foo Gui");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(new GameScreen(frame));
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MouseHandler implements MouseListener, MouseMotionListener {

   private GameScreen screen;
   private GameScreen.MouseHeld mouseHeld;

   public MouseHandler(GameScreen screen) {
      this.screen = screen;
      this.mouseHeld = this.screen.new MouseHeld();
   }

   public void mouseClicked(MouseEvent e) {

   }

   public void mousePressed(MouseEvent e) {
      this.mouseHeld.mouseDown(e);
   }

   public void mouseReleased(MouseEvent e) {

   }

   public void mouseDragged(MouseEvent e) {
      this.mouseHeld.mouseMove(e);
   }

   public void mouseMoved(MouseEvent e) {
      this.mouseHeld.mouseMove(e);
   }

   public void mouseEntered(MouseEvent e) {

   }

   public void mouseExited(MouseEvent e) {

   }

}

class GameScreen extends JPanel {

   private static final long serialVersionUID = 1L;
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private GameFrame frame;

   public GameScreen(GameFrame frame) {
      this.frame = frame;
      this.setSize(1280, 800);
      MouseHandler mouseHandler = new MouseHandler(this);

      addMouseListener(mouseHandler);
      addMouseMotionListener(mouseHandler);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   public class MouseHeld {
      boolean mouseDown = false;
      int handXPos;
      int handYPos;

      public void mouseDown(MouseEvent e) {
         mouseDown = true;

         System.out.println(handYPos);
         System.out.println(handXPos);
         uptadeMouse(e);
      }

      public void mouseMove(MouseEvent e) {
         handXPos = e.getX();
         handYPos = e.getY();
      }

      public void uptadeMouse(MouseEvent e) {
      }
   }

}

class GameFrame extends JFrame {

   public GameFrame(String name) {
      super(name);
   }

}

还要避免覆盖JPanel的绘制方法,而是使用paintComponent

退房: