核心Swing组件(四)

  Icon接口用来将图标与各种组件相关联。一个图标可以是简单的绘画或者是使用ImageIcon类由磁盘所载入的GIF图像。这个接口包含描述尺寸的两个属性以及一个用来绘制图标的方法。 图标的创建非常简单,只需要简单的实现接口。我们所需要做的就是指定图标的尺寸以及要绘制的内容。列表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; publicclass DiamondIcon implements Icon { private Color color; privateboolean selected; privateint width; privateint height; private Polygon polygon; privatestaticfinalint DEFAULT_WIDTH = 10; privatestaticfinalint 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(); } privatevoid 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 publicint getIconHeight() { // TODO Auto-generated method stubreturn height; } @Override publicint getIconWidth() { // TODO Auto-generated method stubreturn width; } @Override publicvoid 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); } }
  一旦我们有了Icon的实现,使用Icon就如何查看一个组件具有相应的属性一样简单。例如,下面的代码创建了一个具有图标的标签: 图4-10显这个标签的运行结果。
  
  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或是URL,带有或是不带有描述来创建ImageIcon的功能。
  使用ImageIcon
  使用ImageIcon就如同使用Icon一样简单:仅需要创建ImageIcon并将其组件相关联。 ImageIcon属性 表4-10显示了ImageIcon的六个属性。ImageIcon的高与宽是实际的Image对象的高与宽。imageLoadStatus属性表示由隐藏MediaTracker载入ImageIcon的结果,或者是MediaTracker.ABORTED,MediaTracker.ERRORED,MediaTracker.COMPLETE。
  ImageIcon属性 有时使用ImageIcon来载入一个Image,然后由Image对象获取Icon是十分有用的。 使用ImageIcon对象时有一个主要问题:使用图标的图像与类文件都是由JAR文件载入时,他们不能工作,除非我们为JAR中的文件指定了完全的URL。我们不能仅仅指定文件名为一个String并使得ImageIcon查找这个文件。我们必须首先手动获取图像数据,然后将这些数据传递给ImageIcon构造函数。
  为了解决在JAR文件外部载入图像,列表4-4显示了一个ImageLoader类,这个类提供了一个public static Image getImage(Class relativeClass, String filename)方法。我们同时指定图像文件相对的基类以及图像文件的名字。然后我们只需要将返回的Image对象传递给ImageIcon的构造函数。 package swingstudy.ch04; import java.awt.Image; import java.awt.Toolkit; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; publicclass ImageLoader { private ImageLoader() { } publicstatic Image getImage(Class relativeClass, String filename) { Image returnValue = null; InputStream is = relativeClass.getResourceAsStream(filename); if(is != null) { BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int ch; while ((ch = bis.read()) != -1) { baos.write(ch); } returnValue = Toolkit.getDefaultToolkit().createImage(baos.toByt eArray()); } catch(IOException e) { System.err.println("Error loading: "+filename); } } return returnValue; } }
  下面的代码显示如何使用这个帮助类:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值