[英]How do I only print to the screen the coordinates of what the mouse is drawing on a canvas in Java?
[英]How do I get my mouse coordinates relative to the window rather than the screen?
我正在嘗試獲取鼠標相對於窗口的坐標。 我知道我需要引用JFrame或其他內容,但我一輩子都無法弄清楚是怎么回事,我的班級為我提供了此初學者代碼,並且我從那里開始構建了它。
public class TheGame extends JFrame
{
public static final int WIDTH = 785;
public static final int HEIGHT = 670;
public static int width;
public static int height;
public static int borderX;
public static int borderY;
public TheGame()
{
super("PLATFORMGAME");
setSize(WIDTH,HEIGHT);
PlatformGame game = new PlatformGame();
((Component)game).setFocusable(true);
getContentPane().add(game);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main( String args[] )
{
TheGame run = new TheGame();
}
}
這是我的鼠標偵聽器類:
public class mouse extends JFrame {
static Point p;
BufferedImage sprite;
public mouse() {
Point p = MouseInfo.getPointerInfo().getLocation();
try {
sprite = ImageIO.read(new File("mouse_pointer.png")) ;
} catch (IOException e1) {
e1.printStackTrace();
}
}
public Point getlocation() {
return p = MouseInfo.getPointerInfo().getLocation();
}
public static double getNorP(double uno, double dos) {
if ((uno - dos) > 0) {return -1;}
else {return 1;}
}
public static double getX(Char c) {
return MouseInfo.getPointerInfo().getLocation().x ;
//return MouseInfo.getPointerInfo().getLocation().x - ReferenceJFrame.getLocationOnScreen().x;
}
public static double getY(Char c) {
return MouseInfo.getPointerInfo().getLocation().y -29;
//return MouseInfo.getPointerInfo().getLocation().y - ReferenceJFrame.getLocationOnScreen().y;
}
public static double getXD(Char c) {
return Math.abs(c.rX-(double)getX(c));
}
public static double getYD(Char c) {
return Math.abs(c.rY-(double)getY(c));
}
// public static float getAngle(Point target,int x,int y) {
// return (float) Math.toDegrees(Math.atan2(getX()-x, getY()-y));
}
public static double getXP(Char c) {
return (getXD(c))/(getXD(c)+getYD(c)) * getNorP(c.rX,getX(c));
}
public static double getYP(Char c) {
return (getYD(c))/(getXD(c)+getYD(c)) * getNorP(c.rY,getY(c));
}
public void draw(Graphics window, Char c) {
window.drawImage(sprite,(int) getX(c)-16,(int) getY(c)-8, 16, 16, null);
}
}
這將返回鼠標相對於屏幕而不是窗口的坐標。 除了JFrame類型的東西,我了解很多Java。
您可以使用JFrame
的方法getLocation()
返回Point
對象。 然后使用類似:
mouseX = MouseInfo.getPointerInfo().getLocation().x - frame.getLocation().x;
mouseY = MouseInfo.getPointerInfo().getLocation().y - frame.getLocation().y;
在您的情況下,getMouseLocation()方法將如下所示:
public Point getMouseLocation(){
int x = MouseInfo.getPointerInfo().getLocation().x - run.getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y - run.getLocation().y;
return new Point(x,y);
}
首先,不要使用MouseInfo
,這並不是獲取鼠標位置的最佳選擇,相反,首先要在PlatformGame
添加MouseListener
,有關更多詳細信息,請參見如何編寫鼠標偵聽器 。
這將在調用鼠標偵聽器時傳遞一個MouseEvent
,該事件將在注冊MouseListener
的組件的坐標空間內,因此,無需進行任何轉換!
現在,如果出於某種奇怪的原因,您仍然希望將PlatformGame
坐標空間轉換為框架的坐標空間,則可以使用以下簡單方法:
evt = SwingUtilities.convertMouseEvent(evt.getComponent(), evt, SwingUtilities.windowForComponent(evt.getComponent()));
evt
是MouseEvent
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.