Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Applet with images

    Hey I have an applet with several images on a canvas.

    I want to know on wich image I have clicked and then be able to drag that image. How can I do this?

    I'm already aware of the drag en drop of a mouseMotionAdapter and a mouseMotionListener. Wich I am using in my package. I just don't know how my application can recognize the image I clicked on.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Applet with images

    Do you know the location of each image? Just check the mouse location against the bounds of each image.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Applet with images

    how my application can recognize the image
    A mouse listener would help you find where the mouse was clicked.
    Then it depends on how the images are being displayed.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Applet with images

    Quote Originally Posted by KevinWorkman View Post
    Do you know the location of each image? Just check the mouse location against the bounds of each image.
    Yes I know the location of the images. When the applet starts 32 images are drawn. (BufferedImage[]) on the applet. 4 rows of eight.
    g2.drawImage(image,positionX,positionY,null) in this way. But how do you check the bounds with your mouse?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Applet with images

    The event passed to the listener has the location of the mouse click that you can get by calling a method.
    Given the x,y of the click and the location of the images, you can search the images' locations to see which contains the location of the mouse click.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Applet with images

    Do you have an example of this? I do have a mouseClicked method that gives me the exact position of the mouse. But I still don't get how you can compare this with all of the x,y coordinates the image overlaps on the canvas. Or isn't that nescessary?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Applet with images

    how you can compare this with all of the x,y coordinates the image
    If the locations of the images are in a list, use a loop to test the mouse's location against the location of each image in the list.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Applet with images

    And if I don't know the location of the images. How do I get the location of the images. I placed the images on the applet myself (mvc) view so in theory I know them, but just wondering.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Applet with images

    Quote Originally Posted by The original stinger View Post
    And if I don't know the location of the images. How do I get the location of the images. I placed the images on the applet myself (mvc) view so in theory I know them, but just wondering.
    You have to know the position, or at least save it in a place you can get it (like in an Object that contains an image and its position). How else are you drawing it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Applet with images

    Yes but all 32 images are drown on a canvas (the view extends canvas) so canvas is the object you are referring to. Still a simple example would be very helpfull to understand this better.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Applet with images

    If you define a rectangle that maps where the image is, you can use the rectangle class's contains() method. Something like this:
    if(imagesRect[ix].contains(mouseClickPt)) {
    // click was in this image
    }
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Applet with images

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: Applet with images

    Quote Originally Posted by Norm View Post
    If you define a rectangle that maps where the image is, you can use the rectangle class's contains() method. Something like this:
    if(imagesRect[ix].contains(mouseClickPt)) {
    // click was in this image
    }
    How do you define a rectangle that maps where the image is? Or how do you make a rectangle that contains an image?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default Re: Applet with images

    Given the image's location at the x,y,w,h then define a Rectangle:
    Rectangle imgRect = new Rectangle(imgX, imgY, imgW, imgH);
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Images in applet
    By Ranu in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 05:05 AM
  2. Putting Applet on web with images
    By Peetahzee in forum New To Java
    Replies: 1
    Last Post: 04-18-2010, 08:35 PM
  3. [Applet] Clicking images & moving them.
    By Perfectworld in forum Java Applets
    Replies: 17
    Last Post: 01-16-2010, 08:09 AM
  4. Extract images from Java applet
    By User01 in forum Java Applets
    Replies: 2
    Last Post: 12-23-2009, 06:44 AM
  5. Applet with Images in Browser
    By baron in forum Eclipse
    Replies: 0
    Last Post: 09-20-2009, 10:06 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
  •