Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating button from image and selecting clickable area

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Creating button from image and selecting clickable area

    Maybe it is not possible, but i'm just wonderring, if it's possible and if there is an easy to do this : i have a button with icon, and my icon is in circle shape, but button is quadratic, so i would like to leave visible only icon, and make clickable only the icon, not the whole hidden button. Is it possible ? And if it is, maybe there is another easy way to do this ?
    I.e., i have a button http://s23.postimg.org/bf7n6j397/aaa.png , red is my icon, and blue is the rest of the button, and want to make a blue area unclickable, only red would act as a button. The point is that i want a circle button.

    My code is something like that :

    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import java.awt.Insets;
     
    class ImageButton extends JButton {
     
      public ImageButton(String img) {
        this(new ImageIcon(img));
      }
     
      public ImageButton(ImageIcon icon) {
        setIcon(icon);
        setMargin(new Insets(0, 0, 0, 0));
        setIconTextGap(0);
        setBorderPainted(false);
        setOpaque(false);
        setBorder(null);
        setText(null);
        setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
      }
     
    }
     
    final ImageButton button = new ImageButton("http://www.javaprogrammingforums.com/images/Default.png");


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Creating button from image and selecting clickable area

    Check out the API for the JButton class for useful functions including setting the border and the background. If that doesn't work, you can always provide your own custom UI or extend JButton.

    --- Update ---

    Edit: I see what you're doing now. You might want to think about overriding the getBounds() function so it only returns the area of your image.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 5
    Last Post: August 10th, 2013, 03:21 PM
  2. Display an image after mouse cursor point on a text area
    By samseah123 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 19th, 2013, 02:24 PM
  3. Selecting and dragging a image from multiple image in Java Applet
    By CY5 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 02:44 PM
  4. Creating a close button
    By Zomby in forum Java Theory & Questions
    Replies: 13
    Last Post: October 17th, 2012, 04:13 AM
  5. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM