저는 광산의 2D 엔진에 레이어를 구현하고 레이어를 스택 가능하게하고 싶습니다. 또한 레이어에 '구멍을 자르는'기능을 원합니다. 이는 알파를 사용한다는 것을 의미합니다.Graphics2D는 알파를 사용할 수 없습니까?
BufferedImage background, foreground;
public GraphicsTest() {
background = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);
foreground = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB); // Fully capable of alpha.
Random r = new Random();
int white = Color.white.getRGB();
// Draw random white dots
for (int i=0; i<500; i++) {
int
x = r.nextInt(500),
y = r.nextInt(500);
background.setRGB(x, y, white);
}
}
@Override
protected void paintComponent(Graphics g) {
BufferedImage output = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB); // Fully capable of alpha.
Graphics2D canvas = output.createGraphics();
Graphics2D overlay = foreground.createGraphics();
canvas.drawImage(background, 0, 0, null);
overlay.setColor(Color.white);
overlay.fillRect(0, 0, 500, 500);
// Start drawing with alpha
overlay.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
overlay.setColor(new Color(0xFF000000)); // Overwrite/draw full alpha, colour doesn't matter when alpha is 100%
overlay.fillRect(100, 100, 125, 87);
//canvas.setColor(Color.red);
//canvas.fillRect(0, 0, 500, 500);
canvas.drawImage(foreground, 0, 0, null);
overlay.dispose();
canvas.dispose();
g.drawImage(output, 0, 0, null);
// Also write to a file for manual raw pixel checking
try {
// Does output a 32-bit depth image
ImageIO.write(output, "PNG", new File("c:/output.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
이 가장 기본적인 테스트가 나는 할 수있다 : 쓰기 그러나 때 그것은 예를 들어 (내 코드), 함께 혼합 색상에 의해 가짜로 시도, 나는 진정한 알파를 사용하여 아무것도 얻을 수없는 것 독립형 내가 배경과 전경을 위해 두 개의 이미지를 가지고 있다고 생각하면, 127x87 (랜덤 사이즈) 상자에서 전경을 통해 배경을 볼 필요가 있습니다. 현재 검은 색 상자가있는 흰색 화면이 나오고 뒷 상자는 배경의 일부 여야합니다.
나는 여기에서 모든 종류의 방법을 시도해 보았는데, 합성 및 검정색을 완전한 알파로 설정했지만 배경을 전혀 볼 수 없을 때 setColor (0xFF000000)는 검정색과 합성색을 그리는 것처럼 보이지 않습니다. 이 FF000000 (검은 색)을 00FFFFFF (알파가없는 흰색) 배경을 FF000000 (내가 설정 한 것)을 겹쳐 쓰는 대신에이 '가짜'의 원인입니다. 대신 007F7F7F (알파가없는 회색)입니다.
내가 볼 수있는 유일한 이유는 모든 것이 Graphics2D 객체를 통과한다는 사실입니다. 느린 속도로 output.setRGB를 사용할 수 없기 때문에 사용자 정의 모양을 그리는 방법을 알지 못합니다.
알파 문제는 확실하지 않지만 그림 메서드에서 ImageIO (또는 IO)를 사용하지 마십시오. – camickr
다른 혼합 모드 – vandale
에 대한 ['AlphaComposite'] (http://docs.oracle.com/javase/7/docs/api/index.html) 클래스에서'setComposite()를 통해 합성을 설정해야합니다. @camickr 왜? 루프에 있지 않으므로 파일을 한 번만 씁니다. –