Alright Stackoverflow, I need your help on this one with drawing.
I am restricted to Graphics2D for this one and was wondering how I would be able to achieve an image that has two layers.
First Layer - Color.white
Second Layer - Color.red
Now what I want to achieve is the ability to draw a circle that allows myself to see the first layer while the second layer is still red. Not simply draw a circle that is white.
Here is a picture of what I mean
I came across some previous code but it did not work, so I was hoping someone had the knowledge of Graphics2D to achieve this.
Here is the code and I leave the rest to you.
BufferedImage img = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGBA);
Graphics2D g = img.createGraphics();
int ovalX = 50;
int ovalY = 70;
int ovalRadius = 20;
/* Draw the grey rectangle */
g.setColor(Color.GRAY);
g.fillRect(0, 0, sizeX, sizeY);
/* Enable Anti-Alias */
g.setRenderingHint(RenderingHints.HINT_ANTIALIAS, RenderingHints.VALUE_ANTIALIAS_ON);
/* Clear the circle away */
g.setComposite(AlphaComposite.CLEAR, 1.0f);
g.fillOval(ovalX - ovalRadius, ovalY - ovalRadius, 2 * ovalRadius, 2 * ovalRadius);
g.dispose();
You can use Area
and Graphics.setClip
Area a = new Area(yourRect);
a.subtract(new Area(yourCircle));
g.setClip(a);