Results 1 to 11 of 11
Like Tree1Likes

Thread: JButton States Set to Pictures From Image Editor

  1. #1
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default JButton States Set to Pictures From Image Editor

    I am aiming to design a circular JButton that has its states set to images that I create in an Image Editor. I have been told that by creating a JButton and removing its default borders, the button's shape will be determined by its icon. I decided to try this out and designed a simple program to do so. The image Redinner.gif is a simple image I created on an image editor, a 50 by 50 pixel red circle.

    public static void main(String[] args) {
    ImageIcon myIcon = new ImageIcon("images/Redinner.gif");
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.add(panel);
    JButton test = new JButton(myIcon);
    test.setBorderPainted(false);
    test.setFocusPainted(false);
    panel.add(test);
    Dimension dim = new Dimension(80, 80);
    test.setPreferredSize(dim);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE );
    }

    I am wondering if the path I used, images/Redinner.gif, images being the folder it is in, contains the problem. There was no error, however the frame came up blank. Moreover, the image editor only allows me to build off of a rectangular window. Thus, I don't understand the point of setting the shape of a JButton to an image as it will only be rectangular, and is juts a more complex way of adjusting the size of a JButton. Thank you very much for your time.

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

    Default

    You need a valid link and the ImageIcon would require a URL. If you use a String, Java will assume that you man a file name (and how could it know any different). If you want to use a web address, you'll need to wrap it in a URL.

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

    Default

    You also will want to post only compilable runnable well formatted code with code tags. For e.g.,

    Java Code:
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    
    public class FooSwing01 {
       public static final String IMAGE_1 = "http://duke.kenai.com/iconSized/duke4.gif";
       public static final String IMAGE_2 = "http://www.java-forums.org/images/Redinner.gif";
    
       public static void main(String[] args) throws MalformedURLException {
          ImageIcon myIcon = new ImageIcon(new URL(IMAGE_1));
          JFrame frame = new JFrame();
          JPanel panel = new JPanel() {
             @Override
             protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Paint paint = new GradientPaint(0, 0, Color.blue, 30, 30, Color.red, true);
                Graphics2D g2 = (Graphics2D)g;
                g2.setPaint(paint);
                g2.fillRect(0, 0, getWidth(), getHeight());
             }
          };
          frame.add(panel);
          JButton test = new JButton(myIcon);
          test.setBorderPainted(false);
          test.setFocusPainted(false);
          test.setOpaque(false); 
          test.setBackground(new Color(0, 0, 0, 0));
          panel.add(test);
          Dimension dim = new Dimension(80, 80);
          test.setPreferredSize(dim);
          frame.pack(); 
          frame.setLocationRelativeTo(null); 
          frame.setVisible(true);
          frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
       }
    
    }

  4. #4
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default

    Sorry if I wasn't clear, I was trying to load a file name.

  5. #5
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default

    And also, why is it that the jbutton is no longer opaque after it is pressed?

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    I don't understand the point of setting the shape of a JButton to an image as it will only be rectangular,
    If the image contains transparent pixels outside of the round image then the button will appear round. You can also make it react only to mouse clicks in the round area of the image by overriding the contains() method of the JButton:

    Java Code:
        // Hit detection.
        Shape shape;
        public boolean contains(int x, int y) {
            // If the button has changed size, make a new shape object.
            if (shape == null || !shape.getBounds().equals(getBounds())) {
                shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
            }
            return shape.contains(x, y);
        }

  7. #7
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default

    I still am unable to take an image file and use it as an ImageIcon.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by wired-in=p View Post
    I still am unable to take an image file and use it as an ImageIcon.
    Perhaps you are not looking in the right place for your image:

    Java Code:
    [color=green]System.out.println("Looking for image in " + new File("Redinner.gif").getAbsolutePath());[/color]
    ImageIcon myIcon = new ImageIcon("Redinner.gif");
    [color=green]System.out.println("Found image " + myIcon.getImage());[/color]
    // etc
    [Edit] "Redinner.gif" should be preceeded by the images directory both times.

    Fubarable's suggestion (compilable, runnable, formatted code) is good - and I'll add brief. This gives everyone more to work on than "I am still unable...".
    Last edited by pbrockway2; 08-21-2011 at 03:19 AM.

  9. #9
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default

    what do i use the absolute path for. do i do..
    ImageIcon myIcon = new ImageIcon(myabsolutepath/Redinner.gif);

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You print the path in order to verify that you are looking for the image in the same place that the image file exists.

  11. #11
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default

    So the only point in printing the path is to use it as a reference to see if your looking in the right place.I have determined that the image is in the right place, however the program still won't work,.
    Last edited by wired-in=p; 08-21-2011 at 04:38 AM.

Similar Threads

  1. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  2. image on a JButton...how??
    By Habib in forum AWT / Swing
    Replies: 2
    Last Post: 02-27-2011, 06:56 PM
  3. select and load image onto editor pane
    By vikram.jamakhandi in forum AWT / Swing
    Replies: 0
    Last Post: 03-03-2010, 11:44 AM
  4. Java image editor
    By jokerbla in forum New To Java
    Replies: 0
    Last Post: 04-11-2009, 04:57 PM
  5. adding Image to JButton
    By mayhewj7 in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 03:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •