2

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);

私が見ているのは、テキストが背景に対して完全に透明になっているが、パンチアウトしていないことです。

私が見るもの:http://i.imgur.com/HYTe1.jpg

私が期待したもの:http://i.imgur.com/YQwZk.jpg

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);
4

3 に答える 3

1

あなたが次のようなことを意味する場合:

ここに画像の説明を入力してください

Shapeテキストのアウトラインを取得してから、レンダリングするPath2Dための結合を作成するために使用する必要があります。Shape

Graphics2D g2d = ...;
TextLayout text = new TextLayout("Welcome", g2d.getFont(), g2d.getFontRenderContext());
Shape sText = text.getOutline(g2d.getTransform());      // The Shape of text
Path2D shape = new Path2D.Double(new Rectangle(200, 100));      // The rectangle
appendTextShape(shape, sText, g2d.getTransform(), 0, 10);   // combine the shape
g2d.setColor(Color.BLACK);
g2d.fill(rect);

そしてappendTextShape方法はここにあります:

public void appendTextShape (Path2D p, Shape s, AffineTransform t, int x, int y) {
    synchronized (s) {
        PathIterator pit = s.getPathIterator(t);
        float[] coords = new float[6];
        int c = 0;
        while (true) {
            pit.next();
            if (pit.isDone()) {
                break;
            }
            switch (pit.currentSegment(coords)) {
                case PathIterator.SEG_MOVETO:
                    p.moveTo(x + coords[0], y + coords[1]);
                    break;
                case PathIterator.SEG_LINETO:
                    if (c == 0) {
                        p.moveTo(x + coords[0], y + coords[1]);
                    } else {
                        p.lineTo(x + coords[0], y + coords[1]);
                    }
                    break;
                case PathIterator.SEG_QUADTO:
                    if (c == 0) {
                        p.moveTo(x + coords[0], y + coords[1]);
                    } else {
                        p.quadTo(x + coords[0], y + coords[1], x + coords[2], y + coords[3]);
                    }
                    break;
                case PathIterator.SEG_CUBICTO:
                    if (c == 0) {
                        p.moveTo(x + coords[0], y + coords[1]);
                    } else {
                        p.curveTo(x + coords[0], y + coords[1], x + coords[2], y + coords[3], x + coords[4], y + coords[5]);
                    }
                    break;
                case PathIterator.SEG_CLOSE:
                    p.closePath();
                    break;
            }
            c++;
        }
    }
}
于 2013-01-17T18:51:28.600 に答える
1

http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html

構成を適切に設定することが解決策でした。SRC_INは私が探していた構成でした。

于 2013-01-17T22:29:26.797 に答える
0

あなたのappendTextShape方法は機能しています。

Path2Dただし、次のように、のconsrtuctorにすでに実装されています。

Path2D path = new Path2D.Double(shape, affineTransform);
于 2013-09-23T07:34:27.773 に答える