📄 dropshadowborder.java
字号:
bottomRightShadowPoint.y - topRightShadowPoint.y - shadowSize);
g2.drawImage(images.get(Position.RIGHT),
rightShadowRect.x, rightShadowRect.y,
rightShadowRect.width, rightShadowRect.height, null);
}
if (showTopShadow) {
Rectangle topShadowRect =
new Rectangle(topLeftShadowPoint.x + shadowSize,
y,
topRightShadowPoint.x - topLeftShadowPoint.x - shadowSize,
shadowSize);
g2.drawImage(images.get(Position.TOP),
topShadowRect.x, topShadowRect.y,
topShadowRect.width, topShadowRect.height, null);
}
if (showLeftShadow || showTopShadow) {
g2.drawImage(images.get(Position.TOP_LEFT),
topLeftShadowPoint.x, topLeftShadowPoint.y, null);
}
if (showLeftShadow || showBottomShadow) {
g2.drawImage(images.get(Position.BOTTOM_LEFT),
bottomLeftShadowPoint.x, bottomLeftShadowPoint.y, null);
}
if (showRightShadow || showBottomShadow) {
g2.drawImage(images.get(Position.BOTTOM_RIGHT),
bottomRightShadowPoint.x, bottomRightShadowPoint.y, null);
}
if (showRightShadow || showTopShadow) {
g2.drawImage(images.get(Position.TOP_RIGHT),
topRightShadowPoint.x, topRightShadowPoint.y, null);
}
g2.dispose();
}
private Map<Position,BufferedImage> getImages(Graphics2D g2) {
//first, check to see if an image for this size has already been rendered
//if so, use the cache. Else, draw and save
Map<Position,BufferedImage> images = CACHE.get(shadowSize);
if (images == null) {
images = new HashMap<Position,BufferedImage>();
/*
* Do draw a drop shadow, I have to:
* 1) Create a rounded rectangle
* 2) Create a BufferedImage to draw the rounded rect in
* 3) Translate the graphics for the image, so that the rectangle
* is centered in the drawn space. The border around the rectangle
* needs to be shadowWidth wide, so that there is space for the
* shadow to be drawn.
* 4) Draw the rounded rect as black, with an opacity of 50%
* 5) Create the BLUR_KERNEL
* 6) Blur the image
* 7) copy off the corners, sides, etc into images to be used for
* drawing the Border
*/
int rectWidth = cornerSize + 1;
RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, rectWidth, rectWidth, cornerSize, cornerSize);
int imageWidth = rectWidth + shadowSize * 2;
BufferedImage image = GraphicsUtilities.createCompatibleTranslucentImage(imageWidth, imageWidth);
Graphics2D buffer = (Graphics2D)image.getGraphics();
buffer.setColor(new Color(0.0f, 0.0f, 0.0f, shadowOpacity));
buffer.translate(shadowSize, shadowSize);
buffer.fill(rect);
buffer.dispose();
float blurry = 1.0f / (float)(shadowSize * shadowSize);
float[] blurKernel = new float[shadowSize * shadowSize];
for (int i=0; i<blurKernel.length; i++) {
blurKernel[i] = blurry;
}
ConvolveOp blur = new ConvolveOp(new Kernel(shadowSize, shadowSize, blurKernel));
BufferedImage targetImage = GraphicsUtilities.createCompatibleTranslucentImage(imageWidth, imageWidth);
((Graphics2D)targetImage.getGraphics()).drawImage(image, blur, -(shadowSize/2), -(shadowSize/2));
int x = 1;
int y = 1;
int w = shadowSize;
int h = shadowSize;
images.put(Position.TOP_LEFT, getSubImage(targetImage, x, y, w, h));
x = 1;
y = h;
w = shadowSize;
h = 1;
images.put(Position.LEFT, getSubImage(targetImage, x, y, w, h));
x = 1;
y = rectWidth;
w = shadowSize;
h = shadowSize;
images.put(Position.BOTTOM_LEFT, getSubImage(targetImage, x, y, w, h));
x = cornerSize + 1;
y = rectWidth;
w = 1;
h = shadowSize;
images.put(Position.BOTTOM, getSubImage(targetImage, x, y, w, h));
x = rectWidth;
y = x;
w = shadowSize;
h = shadowSize;
images.put(Position.BOTTOM_RIGHT, getSubImage(targetImage, x, y, w, h));
x = rectWidth;
y = cornerSize + 1;
w = shadowSize;
h = 1;
images.put(Position.RIGHT, getSubImage(targetImage, x, y, w, h));
x = rectWidth;
y = 1;
w = shadowSize;
h = shadowSize;
images.put(Position.TOP_RIGHT, getSubImage(targetImage, x, y, w, h));
x = shadowSize;
y = 1;
w = 1;
h = shadowSize;
images.put(Position.TOP, getSubImage(targetImage, x, y, w, h));
image.flush();
CACHE.put(shadowSize, images);
}
return images;
}
/**
* Returns a new BufferedImage that represents a subregion of the given
* BufferedImage. (Note that this method does not use
* BufferedImage.getSubimage(), which will defeat image acceleration
* strategies on later JDKs.)
*/
private BufferedImage getSubImage(BufferedImage img,
int x, int y, int w, int h) {
BufferedImage ret = GraphicsUtilities.createCompatibleTranslucentImage(w, h);
Graphics2D g2 = ret.createGraphics();
g2.drawImage(img,
0, 0, w, h,
x, y, x+w, y+h,
null);
g2.dispose();
return ret;
}
/**
* @inheritDoc
*/
public Insets getBorderInsets(Component c) {
int top = showTopShadow ? lineWidth + shadowSize : lineWidth;
int left = showLeftShadow ? lineWidth + shadowSize : lineWidth;
int bottom = showBottomShadow ? lineWidth + shadowSize : lineWidth;
int right = showRightShadow ? lineWidth + shadowSize : lineWidth;
return new Insets(top, left, bottom, right);
}
/**
* @inheritDoc
*/
public boolean isBorderOpaque() {
return false;
}
public boolean isShowTopShadow() {
return showTopShadow;
}
public boolean isShowLeftShadow() {
return showLeftShadow;
}
public boolean isShowRightShadow() {
return showRightShadow;
}
public boolean isShowBottomShadow() {
return showBottomShadow;
}
public int getLineWidth() {
return lineWidth;
}
public Color getLineColor() {
return lineColor;
}
public int getShadowSize() {
return shadowSize;
}
public float getShadowOpacity() {
return shadowOpacity;
}
public int getCornerSize() {
return cornerSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -