📄 shadowborder.java
字号:
package com.ht.dsa;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.geom.GeneralPath;
/**
* Created by IntelliJ IDEA.
*
* @author Dong Shufeng
* Date: 2008-11-5
*/
public class ShadowBorder implements Border {
int borderWidth = 8;
Color inter = Color.LIGHT_GRAY;
public ShadowBorder(int borderWidth) {
this.borderWidth = borderWidth;
}
public ShadowBorder(int borderWidth, Color inter) {
this.borderWidth = borderWidth;
this.inter = inter;
}
public ShadowBorder(Color inter) {
this.inter = inter;
}
public ShadowBorder() {
}
private static Color getMixedColor(Color c1, float pct1, Color c2, float pct2) {
float[] clr1 = c1.getComponents(null);
float[] clr2 = c2.getComponents(null);
for (int i = 0; i < clr1.length; i++) {
clr1[i] = (clr1[i] * pct1) + (clr2[i] * pct2);
}
return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
}
private Shape createClipShape(float x1, float y1, float width, float height) {
float x2 = x1 + width;
float y2 = y1 + height;
float adj = 3.0f; //帮助圆化类锐的拐角
float arc = 8.0f;
GeneralPath gp = new GeneralPath();
gp.moveTo(x1 - adj, y1 + adj);
gp.quadTo(x1, y1, x1 + adj, y1);
gp.lineTo(x2 - arc, y1);
gp.quadTo(x2, y1, x2, y1 + arc);
gp.lineTo(x2, y2 - arc);
gp.quadTo(x2, y2, x2 - arc, y2);
gp.lineTo(x1 + adj, y2);
gp.quadTo(x1, y2, x1, y2 - adj);
gp.closePath();
return gp;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x1,y1;
float w,h;
Color outer = c.getBackground();
//Color outer = Color.WHITE;
for (int i = borderWidth; i >= 2; i -= 2) {
x1 = x + (borderWidth - i);
y1 = y + (borderWidth - i);
w = (float)width - 2.0f * ((float)(borderWidth) - (float)i);
h = (float)height - 2.0f * ((float)(borderWidth) - (float)i);
float pct = (float) (borderWidth - i) / (borderWidth - 1);
g2.setColor(getMixedColor(inter, pct, outer, 1.0f - pct));
g2.setStroke(new BasicStroke(i));
g2.draw(createClipShape(x1, y1, w, h));
}
}
public Insets getBorderInsets(Component c) {
return new Insets(borderWidth, borderWidth, borderWidth, borderWidth);
}
public boolean isBorderOpaque() {
return false;
}
public int getBorderWidth() {
return borderWidth;
}
public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
}
public Color getInter() {
return inter;
}
public void setInter(Color inter) {
this.inter = inter;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -