Results 1 to 1 of 1
-
Example of an icon that changes form
This Java tip shows how to create an icon that changes from at runtime.
Java Code:import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class DynamicIconExample { public static void main(String[] args) { // Create a couple of sliders to control the icon size. final JSlider width = new JSlider(JSlider.HORIZONTAL, 1, 150, 75); final JSlider height = new JSlider(JSlider.VERTICAL, 1, 150, 75); // A little icon class that uses the current slider values. class DynamicIcon implements Icon { public int getIconWidth() { return width.getValue(); } public int getIconHeight() { return height.getValue(); } public void paintIcon(Component c, Graphics g, int x, int y) { g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true); } } ; Icon icon = new DynamicIcon(); final JLabel dynamicLabel = new JLabel(icon); // A listener to repaint the icon when sliders are adjusted. class Updater implements ChangeListener { public void stateChanged(ChangeEvent ev) { dynamicLabel.repaint(); } } ; Updater updater = new Updater(); width.addChangeListener(updater); height.addChangeListener(updater); // Lay it all out. JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); c.add(width, BorderLayout.NORTH); c.add(height, BorderLayout.WEST); c.add(dynamicLabel, BorderLayout.CENTER); f.setSize(210, 210); f.setVisible(true); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
How to change Window Icon
By sharafat in forum AWT / SwingReplies: 7Last Post: 01-15-2010, 12:37 AM -
icon
By amith in forum AWT / SwingReplies: 1Last Post: 05-16-2008, 07:34 PM -
How to set an Icon in a Label?
By Soda in forum New To JavaReplies: 2Last Post: 12-07-2007, 12:38 PM -
how to remove an image icon
By cecily in forum Advanced JavaReplies: 1Last Post: 08-05-2007, 04:25 AM -
To add an icon to my project
By Albert in forum AWT / SwingReplies: 1Last Post: 07-13-2007, 03:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks