Core Swing components (IV)

Source: Internet
Author: User

4.4 Icon Interface

The Icon interface is used to associate an Icon with various components. An icon can be a simple painting or a GIF image loaded by a disk using the ImageIcon class. This interface contains two attributes describing the size and a method for drawing icons.

public interface Icon {  // Properties   public int getIconHeight();  public int getIconWidth();  // Other methods  public void paintIcon(Component c, Graphics g, int x, int y);}
4.4.1 create icon

The creation of icons is very simple. You only need simple interfaces. All we need to do is to specify the Icon size and the content to be drawn. List 4-3 demonstrates the implementation of an Icon. This icon is a diamond icon. Its size, color, and fill status can be configured.

package swingstudy.ch04; import java.awt.Color;import java.awt.Component;import java.awt.Graphics;import java.awt.Polygon; import javax.swing.Icon; public class DiamondIcon implements Icon { private Color color;private boolean selected;private int width;private int height;private Polygon polygon;private static final int DEFAULT_WIDTH = 10;private static final int DEFAULT_HEIGHT = 10; public DiamondIcon(Color color) {this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);} public DiamondIcon(Color color, boolean selected) {this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);} public DiamondIcon(Color color, boolean selected, int width, int height) {this.color = color;this.selected = selected;this.width = width;this.height = height;initPolygon();} private void initPolygon() {polygon = new Polygon();int halfWidth = width/2;int halfHeight = height/2;polygon.addPoint(0, halfHeight);polygon.addPoint(halfWidth, 0);polygon.addPoint(width, halfHeight);polygon.addPoint(halfWidth, height);}@Overridepublic int getIconHeight() {// TODO Auto-generated method stubreturn height;} @Overridepublic int getIconWidth() {// TODO Auto-generated method stubreturn width;} @Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {// TODO Auto-generated method stubg.setColor(color);g.translate(x, y);if(selected) {g.fillPolygon(polygon);}else {g.drawPolygon(polygon);}g.translate(-x, -y);} }
4.4.2 use icons

Once we have the Icon implementation, it is as simple as viewing how a component has the corresponding attributes using the Icon. For example, the following code creates a label with an icon:

Icon icon = new DiamondIcon(Color.RED, true, 25, 25); JLabel label = new JLabel(icon); 

Figure 4-10 shows the running result of this label.

4.4.3 ImageIcon class

The ImageIcon class provides an Icon interface for creating icons for the AWT Image object. The Image object can come from memory (byte []), disk (file name), or network (URL ). Unlike normal Image objects, ImageIcon loading starts immediately when it is created, although it may not be fully loaded yet. In addition, unlike Image objects, ImageIcon objects can be serialized, so they can be easily used by the JavaBean Component.

Create ImageIcon

Nine constructors can be used to create an ImageIcon:

public ImageIcon()Icon icon = new ImageIcon();icon.setImage(anImage); public ImageIcon(Image image)Icon icon = new ImageIcon(anImage); public ImageIcon(String filename)Icon icon = new ImageIcon(filename); public ImageIcon(URL location)Icon icon = new ImageIcon(url); public ImageIcon(byte imageData[])Icon icon = new ImageIcon(aByteArray); public ImageIcon(Image image, String description)Icon icon = new ImageIcon(anImage, "Duke"); public ImageIcon(String filename, String description)Icon icon = new ImageIcon(filename, filename);public ImageIcon(URL location,
 String description)Icon icon = new ImageIcon(url, location.getFile()); public ImageIcon(URL location, String description)Icon icon = new ImageIcon(url, location.getFile()); public ImageIcon(byte imageData[], String description)Icon icon = new ImageIcon(aByteArray, "Duke");

A constructor without parameters creates an uninitialized version. The remaining eight constructors provide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.