Results 1 to 6 of 6
  1. #1
    Chozo Knight is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default 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. :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Chozo Knight is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    Chozo Knight is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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. :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Chozo Knight View Post
    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

  1. Replies: 2
    Last Post: 08-10-2010, 09:03 AM
  2. getX() and getY() return same value
    By chielt in forum Java Applets
    Replies: 14
    Last Post: 12-21-2009, 08:09 AM
  3. How To Lissen to MouseClick on Combobox.?
    By ocean in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 08:06 AM
  4. How to make a JPanel highlighted on mouseclick
    By aneesahamedaa in forum AWT / Swing
    Replies: 3
    Last Post: 09-01-2008, 10:49 PM
  5. How to make a MouseClick paint an object
    By Devilsfutbol17 in forum New To Java
    Replies: 6
    Last Post: 06-05-2008, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •