Chinaunix首页 | 论坛 | 博客
  • 博客访问: 897001
  • 博文数量: 96
  • 博客积分: 10681
  • 博客等级: 上将
  • 技术积分: 2449
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-16 17:52
文章分类

全部博文(96)

文章存档

2011年(30)

2009年(36)

2008年(30)

分类: Java

2011-03-09 22:52:36

4.4 Icon接口

Icon接口用来将图标与各种组件相关联。一个图标可以是简单的绘画或者是使用ImageIcon类由磁盘所载入的GIF图像。这个接口包含描述尺寸的两个属性以及一个用来绘制图标的方法。

  1. public interface Icon {
  2.   // Properties
  3.   public int getIconHeight();
  4.   public int getIconWidth();
  5.   // Other methods
  6.   public void paintIcon(Component c, Graphics g, int x, int y);
  7. }

4.4.1 创建图标

图标的创建非常简单,只需要简单的实现接口。我们所需要做的就是指定图标的尺寸以及要绘制的内容。列表4-3演示了一个Icon的实现。这个图标是一个菱形图标,其尺寸,颜色以及填充状态都是可以配置的。

  1. package swingstudy.ch04;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Polygon;
  7.  
  8. import javax.swing.Icon;
  9.  
  10. public class DiamondIcon implements Icon {
  11.  
  12.     private Color color;
  13.     private boolean selected;
  14.     private int width;
  15.     private int height;
  16.     private Polygon polygon;
  17.     private static final int DEFAULT_WIDTH = 10;
  18.     private static final int DEFAULT_HEIGHT = 10;
  19.  
  20.     public DiamondIcon(Color color) {
  21.         this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  22.     }
  23.  
  24.     public DiamondIcon(Color color, boolean selected) {
  25.         this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  26.     }
  27.  
  28.     public DiamondIcon(Color color, boolean selected, int width, int height) {
  29.         this.color = color;
  30.         this.selected = selected;
  31.         this.width = width;
  32.         this.height = height;
  33.         initPolygon();
  34.     }
  35.  
  36.     private void initPolygon() {
  37.         polygon = new Polygon();
  38.         int halfWidth = width/2;
  39.         int halfHeight = height/2;
  40.         polygon.addPoint(0, halfHeight);
  41.         polygon.addPoint(halfWidth, 0);
  42.         polygon.addPoint(width, halfHeight);
  43.         polygon.addPoint(halfWidth, height);
  44.     }
  45.     @Override
  46.     public int getIconHeight() {
  47.         // TODO Auto-generated method stub
  48.         return height;
  49.     }
  50.  
  51.     @Override
  52.     public int getIconWidth() {
  53.         // TODO Auto-generated method stub
  54.         return width;
  55.     }
  56.  
  57.     @Override
  58.     public void paintIcon(Component c, Graphics g, int x, int y) {
  59.         // TODO Auto-generated method stub
  60.         g.setColor(color);
  61.         g.translate(x, y);
  62.         if(selected) {
  63.             g.fillPolygon(polygon);
  64.         }
  65.         else {
  66.             g.drawPolygon(polygon);
  67.         }
  68.         g.translate(-x, -y);
  69.     }
  70.  
  71. }

4.4.2 使用图标

一旦我们有了Icon的实现,使用Icon就如何查看一个组件具有相应的属性一样简单。例如,下面的代码创建了一个具有图标的标签:

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

图4-10显这个标签的运行结果。

Swing_4_10

4.4.3 ImageIcon类

ImageIcon类提供了由AWT Image对象创建图标的Icon接口实现,Image对象可以来自内存(byte[]),来自磁盘(文件名)或是来自网络(URL)。与普通的Image对象不同,ImageIcon的载入是当ImageIcon被创建时立即启动的,尽管当使用时他也许还没有完全载入。另外,与Image对象不同,ImageIcon对象是可序列化的,所以他们可以很容易为JavaBean组件所使用。

创建ImageIcon

