Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:02:29+00:00 2026-05-26T21:02:29+00:00

Scenario: I have one JFrame and JPanel added to that JFrame . on this

  • 0

Scenario: I have one JFrame and JPanel added to that JFrame. on this panel, I have drawn 3 images using:

public void paint(graphics g) { 
     g.drawImage(img1,100,100,null);
     g.drawImage(img2,200,200,null);
     g.drawImage(img3,300,300,null);
}

I have implemented MouseListener interface to listen clicks. Now, I want that whenever I click any of these images my output(on command prompt using System.out.println();) should be the image object which I have clicked?

Please explain me is it possible and how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:02:30+00:00Added an answer on May 26, 2026 at 9:02 pm

    Well, first of all you are drawing all your images at (0,0), are you sure you want to do that? If you do that is possible that you click a point that belongs to all your images (es, 0,0).

    By the way, inside your MouseListener you have this method:

    public void mouseClicked(MouseEvent e)
    {
        Point point = e.getPoint();
    }
    

    point store the coordinate of your click relative to the component you are listening to.
    So what you have to do is simply to check if the point where you click is inside image area. You can do the following:

    Rectangle imageBounds = new Rectangle(x,y,image_width, image_height);
    if (imageBounds.contains(point)){
        //point is inside given image
    }
    

    where x,y are the coordinate where you are drawing your image with drawImage method(0,0 in your case) and image_width, image_height are the dimension of your image.

    EDIT:

    there is an alternative to the solution I explained above. Like suggested by Hovercraft Full Of Eels you can do the following:

    1. create a JLabel for each image you have want to display
    2. use JLabel’s setIcon() method to specify the image that will be displayed on each label.
    3. Add your labels to your JPanel
    4. add a mouse listener to each JLabel

    This approach has a great benefit: you don’t have to worry about mouse coordinates because each JLabel has it’s relative mouse listener.
    The only one thing that you should consider is the following:

    using Component instead of drawing your images, you won’t be able to absolute poisiotion them, but you have to use an appropiate LayoutManager to layout your JLabel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a similar scenario as this one: public class TestLinq2Xml { private XElement
Here is the scenario that I have. I have a cvs repository in one
I have one scenario in which the user can define the column and that
So this is the scenario: You have a bunch of data that needs to
Scenario I have 3 database tables. One is for Images and the other two
The scenario is this: I have Java swing application with JFrame. There is textarea
i have some simple Export/Import Scenario that i can't figur out why this not
This is a simplified scenario to cut to the chase: I have one webpage
I have one scenario here, and maybe someone could enlighten me at this corner
I have a scenario as follows: I have one Windows 2003 server that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.