The painting engine can handle this by itself. Take a look at the XOR-Mode for Graphics:
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#setXORMode(java.awt.Color)
In your example, you have to set the XOR color to White before writing the text:
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.BLUE);
g2.fill(myEllipse);
g2.setColor(Color.red);
g2.setXORMode(Color.white); // Set XOR mode to white
g2.drawString(myText, 70, 70);
It can be quite tricky to find the correct XOR color. But with regards to this question, you have to bitwise XOR the foreground of the graphics, the XOR-Color, and the color you're painting onto.
For the circle area we get:
Foreground = FF 00 00 (red)
XOR-Color = FF FF FF (white)
Background = 00 00 FF (blue)
----------------------
Result = 00 FF 00 (green)
and outside:
Foreground = FF 00 00 (red)
XOR-Color = FF FF FF (white)
Background = FF FF FF (white)
----------------------
Result = FF 00 00 (red)
Update:
To find the XOR / Foreground pair, you can do the following:
You have to combine the background and resulting color by XOR. We see that for both cases (in ellipse and outside), we have
Ellispe: 00 00 FF ^ 00 FF 00 = 00 FF FF
Outside: FF FF FF ^ FF 00 00 = 00 FF FF
So we can choose any XOR / Foreground pair that results in 00 FF FF
by XOR. In the example, I used white and red, but black and cyan would yield the same result.