核心Swing組件(四) – JAVA編程語言程序開發技術文章

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或

發佈留言