Home » Java » java – Can I "save" Graphics2D drawn image that will not be affected with consequent usage of DstOut composite?-Exceptionshub

java – Can I "save" Graphics2D drawn image that will not be affected with consequent usage of DstOut composite?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am making my own UI design based on swing JFrame.
The button element I made is using DstOut AlphaComposite to “engrave” text on white fill while mouse is holding button. It fills button area with white and then paints button text with DstOut to make the background visible through area where text is engraved. To make this, I create an instance of BufferedImage where I paint the button and then engrave the text. After the image is done, I paint it in the place where the button should be.

Normal and holding appearances of button
The question I have is, can I do this manipulation without creating a separate BufferedImage object?

The aforementioned method has a disadvantage for me — all Graphics settings are reset. Anti-aliasing is a simple thing to set again, but I am also planning to use scale and translate to animate scene transitions.

If I use g.scale(4, 4) before drawing the entire window, everything becomes 4 times bigger, but that BufferedImage still treats 1 pixel as 1 pixel, not knowing that it’s going to be upscaled. The upscaling is done when BufferedImage is drawn on the screen, but not when BufferedImage’s contents are drawn. This makes the upscaled picture look bad as a result of upscaling low-res picture (each pixel now takes area of 16 pixels).

Upscale example As you can see on the screenshot, the first button with usual appearance looks good, but the second one which uses a BufferedImage to draw its border (in extended animated state) looks terrible.

A perfect solution for me would be some way to prevent AlphaComposite.DstOut touching anything drawn before starting drawing of the button but to do it using the same Graphics2D instance to keep settings which include scale and translation.

If it’s impossible, can you please recommend a way of getting the same result but better than just adding translation and scale to paint method arguments and tediously processing it?

How to&Answers: