Make the panel fit the image so MouseListener only invoke when click  image

I want to add MouseListener to only the image itself. So I draw the image onto a image panel, add that panel onto the main panel, then add the main panel onto the frame. However, my problem is that the size of my image panel is too large, it take up almost the entire frame. So my mouselistener response not only when I click onto the image, but also anywhere in my image panel. I try to resize the image panel to fit the size of my image, but no luck. Is there a solution for this. I feel like, it got something to do with the layout. Here is my code, if you mind read it. Thank you very much
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class ImageRotate
     private static final String LABEL = "MIDI PROTOCOL";
     private JPanel mainPanel = new JPanel();
     private JPanel labelPanel = new JPanel();
     public ImageRotate()
          mainPanel.setOpaque(true);
          mainPanel.setBackground(Color.YELLOW);
          mainPanel.setLayout(new BorderLayout());
          Font labelFont = new Font("serif", Font.BOLD, 30);
          createLabelPanel(LABEL, labelFont);
          labelPanel.setOpaque(false);
          mainPanel.add(labelPanel, BorderLayout.NORTH);
          Drawing d = new Drawing();
          d.setOpaque(true);
          d.addMouseListener(new MouseAdapter(){
               public void mousePressed(MouseEvent e)
                    System.out.println("Pressing on the image");
          d.addMouseMotionListener(new MouseAdapter(){
               public void mouseDragged(MouseEvent e)
                    System.out.println("Dragging the image");
               public void mouseMoved(MouseEvent e)
          mainPanel.add(d, BorderLayout.CENTER);
     private void createLabelPanel(String str, Font font)
          JLabel label = new JLabel(str);
          label.setFont(font);
          label.setForeground(Color.RED);
          labelPanel.add(label);
     public JPanel getPanel()
          return mainPanel;
     public static void main(String args[])
          JFrame frame = new JFrame();
          frame.getContentPane().add(new ImageRotate().getPanel());
          frame.setSize(new Dimension(300, 300));
          frame.setVisible(true);
class Drawing extends JPanel
     Image img;
     public Drawing()
          setSize(new Dimension(100,100));     //Try to make the panel fit the image...NO LUCK
          setBackground(Color.GREEN);     
          img = Toolkit.getDefaultToolkit().createImage("images/nob.gif");
     public void paintComponent(Graphics g)
          super.paintComponent(g);
          Graphics2D g2D = (Graphics2D)g;
          g2D.drawImage(img, 40, 40, this);
}

yunaeyes wrote:
In class Drawing extends JPanel, I did try to to set the panel to the image size but no luck there,
both setSize(new Dimension(100,100)), setPreferredSize(new Dimension(100,100)) does not make the panel smallerIt can be made to work. I find it hard to comment on unseen code.
>
BigDaddyLoveHandles wrote:
I think a better approach is to handle the mouse click properly -- see if the click is over the image or not.Can you elaborate on this?The mousePressed method you posted assumes you are over the image. Check the mouse coordinates! If the component is bigger than the image this may not be so.

Similar Messages

  • How can we make the Retake Quiz button only appear when user reenters the course?

    In a Captivate 8 Quiz, we need to force the user to close the course (so attempts are captured by the LMS) from the Quiz Results page. The Retake Quiz button cannot show at this time. However, when the learner reenters the course from the LMS, the bookmarking brings the user to the Results page again. We need a Retake Quiz button to only show when the user reenters the course, not on Quiz completion. Disabling the bookmarking disables the reporting of attempts. Does anyone know a way to make that happen or a workaround?

    Have a look at my blog post:
    http://blog.lilybiri.com/review-only-incorrect-slides-captivate-6
    Lilybiri

  • I would like to be able to size images by kilobytes instead of pixels--is this possible?  I use I-Contact which requires a maximum of 70kb per image.  For best quality I want to make the images as close to 70 as possible.  Currently I choose a size of 300

    I would like to be able to size images by kilobytes instead of pixels--is this possible?  I use I-Contact which requires a maximum of 70kb per image.  For best quality I want to make the images as close to 70 as possible.  Currently I choose a size of 300 pixels on the shortest side but sometimes this gives me an image of slightly greater than 70kb and sometimes the image is just 30-40kb.  Is there a way to be more precise in reaching but not exceeding 70 kb?

    That is not a feature of iPhoto - suggest to Apple - iPhoto Menu ==> provide iPhoto feedback.
    You can opnly resize using the available options and file size is not one of them
    LN

  • Please help me!--rendering makes the images or video blurry (very pixelated) deteriorates the image  Adobe Premier Elements 13  need help!  .jpg and mpeg images,  but I have never "rendered" before since I got APE 13 about 6 weeks ago.  I am desperate for

    Please help me!--rendering makes the images or video blurry (very pixelated) deteriorates the image  Adobe Premier Elements 13  need help!  .jpg and mpeg images,  but I have never "rendered" before since I got APE 13 about 6 weeks ago.  I am desperate for assistance!

    That's going to be a ridiculous waste of money and energy.
    First of all, the current ATI drivers don't support multiple GPUs, so at the moment even a single 4870X2 would be only a 'normal' 4870 (which is quite a speed beast already). GFX drivers evolve rapidly, so things might look different next month, but when it comes to Linux and hardware there's one Golden Rule: stay away from the newest stuff and wait for proper support to get coded.
    I also wonder what power supply could possibly cope with the differences between idle and full load; that's way beyond 400W. But then, I'm one of those "quiet&green" types where >100W idle is already a bit much.
    I kind of understand that you want to get it done and not worry about hardware for the next 10 years or so, but that's simply not how the hardware world works and never did. At least not for the average consumer.

  • How do I make the image to a link?

    I am trying to make the image a link. I have the facebook logo position on the left sidebar, but I need to make it a link. Here is my code:
    <div id="sidebar">
    <span style="position: absolute; background: url(images/certificate.png); background-repeat: no-repeat; width: 250px; height: 150px; left: 160px; color:#600;">
    <p style="position: absolute; bottom: 44px; left: 3px; width: 210px; height: 90px; font-size:11px;">
    Alan is a certified Renovation, Repair and Painting Contractor. </p>
    <p style="position:absolute; bottom: -6px; left: 2px; width: 220px; height: 90px; font-size:12px; font-style:oblique; color:#600;">
    Click to see certificate</p></span>
      <span style="position: absolute; background: url(images/facebook_logo.png); background-repeat: no-repeat; width: 250px; height: 150px; top: 420px; left: 160px; color:#600;"></span>
      <p><a target="_blank" href="http://www.facebook.com/pages/WE-SHOW-UP/198687606583?ref=ts%3Cimg%20src="></p>
        <span style="position: absolute; background: url(images/facebook_logo.png); background-repeat: no-repeat; width: 250px; height: 150px; top: 420px; left: 160px; color:#600;"></span>
          <span style="position: absolute; background:url(images/BBB.png); background-repeat: no-repeat; width: 250px; height: 150px; top: 500px; left: 160px; color:#600;"></span>
    </div>
    Something went wrong here, because the whole right side became the link, but the facebook logo is not working.
    Here is the image.
    Thanks!

    <a target="_blank" href="http://www.facebook.com/pages/WE-SHOW-UP/198687606583?ref=ts%3Cimg%20s rc=">
    It's also missing any meaningful HTML to apply that link to!
    The code is so horrible I'm not sure there is a simple fix.  It appears that you are wanting that link to apply to the facebook image which is used as a background image -
    <p><a target="_blank" href="http://www.facebook.com/pages/WE-SHOW-UP/198687606583?ref=ts%3Cimg%20s rc="></p>
        <span style="position: absolute; background: url(images/facebook_logo.png); background-repeat: no-repeat; width: 250px; height: 150px; top: 420px; left: 160px; color:#600;"></span>
    But you cannot apply a link to a background image.  Ideally, you would have something like this -
    <p><a target="_blank" href="http://www.facebook.com/pages/WE-SHOW-UP/198687606583?ref=ts%3Cimg%20s rc="><img src="images/facebook_logo.png"/></a></p>
    Where the image is contained within the anchor tags.  But that's not possible given the code we have been shown - the whole layout approach would need to be changed.
    Where did this code (*ew*) come from?

  • Want to make the images un-clickable!

    I have a site where I need to make the images un-clickable. I don't want them to be enlarged. Not sure where to go to do this.
    Thanks for any suggestions!

    Just drag them onto a blank page and arrange them as you wish.

  • Why don't you make the image classification except the computer?

          Why don't you make the image classification except the computer?

    Please try posting in your native language. This question make no sense.

  • I have an Airport Extreme Base Station and have recently upgraded to Mountain Lion. Now airport utility won't work with my base station so I bought a Time Capsule. For the life of me I can't make the substitution work and only see a blinking amber light.

    I have an Airport Extreme Base Station and have recently upgraded to Mountain Lion. Now airport utility won't work with my base station so I bought a Time Capsule. For the life of me I can't make the substitution work and only see a blinking amber light. Can anyone who has made this change offer some advice?

    apikoros wrote:
    The Utility transferred all of the AE's settings, so I still have to change the password, which leaves me with only 2 other questions, I think:
    1)  I assume it's just a matter of using the Utility, entering a stronger password and checking for it to be remembered in Keychain Access.  But do I have to  change the password for each individual unit-- the TC, the Extreme and both Expresses-- or will changing it just for the TC alone work for the entire network?
    Resetting the password you will need to do for each device... the utility cannot even see those old units.
    So you will have to do it for each one.. think it through.. because as you change passwords the others will lose connection.. so start from the express which are wireless extending .. change those first.. and go back up the chain.. as each one changes it will drop off the network.. until you reach extreme and change that. Then you might need to reboot the whole network to get everything talking again. If something goes wrong.. just pluck that one out of the mix and plug in ethernet.. reset and redo the setup. That is my preferred method anyway.. do everything in isolation one by one. By ethernet and then nothing goes wrong.
    2)  Who's the treasonous SOB who spilled the beans to you about the ICBM in my back yard?!?
    N.Korean hackers.
    [Edit] Whoops-- one more question:  I want to partition the TC's disk, but Disk Utility doesn't see it.  What do I need to do?
    You cannot partition a network disk. And apple provided no tools for it in the TC itself. You can pull the disk out and partition it but that voids your warranty. (although done with care who is to know).
    Look at Q3 here.
    http://pondini.org/TM/Time_Capsule.html
    Mixing TM and data on the TC is worth planning carefully. They don't necessarily sit happily together.

  • How do I make the menu title a different color when it is the active menu in Spry horizontal menu?

    How do I make the menu title a different color when it is the active menu in Spry horizontal menu?
    Dreamweaver CS5.5
    Apple OS X.6.8
    View site at: http://dorsay-easton-indian-law.com/staging/index.html
    Steps:
    1. Click link to land on Home page
    2. View Home link in Spry horizontal navigation menu
    Actual:
    Home menu title is the same color as all of the others.
    Expected:
    The title of the active menu is       color: #FFC.
    I was hoping that a:active would give me this functionality, but that's not how it is described. Any suggestions are welcome!

    Nothing I have tried in the template makes the BODY tag editable. That's why I'm wondering how to disconnect the template from the individual pages. Adding an ID to each body tag is a prerequisite of the how-to page you supplied.
    You don't make the <body> tag editable in a Template.
    You make attributes of the <body> tag editable.
    Don't disconnect the Template from the child pages.
    Specify editable tag attributes in a template
    http://help.adobe.com/en_US/dreamweaver/cs/using/WScbb6b82af5544594822510a94ae8d65-7aa3a.h tml
    In the Template:
    Select the <body> tag (either in the Tag selector or click inside the <body> tag in Code View)
    Modify > Templates > Make Attribute Editable
    Select ID from the Attribute dropdown menu
    If there's no existing ID attribute then click Add and type ID into the next dialogue box
    Attribute: ID
    Check the box to "Make attribute editable"
    Enter anything into the Label field as a default e.g. foo
    OK
    Dreamweaver will change the <body> tag to read <body id="@@(foo)@@"....
    Now the ID is editable
    Save the Template and update the child page
    In each child page:
    Modify > Template Properties
    Select the id attribute from the list (it's probably the only one listed)
    Change its name in the box to whatever name matches your CSS rule for the active state for that page
    OK
    Save
    Upload

  • I would make the signature field read only until something is entered in the mandatory fields.

    I would make the signature field read only until something is entered in the mandatory fields.

    Mina,
    You can use LiveCycle Designer's Action Builder to easily achieve this.
    Please go through online documentation for the specific version of LiveCycle Designer that you are using.
    It is intuitive and very easy to use Action Builder (You will find this option under Tools Menu item of Designer).
    --Santosh

  • Cancelled contract lines are populating in the SR form's subject tab when clicking on 'Get Contracts' button

    Cancelled contract lines are populating in the SR form's subject tab when clicking on 'Get Contracts' button. But user says, it should not be populated. What could be the reason? Please suggest.
    I have told the user :
    "In the case of a renewed contract, if the service request is logged during the effectivity of the original contract (which is now Expired), the original contract will be retrieved. If the service request is logged during the effectivity of the renewal contract then only the renewal contract will be retrieved.
      Contract Entitlement on Subject tab is based on Contract Start and End dates. All the contracts i.e. expired, active, cancelled will appear until the end date. As we can see the contract ES-10165 is end dated on 09-DEC-2014, hence it will appear on the subject tab as per the functionality."
    But the User is saying:
    "Contract coverage is clear to be based on dates, and that is quite clear to me as well… the problem comes with the different statuses.
    A SR can be associated to an “EXPIRED” contract line because the SR creation date could be falling between coverage line start and end dates, but can never be related to a “CANCELLED” line.
    This “CANCELLED” status means that the line is not valid for the contract, so if it is not valid for a contract, it is not valid for referencing a job…and this is only creating wrong costs associations."

    Hi Sudhakar,
    Drop inline message components for each of the textFields and the textArea and then run the application. You could also use a message list component but inline message components for each of the input components would give you a clear idea if any errors are occuring. In case any such errors are indeed occuring then the situation can be analysed further.
    Hope this helps
    Cheers
    Giri :-)

  • Ay suggestions to connect my iPhone 5 to the Internet, it will only connect when sat next to the rooter, I have reset signals on both ?

    Ay suggestions to connect my iPhone 5 to the Internet, it will only connect when sat next to the rooter, I have reset signals on both ?

    What version of iOS?
    Is the router running the latest version of available firmware?
    Are any other devices having issues connecting to the router?
    Does the iPhone have the same issue with other Wi-Fi hot spots?

  • Before downloading the latest version of I-Tunes, I used to be able to view the ALBUM ARTWORK at bottom left when clicking on a song.   I am now unable to see the artwork.   Is it no longer possible with the updated software ??

    before downloading the latest version of I-Tunes, I used to be able to view the ALBUM ARTWORK at bottom left when clicking on a song.
    I am now unable to see the artwork.
    Is it no longer possible with the updated software ??

    Correct, Apple have removed that feature. However, you can still see the artwork of the song that's playing; it's shown in the Now Playing indicator at the top of the iTunes window. It is small, but if you click on it, a larger version pops up.
    Also, in Albums View, you can see the album artwork.

  • I tried to do the download of a magazine pays when clicked buy my ipad shows a text saying that I have to communicate with the support of the itunes store. I do not know what I have to do and how.

    I tried to do the download of a magazine pays when clicked buy my ipad shows a text saying that I have to communicate with the support of the itunes store. I do not know what I have to do and how.
    Help me 

    Hi gmoll5,
    Welcome to the Support Communities!
    If you have updated to iTunes 11, click on View > Show Sidebar
    Then, select your playlist and choose File > Print
    You will have the option to print a jewel case like you did before
    iTunes 11: Frequently used features
    http://support.apple.com/kb/HT5649http://support.apple.com/kb/HT5649
    iTunes 11 for Windows: Create your own CDs and DVDs
    http://support.apple.com/kb/PH12348
    iTunes can print a playlist formatted for use as a jewel case insert; choose Command-P and select “CD jewel case insert.”
    Cheers,
    - Judy

  • What is the procedure for disabling Entourage launching when clicking on an email address in Safari 5.0.5? I use Gmail.

    What is the procedure for disabling Entourage launching when clicking on an email address in Safari 5.0.5? I use Gmail.

    Is this about clicking a link in an external program?
    You can check this pref on the about:config page for external links.
    * browser.link.open_newwindow.override.external (-1)
    If this pref has the default value -1 then browser.link.open_newwindow is used.
    * http://kb.mozillazine.org/browser.link.open_newwindow
    *1: current tab; 2:new window; 3:new tab;
    You can open the <b>about:config</b> page via the location/address bar.
    You can accept the warning and click "I'll be careful" to continue.
    *http://kb.mozillazine.org/about:config

Maybe you are looking for