public class ImageBorder extends AbstractBorder {
Image top_center, top_left, top_right;
Image left_center, right_center;
Image bottom_center, bottom_left, bottom_right;
Insets insets;
public ImageBorder(Image top_left, Image top_center, Image top_right,
Image left_center, Image right_center, Image bottom_left,
Image bottom_center, Image bottom_right) {
this.top_left = top_left;
this.top_center = top_center;
this.top_right = top_right;
this.left_center = left_center;
this.right_center = right_center;
this.bottom_left = bottom_left;
this.bottom_center = bottom_center;
this.bottom_right = bottom_right;
}
public void setInsets(Insets insets) {
this.insets = insets;
}
@Override
public Insets getBorderInsets(Component c) {
if (insets != null) {
return insets;
} else {
int top = top_center.getHeight(null);
int left = left_center.getWidth(null);
int bottom = left_center.getWidth(null);
int right = right_center.getHeight(null);
return new Insets(top, left, bottom, right);
}
}
@SuppressWarnings("unused")
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
//绘制背景色
g.setColor(Color.white);
g.fillRect(x, y, width, height);
Graphics2D g2 = (Graphics2D) g;
int tlw = top_left.getWidth(null);
int tlh = top_left.getHeight(null);
int tcw = top_center.getWidth(null);
int tch = top_center.getHeight(null);
int trw = top_right.getWidth(null);
int trh = top_right.getHeight(null);
int lcw = left_center.getWidth(null);
int lch = left_center.getHeight(null);
int rcw = right_center.getWidth(null);
int rch = right_center.getHeight(null);
int blw = bottom_left.getWidth(null);
int blh = bottom_left.getHeight(null);
int bcw = bottom_center.getWidth(null);
int bch = bottom_center.getHeight(null);
int brw = bottom_right.getWidth(null);
int brh = bottom_right.getHeight(null);
fillTexture(g2, top_left, x, y, tlw, tlh);
fillTexture(g2, top_center, x + tlw, y, width - tlw - trw, tch);
fillTexture(g2, top_right, x + width - trw, y, trw, trh);
fillTexture(g2, left_center, x, y + tlh, lcw, height - tlh - blh);
fillTexture(g2, right_center, x + width - rcw, y + trh, rcw, height
- trh - brh);
fillTexture(g2, bottom_left, x, y + height - blh, blw, blh);
fillTexture(g2, bottom_center, x + blw, y + height - bch, width - blw
- brw, bch);
fillTexture(g2, bottom_right, x + width - brw, y + height - brh, brw,
brh);
}
public void fillTexture(Graphics2D g2, Image img, int x, int y, int w, int h) {
BufferedImage buff = createBufferedImage(img);
// 根据指定图像的大小创建一个基准矩形
Rectangle anchor = new Rectangle(x, y, img.getWidth(null),
img.getHeight(null));
/** TexturePaint 类提供一种用被指定为 BufferedImage 的纹理填充 Shape 的方式 **/
// 将buff复制定位到anchor中
TexturePaint paint = new TexturePaint(buff, anchor);
// 为 Graphics2D 设置 TexturePaint 属性
g2.setPaint(paint);
//根据位置填充矩形
g2.fillRect(x, y, w, h);
}
/** 将Image转化为BufferedImage **/
public BufferedImage createBufferedImage(Image img) {
// 创建一个与原图大小相同的缓冲空图像
// 图像类型为8位ARGB颜色分量(24b全彩色,并带有一个alpha通道)
BufferedImage buff = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// 创建一个 Graphics2D,可以将它绘制到此 BufferedImage中
Graphics gfx = buff.createGraphics();
// 绘制指定图像中当前可用的图像
gfx.drawImage(img, 0, 0, null);
// 释放Graphics使用的所有系统资源,释放之后不能再使用 此对象
gfx.dispose();
// 返回将Image的缓冲对象
return buff;
}
}
/** 调用情况
ImageBorder image_border = new ImageBorder(upper_left, upper,
upper_right, left_center, right_center, bottom_left,
bottom_center, bottom_right);
panel.setBorder(image_border);(或者JButton)
*/
|
public class RichLabel extends JLabel {
private int tracking;
private int left_x, left_y, right_x, right_y;
private Color left_color, right_color;
/**
*
* @param text :需要显示的文本
* @param tracking :文本字符间隙大小
*/
public RichLabel(String text, int tracking) {
super(text);
this.tracking = tracking;
}
public void setLeftShadow(int x, int y, Color color) {
this.left_x = x;
this.left_y = y;
this.left_color = color;
}
public void setRightShadow(int x, int y, Color color) {
this.right_x = x;
this.right_y = y;
this.right_color = color;
}
@Override
public Dimension getPreferredSize() {
String text = getText();
FontMetrics fm = getFontMetrics(getFont());
int w = fm.stringWidth(text);
w += (text.length() - 1) * tracking;
w += left_x + right_x;
int h = fm.getHeight();
h += left_y + right_y;
return new Dimension(w, h);
}
@Override
protected void paintComponent(Graphics g) {
// 将文本抗锯齿功能打开
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
char[] chars = getText().toCharArray();
// FontMetrics 类含有很多获取当前文本字体高和宽度的方法
FontMetrics fm = getFontMetrics(getFont());
// 获取基线到字体顶部的距离
int h = fm.getAscent();
int x = 0;
for (int i = 0; i < chars.length; i++) {
char ch = chars[i];
int w = fm.charWidth(ch) + tracking;
g.setColor(left_color);
g.drawString("" + chars[i], x - left_x, h - left_y);
g.setColor(right_color);
g.drawString("" + chars[i], x + right_x, h + right_y);
g.setColor(getForeground());
g.drawString("" + chars[i], x, h);
x += w;
}
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
}
}
/*调用此标签
RichLabel label = new RichLabel("2012", 0);
label.setLeftShadow(5, 5, new Color(0xeebbcc));
label.setRightShadow(-3, -3, new Color(0xbacbdc));
label.setForeground(new Color(0X8888ff));
label.setFont(label.getFont().deriveFont(200f));
*/
|