2012-04-07 6 views
3

저는 알파 (그라디언트)가있는 PNG를 생성하고 자바에서 프로그래밍 방식으로 그릴 계획 인 프로젝트를 시작하려고합니다.자바에서 알파 이미지 만들기

예를 들어, 상자를 그리고 드롭 섀도우를 추가 한 다음이를 다른 그래픽에 오버레이 될 수있는 PNG 파일로 저장하고 싶습니다.

  1. 표준 JRE 시스템 라이브러리에서 가능합니까?
  2. 이러한 라이브러리는 어떤 라이브러리를 사용하면 간단할까요?

감사합니다.

+1

[이미지 작업] (http://docs.oracle.com/javase/tutorial/2d/images/index.html) – Jeffrey

+0

Java의 알파 마스크 관련 질문은 다음과 같습니다. http://stackoverflow.com/questions/ 221830/set-bufferedimage-alpha-mask-in-java – Patrick

+0

감사합니다. Jeffry 제가 거기에 도착하고 있습니다 ... – pstanton

답변

5

표준 JRE 시스템 라이브러리에서 가능합니까?

예, 가능하고 매우 간단합니다.

screenshot

public static void main(String[] args) throws IOException { 

    int x = 50, y = 50, w = 300, h = 200; 

    // draw the "shadow" 
    BufferedImage img = new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB); 
    Graphics g = img.getGraphics(); 
    g.setColor(Color.BLACK); 
    g.fillRect(x + 10, y + 10, w, h); 

    // blur the shadow 
    BufferedImageOp op = getBlurredOp(); 
    img = op.filter(img, null); 

    // draw the box 
    g = img.getGraphics(); 
    g.setColor(Color.RED); 
    g.fillRect(x, y, w, h); 

    // write it to disk 
    ImageIO.write(img, "png", new File("test.png")); 
} 

private static BufferedImageOp getBlurredOp() { 

    float[] matrix = new float[400]; 
    for (int i = 0; i < 400; i++) 
     matrix[i] = 1.0f/400.0f; 

    return new ConvolveOp(new Kernel(20, 20, matrix), 
          ConvolveOp.EDGE_NO_OP, null); 
} 

조작 간단한의이 종류를 만들 것 도서관 : 아래의 코드는 이미지 (투명 PNG)를 생산?

다른 사용 사례에 따라 다릅니다. 상자와 타원과 같은 단순한 도형에 대해서는 위의 솔루션으로 갈 것입니다. 라이브러리가 필요하지 않습니다.

+0

위대한 대답, 나는이 블로그 http://www.jhlabs.com/ip/blurring.html과 도서관 (조금 둘러보기)을 매우 흐리게/dropshadow에 유용 발견. – pstanton