我在互聯網中找到了2個關於陰影邊框的示例。 對於我想使用的一個示例,我無法更改顏色。 這裏是代碼,其中的顏色定義:陰影邊框更改顏色不起作用java
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>();
int rectWidth = cornerSize + 1;
int imageWidth = rectWidth + shadowSize * 2;
BufferedImage image = new BufferedImage(
imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D buffer = (Graphics2D)image.getGraphics();
buffer.setColor(new Color(1.0f, 0.0f, 0.0f, 0.5f));
buffer.translate(shadowSize, shadowSize);
RoundRectangle2D rect = new RoundRectangle2D.Double(
0, 0, rectWidth, rectWidth, cornerSize, cornerSize);
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 = new BufferedImage(
imageWidth, imageWidth, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) targetImage.getGraphics();
g2d.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));
// and so on...
}
編輯:
public void paintBorder(Component c, Graphics graphics, int x, int y, int width, int height) {
Map<Position,BufferedImage> images = getImages(null);
Graphics2D g2 = (Graphics2D)graphics.create();
g2.setColor(Color.red);
Point topLeftShadowPoint = null;
if (showLeftShadow || showTopShadow) {
topLeftShadowPoint = new Point();
if (showLeftShadow && !showTopShadow) {
topLeftShadowPoint.setLocation(x + width - shadowSize, y + height - shadowSize);
} else if (showLeftShadow && showTopShadow) {
topLeftShadowPoint.setLocation(x + width - shadowSize, y + height - shadowSize);
} else if (!showLeftShadow && showTopShadow) {
topLeftShadowPoint.setLocation(x + width - shadowSize, y + height - shadowSize);
}
}
Point bottomRightShadowPoint = null;
if (showRightShadow || showBottomShadow) {
bottomRightShadowPoint = new Point();
if (showRightShadow && !showBottomShadow) {
bottomRightShadowPoint.setLocation(x, y);
} else if (showRightShadow && showBottomShadow) {
bottomRightShadowPoint.setLocation(x, y);
} else if (!showRightShadow && showBottomShadow) {
bottomRightShadowPoint.setLocation(x , y);
}
}
Point bottomLeftShadowPoint = null;
if (showLeftShadow || showBottomShadow) {
bottomLeftShadowPoint = new Point();
if (showLeftShadow && !showBottomShadow) {
bottomLeftShadowPoint.setLocation(x + width - shadowSize, y);
} else if (showLeftShadow && showBottomShadow) {
bottomLeftShadowPoint.setLocation(x + width - shadowSize, y);
} else if (!showLeftShadow && showBottomShadow) {
bottomLeftShadowPoint.setLocation(x + width - shadowSize, y);
}
}
Point topRightShadowPoint = null;
if (showRightShadow || showTopShadow) {
topRightShadowPoint = new Point();
if (showRightShadow && !showTopShadow) {
topRightShadowPoint.setLocation(x, y + height - shadowSize);
} else if (showRightShadow && showTopShadow) {
topRightShadowPoint.setLocation(x, y + height - shadowSize);
} else if (!showRightShadow && showTopShadow) {
topRightShadowPoint.setLocation(x, y + height - shadowSize);
}
}
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
if (showRightShadow) {
Rectangle leftShadowRect =
new Rectangle(x + width - shadowSize,
topRightShadowPoint.y + shadowSize,
shadowSize,
bottomRightShadowPoint.y - topRightShadowPoint.y - shadowSize);
g2.drawImage(images.get(Position.LEFT),
leftShadowRect.x, leftShadowRect.y,
leftShadowRect.width, leftShadowRect.height, null);
}
if (showLeftShadow) {
Rectangle rightShadowRect =
new Rectangle(x,
topLeftShadowPoint.y + shadowSize,
shadowSize,
bottomLeftShadowPoint.y - topLeftShadowPoint.y - shadowSize);
g2.drawImage(images.get(Position.RIGHT),
rightShadowRect.x, rightShadowRect.y,
rightShadowRect.width, rightShadowRect.height, null);
}
if (showTopShadow) {
Rectangle bottomShadowRect =
new Rectangle(topLeftShadowPoint.x + shadowSize,
y,
topRightShadowPoint.x - topLeftShadowPoint.x - shadowSize,
shadowSize);
g2.drawImage(images.get(Position.BOTTOM),
bottomShadowRect.x, bottomShadowRect.y,
bottomShadowRect.width, bottomShadowRect.height, null);
}
if (showBottomShadow) {
Rectangle topShadowRect =
new Rectangle(bottomLeftShadowPoint.x + shadowSize,
y + height - shadowSize,
bottomRightShadowPoint.x - bottomLeftShadowPoint.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();
}
我選用新顏色(1.0F,0.0F,0.0F,0.5F),它應顯示一個紅色邊框。但是灰色的邊界也來了。 有人有建議嗎?提前致謝。
任何機會,你可以張貼圖像,所以我可以更好地瞭解你的意思是兩種顏色顯示?對此感到好奇。 – jesric1029