2013-01-17 29 views
2

我試圖在Graphics2D渲染上創建Punch Out效果。使用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我實現了 '衝出' 的效果?

編輯:Punch-Out效果就像使用JLabel的前景設置爲0x00FF0000,並且返回地面0xFF000000。它從背後「切出」/「衝出」文字。我也不希望它總是打出來,只有當alpha是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); 

回答

1

如果你的意思是這樣的:

enter image description here

你需要獲得大綱文本的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++; 
     } 
    } 
} 
+0

你如何使用Path2D這樣呢?它的抽象爲Java 7u11 – meriley

+0

看起來像你需要Path2D.double(形狀) – meriley

+0

而且它看起來即使這個實現我得到相同的確切結果。 – meriley

0

您的appendTextShape方法正在工作。

但是,它已經在Path2D的consrtuctor實現,像這樣:

Path2D path = new Path2D.Double(shape, affineTransform);