Example of an icon that changes form : Icon « 2D Graphics GUI « Java






Example of an icon that changes form

Example of an icon that changes form
     
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) {
    final JSlider width = new JSlider(JSlider.HORIZONTAL, 1, 150, 75);
    final JSlider height = new JSlider(JSlider.VERTICAL, 1, 150, 75);

    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);

    class Updater implements ChangeListener {
      public void stateChanged(ChangeEvent ev) {
        dynamicLabel.repaint();
      }
    }
    Updater updater = new Updater();

    width.addChangeListener(updater);
    height.addChangeListener(updater);

    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);
  }
  
}

           
         
    
    
    
    
  








Related examples in the same category

1.Create a dynamic icon
2.A simple application to test the functionality of the OvalIcon classA simple application to test the functionality of the OvalIcon class
3.Icon DisplayerIcon Displayer
4.Implement the Icon interfaceImplement the Icon interface
5.Calendar Page icons with Weekday, Day and MonthCalendar Page icons with Weekday, Day and Month
6.MemImage is an in-memory icon showing a Color gradientMemImage is an in-memory icon showing a Color gradient
7.Custom Icon DemoCustom Icon Demo
8.Create Image Icon from PNG file
9.Reading an Image or Icon from a File
10.Draw an Icon object
11.Plain Color Icon
12.Color Icon
13.Arrow Icon
14.Icon Line
15.Return a filled oval as an Icon
16.Layered Icon
17.An icon for painting a square swatch of a specified Color.
18.An empty icon with arbitrary width and height.
19.Transparent icon with no content
20.Creates a transparent icon.
21.Icon Codec