java - How can I check the mouse's position over a 2D array of images for a game? -
okay creating candy crush game replica in java. problem lies in trying create event listener clicking or hovering on objects. have tried different methods using imageicons , using event listeners using jlabels , using icons , trying determine if jlabel clicked.
what trying use rectangles , hitboxes so:
public rectangle mousehitbox(mouseevent e) { int x = (int)e.getlocationonscreen().getx(); //grab mouse x coordinate , set int int y = (int)e.getlocationonscreen().gety(); //grab mouse y coordinate , set int mouse.setbounds(x,y,1,1); //create mouse hitbox return mouse; //return mouse event } public rectangle gemhitbox() { (int = 0; < 8; i++) { (int j = 0; j < 8; j++) //for loops access 81 gem images { int x = gemimage[i][j].getx(); // x coordinate of specific array destination int y = gemimage[i][j].gety(); // y coordinate of specific array destination gem.setbounds(x,y,50,47); // gem hitbox } } return gem; //return mouse event } @override public void mouseclicked(mouseevent e) { if(mousehitbox(e).intersects(gemhitbox())) // if mouse hitbox comes in contact 1 of array positions { //prepare swap gems } }
the main class implements mouselistener why can have public void mouseclicked.
no matter code contatined in mouse listeners, has thrown exception:
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ game.mousehitbox(game.java:125) //this line: mouse.setbounds(x,y,1,1); @ game.mouseclicked(game.java:147) //this line: if(mousehitbox(e).intersects(gemhitbox())) @ java.awt.component.processmouseevent(component.java:6508) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3320) @ java.awt.component.processevent(component.java:6270) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4861) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4501) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422) @ java.awt.container.dispatcheventimpl(container.java:2273) @ java.awt.window.dispatcheventimpl(window.java:2719) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:735) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:694) @ java.awt.eventqueue$3.run(eventqueue.java:692) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:708) @ java.awt.eventqueue$4.run(eventqueue.java:706) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:705) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)
i have been stuck on problem 3 days , suggestions great.
have checked mousehitbox return not null ?
the stacktrace seems show method return null in case.
is mouse initialised in case?
Comments
Post a Comment