阴影边框改变颜色不起作用

时间:2016-02-12 15:09:55

标签: java graphics2d

我在互联网上找到了2个关于阴影边界的例子。 对于我想要使用的一个例子,我无法改变颜色。 这是代码,其中定义了颜色:

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),应显示红色边框。但灰色的边界也来了。 有人有什么建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以考虑使用具有预定义类的ShadowBorder类,并使您尝试实现的更容易,我不确定这是否已经是您的使用但是

https://docs.oracle.com/cd/E15523_01/apirefs.1111/e13403/oracle/javatools/ui/border/ShadowBorder.html

是API的链接。您可以将该方法用于paintBorder和setColor,以确保只显示您想要的颜色。