阴影边框不改变颜色';t工作java

阴影边框不改变颜色';t工作java,java,graphics2d,Java,Graphics2d,我在网上找到了两个关于阴影边界的例子。 举个例子,我想用它,我不能改变颜色。 以下是定义颜色的代码: 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

我在网上找到了两个关于阴影边界的例子。 举个例子,我想用它,我不能改变颜色。 以下是定义颜色的代码:

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...
 }
private Map getImages(Graphics2D g2){
//首先,检查此大小的图像是否已渲染
//如果是,请使用缓存。否则,请绘制并保存
Map images=CACHE.get(阴影大小);
如果(图像==null){
images=newhashmap();
int rectWidth=拐角尺寸+1;
int imageWidth=rectWidth+shadowSize*2;
BuffereImage=新的BuffereImage(
imageWidth、imageWidth、BuffereImage.TYPE_INT_ARGB);
Graphics2D缓冲区=(Graphics2D)image.getGraphics();
设置颜色(新颜色(1.0f,0.0f,0.0f,0.5f));
转换(阴影大小,阴影大小);
RoundRectangle2D rect=新的RoundRectangle2D.Double(
0,0,rectWidth,rectWidth,cornerSize,cornerSize);
缓冲区填充(rect);
buffer.dispose();
浮动模糊=1.0f/(浮动)(阴影大小*阴影大小);
float[]blurKernel=新的float[shadowSize*shadowSize];

对于(int i=0;i),您可以考虑使用具有预定义类的阴影边框类,并使您试图实现的更容易,但我不确定这是否已经是您使用的但


是指向API的链接。您可以使用paintBorder和setColor的方法来确保只显示您想要的颜色。

您是否可以发布一张图像,以便我更好地了解这两种颜色显示的含义?对此很好奇。我是否必须下载完整的Oracle JDeveloper,或者下载通用版本Java Edition和jar文件的名称是什么?您需要下载Developer。这会有点痛苦,但非常有用,因为Oracle有一些独特的工具可供您使用。我相信您必须在这之后添加整个Middlewear扩展,这是我以前没有做过的,但我确信有diroracle站点上的分区。ShadowBorder有一个方法setColor,但它忽略了该颜色并显示灰色边框。
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();
    }