2016-02-12 90 views
2

我在互聯網中找到了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),它應顯示一個紅色邊框。但是灰色的邊界也來了。 有人有建議嗎?提前致謝。

+0

任何機會,你可以張貼圖像,所以我可以更好地瞭解你的意思是兩種顏色顯示?對此感到好奇。 – jesric1029

回答

0

你可以考慮,而不是使用已預定義的類並會使您的努力,從而實現更容易什麼ShadowBorder類,我不知道這是否已經在你的使用的是什麼,但

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

是到API的鏈接。你可以使用paintBorder和setColor的方法來確保只顯示你想要的顏色。

+0

我是否必須下載完整的Oracle JDeveloper,或者是否足以下載通用版本Java版本,以及如何調用jar文件? – Alexandra

+0

您需要下載Developer。這將會有點痛苦,但Oracle有一些獨特的工具可供您隨時使用,這非常有用。我相信你必須在之前沒有做過的之前添加整個Middlewear Extension,但我確信oracle網站上有指示。 – jesric1029

+0

ShadowBorder具有setColor方法,但它忽略顏色並顯示灰色邊框。 – Alexandra