zoukankan      html  css  js  c++  java
  • 核心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显这个标签的运行结果。

    Swing_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或是URL,带有或是不带有描述来创建ImageIcon的功能。

    使用ImageIcon

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

    Icon icon = new ImageIcon("Warn.gif");
    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是十分有用的。

    ImageIcon imageIcon = new ImageIcon(...);
    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的构造函数。

    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;
     
    public class ImageLoader {
     
    	private ImageLoader() {
     
    	}
     
    	public static 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.toByteArray());
    			}
    			catch(IOException e) {
    				System.err.println("Error loading: "+filename);
    			}
    		}
    		return returnValue;
    	}
    }

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

    Image warnImage = ImageLoader.getImage(LabelJarSample.class, "Warn.gif");
    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)。

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

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

    Icon warningIcon = new ImageIcon(grayImage);
    JLabel warningLabel = new JLabel(warningIcon);
  • 相关阅读:
    Flutter-现有iOS工程引入Flutter
    EventBus 3.0使用详解
    一步一步教你实现iOS音频频谱动画(二)
    一步一步教你实现iOS音频频谱动画(一)
    iOS热更新实现方式
    github 远程仓库名或地址修改,本地如何同步
    Xmind ZEN破解版来袭:如何去除水印
    flutter packages get 慢 解决方案
    web计时机制——performance对象
    jQuery设计思想
  • 原文地址:https://www.cnblogs.com/dyllove98/p/2461920.html
Copyright © 2011-2022 走看看