Results 1 to 6 of 6
Thread: getx and gety mouseClick stuff
- 11-22-2010, 04:16 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
getx and gety mouseClick stuff
Hi, I'm new here so don't flame me for being a newb or something.
So I learned a little bit of Java and C++ in my computer science class and I decided to write make a game for computer club. It is a tower defense game.
I'm 99% finished with it, the one last thing that I have to do is find a way to get the x and y coordinates of where the user last clicked. That's it. I already have the functions set up to buy, place, upgrade, and sell towers determined on where the user clicked on the screen, all I need is an easy way to input the x and y.
I've been searching for an easy solution to this all afternoon, and what I have found is that it there needs to be an object like a rectangle to click on and it will be able to tell where the user clicked on that object. But I wrote my code in a very linear format, that is, I haven't learned how to use object oriented code yet, soo instead of having each individual thing on screen realize that you clicked on it, the program is supposed to search to see if the x and y coordinates of the last mouse click lie within any of the objects on screen.
I understand that to make this work I need to have a transparent rectangle drawn over everything so that the program can tell exactly where you clicked on that rectangle. but I don't know how to do that, and any transparent stuff that I have tried just ends up making the whole screen white.
do any of you know a quick fix for this? that is, something that won't have me rewriting my whole program just to make it work. Thanks in advance for your help. :)
-
If Swing, then this can be done with the glass pane which can overlie everything in a top-level component such as a JFrame. But having said that, I suggest that you learn the OOP stuff first before doing GUI coding.
- 11-22-2010, 04:32 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
so the jframe is initially invisible, right? and I can make a jframe over everything else that the applet has drawn and have that be clickable? (clickable meaning that if I click on it it will return the x and y coordinates of where I clicked)
-
No. If you are using a JApplet, then you would use it's glass pane obtained via its getGlassPane() method. Check out the Oracle Java Swing tutorials on how to use the glass pane.
- 11-22-2010, 04:43 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
hmm... it seems that I'm not going to figure this out anytime soon. I'll get my friend to help me. we were supposed to be working together on this and I ended up doing almost everything myself. >:( I'll just have him figure it out. lol.
thanks though, it's not that you weren't any help, it's just that I was looking for like 1 or 2 lines of code that I could insert into my program to make it work. I guess it's not as simple as I thought it would be. :)
-
With glass pane, no it would take a bit more since you'd have to get the mouse click and then somehow send it to the components below to act on. Not to harp too much on anything, but I'm it's possible that the solution would be easier if you make your program a well-behaved OOP program.
Similar Threads
-
MouseClick in table of Search and To see previes record
By Zuraima in forum Java SoftwareReplies: 2Last Post: 08-10-2010, 09:03 AM -
getX() and getY() return same value
By chielt in forum Java AppletsReplies: 14Last Post: 12-21-2009, 08:09 AM -
How To Lissen to MouseClick on Combobox.?
By ocean in forum New To JavaReplies: 1Last Post: 12-04-2009, 08:06 AM -
How to make a JPanel highlighted on mouseclick
By aneesahamedaa in forum AWT / SwingReplies: 3Last Post: 09-01-2008, 10:49 PM -
How to make a MouseClick paint an object
By Devilsfutbol17 in forum New To JavaReplies: 6Last Post: 06-05-2008, 01:54 PM
Bookmarks