Results 1 to 7 of 7
  1. #1
    DaRancor is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default Basic Image Questions

    Hello, I want to be more informed on the basic syntax for my problem.

    In our Java class we are making games that basically only requires sound and animation. For my game I wanted to do that "adding game" which is a two player game involving your hands. I am currently trying to just do a detailed psuedo-code for my game.

    So my questions are:
    1. How would I set up a panel with images into specific places? (In my game I need 4 hands so I want two sets of hands on the bottom of the panel, and another set on the top)
    2. How would you know if the mouse clicked within a certain image's boundary and know which image that is? (In my game I need to know the sequence a user clicked on the hand images to determine what game function should be executed. For example: If I clicked on player one's right hand and then player two's left hand an animation would occur)

    If my questions are too general please feel free to ask for specifications. Thank you, to anyone who answers my post!

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

    Default

    Swing? AWT?

    1. How would I set up a panel with images into specific places? (In my game I need 4 hands so I want two sets of hands on the bottom of the panel, and another set on the top)
    Either use layout managers or absolute positioning (the null layout manager). We in general prefer to use layout managers since that lets the managers handle the dirty work for us, but when this is impossible, fall back on null layouts. Just realize that if you use a null layout, you are responsible for both the location and size of each component held by that container.

    How would you know if the mouse clicked within a certain image's boundary and know which image that is? (In my game I need to know the sequence a user clicked on the hand images to determine what game function should be executed. For example: If I clicked on player one's right hand and then player two's left hand an animation would occur)
    If Swing, then I imagine each image will be held in its own JPanel or JLabel. You could give these components a MouseListener and listen for mousePressed events.

  3. #3
    DaRancor is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Sorry, I'm not too sure what you mean by "Swing?" or "AWT?". I thought those are just different tools to writing a program. And I assume I'm probably going to be using both in my game.

    Making each image into a panel/label sounds like a great idea, that helped make things a little clearer for me.

    Also in regards to the mouseListener class , instead of having to keep track of the numerical coordinate point the user clicked, is there a way I can get the event in terms of which panel it clicked? (Like with a radio button listener, we determine the action event in terms of the name we gave to the button. Can we do this for panels and mouseClicked events?)

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

    Default

    Sorry, I'm not too sure what you mean by "Swing?" or "AWT?". I thought those are just different tools to writing a program. And I assume I'm probably going to be using both in my game.
    Swing and AWT are both toolkits used for building GUI applications. AWT is the older of the two while Swing which was built on top of AWT is the more recent, the more powerful and in general the more popular. You will usually create a GUI using graphical components from one or the other toolkits but not from both, and I suggest that you choose Swing.

    Also in regards to the mouseListener class , instead of having to keep track of the numerical coordinate point the user clicked, is there a way I can get the event in terms of which panel it clicked? (Like with a radio button listener, we determine the action event in terms of the name we gave to the button. Can we do this for panels and mouseClicked events?)
    If you add a MouseListener to a JPanel that has an image for instance, that JPanel will "know" when it is being clicked since its mouse listener will only be activated if this in fact happens. In this situation, there's no need to keep track of numerical coordinates.

  5. #5
    DaRancor is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Ah, I see, well thank you for the information. Alright that's good to hear I won't be needing to keep track of the coordinates.

    I think I might be a bit confused on the process, as I am unsure of how I read in a mouse event for a clicked panel. Do I use the getSource() method and compare the source with the titles of the panels? Also, how would I know if a certain sequence is being clicked by the user? As mentioned before my game needs two clicks by the user over two different image panels to execute any actions.

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

    Default

    getSource will in fact tell you which component has been clicked. As for your program logic, for that you'll need to have a counter in your MouseListener, and you'll want to add the same MouseListener to all the components that need it.

  7. #7
    DaRancor is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Hmm I see, so I will probably use a %2 or something for the counter to determine the number of events clicked in my case then. Alrighty, that was extremely helpful, thank you very much!!

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. Need some really basic help
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 01-24-2009, 06:00 AM
  4. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 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
  •