Results 1 to 3 of 3
- 04-08-2012, 04:38 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Properly buffering a custom JButton
So I made a custom JButton class:
Now the problem is that the transition between lock.png and lock_rot.gif doesn't go smoothlyJava 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; } } }
As you move the cursor inside the JButton the Icon occasionally blinks before properly loading the .gif image
Maybe working with:
to enable doublebuffering would work? Not sure if this can apply to the JButton (as I generally use this to double buffer my Applets)Java Code:public void update(Graphics g) { paintComponent(g); }
So...how to remove the "blinking"?Last edited by Reskaillev; 04-08-2012 at 04:57 PM.
-
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); } }
- 04-08-2012, 08:32 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Similar Threads
-
I Really Need Help Understand Buffering Images And How To Properly Use Graphics...
By AmpedNavy in forum New To JavaReplies: 10Last Post: 03-11-2012, 06:07 AM -
Help, I can't make the JButton to work properly
By furieux in forum New To JavaReplies: 11Last Post: 01-07-2012, 05:13 PM -
Custom JButton
By adwart in forum AWT / SwingReplies: 2Last Post: 07-27-2011, 03:17 AM -
Custom JButton with flow layout
By phil128 in forum AWT / SwingReplies: 1Last Post: 01-17-2011, 10:44 PM -
[SOLVED] JButton does not display ImageIcon properly
By Singing Boyo in forum New To JavaReplies: 1Last Post: 04-17-2009, 03:47 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks