簡體   English   中英

陰影邊框更改顏色不起作用java

[英]shadowed border change color doesn't work 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();
    } 

我選擇了:new Color(1.0f,0.0f,0.0f,0.5f),應該顯示紅色邊框。 但是灰色邊框也來了。 有人建議嗎? 提前致謝。

您可以考慮使用具有預定義類的ShadowBorder類,這將使您實現MUCH的嘗試變得更加容易,我不確定這是否已經在您的使用中了,但是

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

是指向API的鏈接。 您可以將方法同時用於paintBorder和setColor,以確保僅顯示所需的顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM