Java 如何检查鼠标';在游戏的2D图像阵列上的位置?
好的,我正在用Java创建一个糖果粉碎游戏副本。 我的问题在于试图创建一个事件侦听器,用于单击或悬停在对象上。我尝试了不同的方法,从使用ImageIcons和使用事件侦听器到现在使用JLabel和图标,并尝试确定是否单击了JLabel 我现在尝试的是使用矩形和点击框,如下所示:Java 如何检查鼠标';在游戏的2D图像阵列上的位置?,java,mouseevent,multidimensional-array,Java,Mouseevent,Multidimensional Array,好的,我正在用Java创建一个糖果粉碎游戏副本。 我的问题在于试图创建一个事件侦听器,用于单击或悬停在对象上。我尝试了不同的方法,从使用ImageIcons和使用事件侦听器到现在使用JLabel和图标,并尝试确定是否单击了JLabel 我现在尝试的是使用矩形和点击框,如下所示: public Rectangle mouseHitBox(MouseEvent e) { int x = (int)e.getLocationOnScreen().getX(); //Grab Mouse X C
public Rectangle mouseHitBox(MouseEvent e)
{
int x = (int)e.getLocationOnScreen().getX(); //Grab Mouse X Coordinate and set it as an Int
int y = (int)e.getLocationOnScreen().getY(); //Grab Mouse Y Coordinate and set it as an Int
mouse.setBounds(x,y,1,1); //Create Mouse Hitbox
return mouse; //Return it for Mouse Event
}
public Rectangle gemHitBox()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++) //For loops to access all 81 gem images
{
int x = gemImage[i][j].getX(); // Get X coordinate of that specific array destination
int y = gemImage[i][j].getY(); // Get Y coordinate of that specific array destination
gem.setBounds(x,y,50,47); // Gem Hitbox
}
}
return gem; //Return it for Mouse Event
}
@Override
public void mouseClicked(MouseEvent e) {
if(mouseHitBox(e).intersects(gemHitBox())) // If the Mouse Hitbox comes in contact with one of the array positions then
{
//Prepare to Swap Gems
}
}
我已经在这个问题上纠缠了三天了,有什么建议都可以。你检查过mouseHitBox返回的东西不是空的吗 stacktrace似乎显示,在这种情况下,该方法返回null
在这种情况下,鼠标是初始化的吗?我知道我做了什么,但忘了初始化它。谢谢!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Game.mouseHitBox(Game.java:125) //this line: mouse.setBounds(x,y,1,1);
at Game.mouseClicked(Game.java:147) //this line: if(mouseHitBox(e).intersects(gemHitBox()))
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)