I'm trying to create a Punch Out effect on a Graphics2D render.
I have a rectangle colored black. Text Colored Red. I want to be able to set the color to 0x00FF0000 and have it "punch out" of the back ground.
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);
What I see is the text goes completely transparent against the back ground, but does not punch out.
What I see:
What I expected to see :
How can I achieve the 'punch out' effect using Graphics2D?
EDIT: Punch-Out Effect is like using a JLabel with a foreground set to 0x00FF0000, and a Back Ground 0xFF000000. it "cuts out"/"Punches Out" the text from the back ground. Also I do not want it to always punch out, only when alpha is 0.
EDIT2: Ive tried the sugested code below with identical results.
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);
http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html
Properly setting up composition was the solution. SRC_IN was the composition i was looking for.