核心Swing组件(四)
4.4 Icon接口
Icon接口用来将图标与各种组件相关联。一个图标可以是简单的绘画或者是使用ImageIcon类由磁盘所载入的GIF图像。这个接口包含描述尺寸的两个属性以及一个用来绘制图标的方法。
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 创建图标
图标的创建非常简单,只需要简单的实现接口。我们所需要做的就是指定图标的尺寸以及要绘制的内容。列表4-3演示了一个Icon的实现。这个图标是一个菱形图标,其尺寸,颜色以及填充状态都是可以配置的。
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); } @Override public int getIconHeight() { // TODO Auto-generated method stub return height; } @Override public int getIconWidth() { // TODO Auto-generated method stub return width; } @Override public void paintIcon(Component c, Graphics g, int x, int y) { // TODO Auto-generated method stub g.setColor(color); g.translate(x, y); if(selected) { g.fillPolygon(polygon); } else { g.drawPolygon(polygon); } g.translate(-x, -y); } }4.4.2 使用图标
一旦我们有了Icon的实现,使用Icon就如何查看一个组件具有相应的属性一样简单。例如,下面的代码创建了一个具有图标的标签:
Icon icon = new DiamondIcon(Color.RED, true, 25, 25); JLabel label = new JLabel(icon);图4-10显这个标签的运行结果。
4.4.3 ImageIcon类
ImageIcon类提供了由AWT Image对象创建图标的Icon接口实现,Image对象可以来自内存(byte[]),来自磁盘(文件名)或是来自网络(URL)。与普通的Image对象不同,ImageIcon的载入是当ImageIcon被创建时立即启动的,尽管当使用时他也许还没有完全载入。另外,与Image对象不同,ImageIcon对象是可序列化的,所以他们可以很容易为JavaBean组件所使用。
创建ImageIcon
有九个构造函数可以用于创建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");无参数的构造函数创建一个未初始化的版本。其余的八个构造函数提供了由Image,byte数组,文件名String或
补充:软件开发 , Java ,