I want to fill a region using Graphics.fillRoundRect(), but I want a rectangle in the middle of it to not be filled.
Essentially, given a component of 100×30, I want to set clipping to be the rectangle at 10,10 of size 80×10 but have the fill only paint the area outside that 80×10 rectangle. The reason is that I want a border of n pixels with a curved outline painted without affecting the inside component area.
The best way I can see so far is to clip to 10,10 90×10 and do the fillRoundRect() and then clip to 90,10 10×10 and do a fillRect() to fill in the right hand side, below and above the corners.
If I simple repaint a single line rectangle then I end up with “spotting” on the corners because the curves don’t quite abut (and/or because AA affects surrounding pixels).
EDIT: Caveat – I need a way to do it which will work with J2ME AWT (CDC with Personal Profile 1.1) as well as J2SE.
Edit: Another similar question has an an answer I was able to adapt. The code that works correctly for my situation is posted as a self-answer.
I have a similar answer on the other question too, which is to use a polygon as an AWT clip. Maybe this is supported in J2ME? You’ll need to know the bounds of the rectangle you want to exclude, and the outer bounds of your drawing area.
EDIT FROM OP.
This answer worked for me and the API’s are supported on J2ME. The answer of the other question appears to have one mistake – the set of coordinates needs to start a the point on the outer-left and inner top in order to create an enclosed polygon. My final code which worked follows:
To create a clipping Shape, I used this method:
which I set into the Graphics context and then filled my rectangle:
and then I created the illusion of rounded inside corners by painting a single dot in each corner: