Graphics2Dレンダリングでパンチアウト効果を作成しようとしています。
黒の長方形があります。テキストは赤に着色されています。色を0x00FF0000に設定して、背景から「パンチアウト」できるようにしたいと思います。
Graphics2D newG = (Graphics2D)g.create();
newG.setColor(new Color(0x00,0x00,0x00,0xFF)); //255 Alpha
newG.fillRect(box.x, box.y, box.width, box.height);
newG.setColor(new Color(0xFF,0x00,0x00,0x00)); //0 Alpha
newG.drawString("Welcome", box.x ,box.y);
私が見ているのは、テキストが背景に対して完全に透明になっているが、パンチアウトしていないことです。
私が見るもの:
私が期待したもの:
Graphics2Dを使用して「パンチアウト」効果を実現するにはどうすればよいですか?
編集:パンチアウト効果は、前景が0x00FF0000に設定され、背景が0xFF000000に設定されたJLabelを使用するようなものです。バックグラウンドからテキストを「切り取り」/「パンチアウト」します。また、アルファが0の場合にのみ、常にパンチアウトしたくありません。
EDIT2:私は以下の提案されたコードを試しましたが同じ結果になりました。
Rectangle stringBox = new Rectangle(x, y - fm.getHeight() + fm.getDescent(), fm.stringWidth(splitStr[i]), fm.getHeight());
TextLayout textLO = new TextLayout(splitStr[i], newG.getFont(), newG.getFontRenderContext());
Shape sText = textLO.getOutline(newG.getTransform()); // The Shape of text
Path2D.Double shape = new Path2D.Double(stringBox); // The rectangle
appendTextShape(shape, sText, newG.getTransform(), 0, 10);
newG.setColor(Color.black);
newG.fill(shape);
newG.setColor(new Color(0xFF, 0x00,0x00,0x00));
newG.drawString(splitStr[i], x, y);