Results 1 to 3 of 3
  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Properly buffering a custom JButton

    So I made a custom JButton class:

    Java Code:
    package password_example;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
    
    @SuppressWarnings("serial")
    public class SafeButton extends JButton 
    {
    	
    	public SafeButton()
    	{
    		super();
    		setContentAreaFilled(false); 
    		setBorderPainted(false);
    		setIcon(createImageIcon("Images/lock.png"));
    		setRolloverIcon(createImageIcon("Images/lock_rot.gif"));
    		setPressedIcon(getRolloverIcon());
    		setFocusPainted(false);
    	}
    	
    	protected static ImageIcon createImageIcon(String path) 
    	{
    		java.net.URL imgURL = SafeButton.class.getResource(path);
    		if (imgURL != null) 
    		{
    			return new ImageIcon(imgURL);
    		} 
    		else 
    		{
    			System.err.println("Couldn't find file: " + path);
    			return null;
    		}
    	}
    
    }
    Now the problem is that the transition between lock.png and lock_rot.gif doesn't go smoothly
    As you move the cursor inside the JButton the Icon occasionally blinks before properly loading the .gif image

    Maybe working with:

    Java Code:
    	public void update(Graphics g) 
    		{
    			paintComponent(g);
    		}
    to enable doublebuffering would work? Not sure if this can apply to the JButton (as I generally use this to double buffer my Applets)

    So...how to remove the "blinking"?
    Last edited by Reskaillev; 04-08-2012 at 05:57 PM.

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

    Default Re: Properly buffering a custom JButton

    I think that to best be able to help you, we need to be able to compile and run your code and experience your problem. One thing I can say is to get rid of your update method override as there's no place for that here.

    I tried to reproduce your code using publicly available images but without any "blinking" effect:

    Java Code:
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class SafeButton extends JButton {
       public static final String IMAGE_1 = "http://upload.wikimedia.org/wikipedia/"
             + "commons/thumb/b/ba/262326_3cfa1c07_stmrvw.jpg/120px-262326_3cfa1c07_stmrvw.jpg";
       public static final String IMAGE_2 = "http://upload.wikimedia.org/wikipedia/"
             + "commons/thumb/6/6d/3390india.jpg/120px-3390india.jpg";
    
       public SafeButton() {
          super();
          setContentAreaFilled(false);
          setBorderPainted(false);
          // setIcon(createImageIcon("Images/lock.png"));
          setIcon(createImageIconFromUrl(IMAGE_1));
          // setRolloverIcon(createImageIcon("Images/lock_rot.gif"));
          // setPressedIcon(getRolloverIcon());
          
          setRolloverIcon(createImageIconFromUrl(IMAGE_2));
          setPressedIcon(getRolloverIcon());
          setFocusPainted(false);
       }
    
       private Icon createImageIconFromUrl(String imgUrl) {
          try {
             BufferedImage img = ImageIO.read(new URL(imgUrl));
             ImageIcon icon = new ImageIcon(img);
             return icon;
          } catch (MalformedURLException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
          return null;
       }
    
       protected static ImageIcon createImageIcon(String path) {
          java.net.URL imgURL = SafeButton.class.getResource(path);
          if (imgURL != null) {
             return new ImageIcon(imgURL);
          } else {
             System.err.println("Couldn't find file: " + path);
             return null;
          }
       }
       
       public static void main(String[] args) {
          SafeButton safeButton = new SafeButton();
          JPanel panel = new JPanel();
          panel.add(safeButton);
          JOptionPane.showMessageDialog(null, panel);
       }
    
    }

  3. #3
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Re: Properly buffering a custom JButton

    Here are the images:

    Properly buffering a custom JButton-lock.png

    Properly buffering a custom JButton-lock_rot.gif

    If you quickly move over the button you will notice that the JVM blinks shortly.
    The .gif is made out of 71 frames? Might be overkill for the JVM?

Similar Threads

  1. Replies: 10
    Last Post: 03-11-2012, 07:07 AM
  2. Help, I can't make the JButton to work properly
    By furieux in forum New To Java
    Replies: 11
    Last Post: 01-07-2012, 06:13 PM
  3. Custom JButton
    By adwart in forum AWT / Swing
    Replies: 2
    Last Post: 07-27-2011, 04:17 AM
  4. Custom JButton with flow layout
    By phil128 in forum AWT / Swing
    Replies: 1
    Last Post: 01-17-2011, 11:44 PM
  5. [SOLVED] JButton does not display ImageIcon properly
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 04:47 AM

Posting Permissions

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