有九个构造函数可以用于创建ImageIcon:

  1. public ImageIcon()
  2. Icon icon = new ImageIcon();
  3. icon.setImage(anImage);
  4.  
  5. public ImageIcon(Image image)
  6. Icon icon = new ImageIcon(anImage);
  7.  
  8. public ImageIcon(String filename)
  9. Icon icon = new ImageIcon(filename);
  10.  
  11. public ImageIcon(URL location)
  12. Icon icon = new ImageIcon(url);
  13.  
  14. public ImageIcon(byte imageData[])
  15. Icon icon = new ImageIcon(aByteArray);
  16.  
  17. public ImageIcon(Image image, String description)
  18. Icon icon = new ImageIcon(anImage, "Duke");
  19.  
  20. public ImageIcon(String filename, String description)
  21. Icon icon = new ImageIcon(filename, filename);public ImageIcon(URL location, String description)
  22. Icon icon = new ImageIcon(url, location.getFile());
  23.  
  24. public ImageIcon(URL location, String description)
  25. Icon icon = new ImageIcon(url, location.getFile());
  26.  
  27. public ImageIcon(byte imageData[], String description)
  28. Icon icon = new ImageIcon(aByteArray, "Duke");

无参数的构造函数创建一个未初始化的版本。其余的八个构造函数提供了由Image,byte数组,文件名String或是URL,带有或是不带有描述来创建ImageIcon的功能。

使用ImageIcon

使用ImageIcon就如同使用Icon一样简单:仅需要创建ImageIcon并将其组件相关联。

  1. Icon icon = new ImageIcon("Warn.gif"); 
  2. JLabel label3 = new JLabel("Warning", icon, JLabel.CENTER)

ImageIcon属性

表4-10显示了ImageIcon的六个属性。ImageIcon的高与宽是实际的Image对象的高与宽。imageLoadStatus属性表示由隐藏MediaTracker载入ImageIcon的结果,或者是MediaTracker.ABORTED,MediaTracker.ERRORED,MediaTracker.COMPLETE。

ImageIcon属性

属性名
数据类型

访问性

description
String

读写

iconHeight
int

只读

iconWidth
int

只读

image
Image

读写

imageLoadStatus
int

只读

imageObserver
ImageObserver

读写

有时使用ImageIcon来载入一个Image,然后由Image对象获取Icon是十分有用的。

  1. ImageIcon imageIcon = new ImageIcon(...); 
  2. Image image = imageIcon.getImage();

使用ImageIcon对象时有一个主要问题:使用图标的图像与类文件都是由JAR文件载入时,他们不能工作,除非我们为JAR中的文件指定了完全的URL。我们不能仅仅指定文件名为一个String并使得ImageIcon查找这个文件。我们必须首先手动获取图像数据,然后将这些数据传递给ImageIcon构造函数。

为了解决在JAR文件外部载入图像,列表4-4显示了一个ImageLoader类,这个类提供了一个public static Image getImage(Class relativeClass, String filename)方法。我们同时指定图像文件相对的基类以及图像文件的名字。然后我们只需要将返回的Image对象传递给ImageIcon的构造函数。

  1. package swingstudy.ch04;
  2.  
  3. import java.awt.Image;
  4. import java.awt.Toolkit;
  5. import java.io.BufferedInputStream;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9.  
  10. public class ImageLoader {
  11.  
  12.     private ImageLoader() {
  13.  
  14.     }
  15.  
  16.     public static Image getImage(Class relativeClass, String filename) {
  17.         Image returnValue = null;
  18.         InputStream is = relativeClass.getResourceAsStream(filename);
  19.         if(is != null) {
  20.             BufferedInputStream bis = new BufferedInputStream(is);
  21.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  22.             try {
  23.                 int ch;
  24.                 while ((ch = bis.read()) != -1) {
  25.                     baos.write(ch);
  26.                 }
  27.                 returnValue = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
  28.             }
  29.             catch(IOException e) {
  30.                 System.err.println("Error loading: "+filename);
  31.             }
  32.         }
  33.         return returnValue;
  34.     }
  35. }

下面的代码显示如何使用这个帮助类:

  1. Image warnImage = ImageLoader.getImage(LabelJarSample.class, "Warn.gif"); 
  2. Icon warnIcon = new ImageIcon(warnImage); JLabel label2 = new JLabel(warnIcon);

 4.4.4 GrayFilter类

另一个值得一提的类就是GrayFilter类。许多Swing组件依赖这个类来创建一个禁止的Image版本用作Icon。组件自动使用这个类,但是有时我们需要使用AWT的ImageFilter类实现灰度平衡。我们可以通过调用类的一个方法将一个Image由普通形式转换为灰度形式:public static Image crateDisabledImage(Image image)。

  1. Image normalImage = ... Image grayImage = GrayFilter.createDisabledImage(normalImage)

现在我们可以使用一个灰色的图像作为组件的Icon:

  1. Icon warningIcon = new ImageIcon(grayImage); 
  2. JLabel warningLabel = new JLabel(warningIcon);
阅读(915) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~