Swing: How to read graphical information under a component?
How could I "read" the graphical information underneath a component (say as a BufferedImage )?
I want to make this component semi-transparent (already done) and apply graphical effects to the base material, such as blurring all elements under this component ( but not the component itself ).
My approach is probably wrong:
I'm trying to get the graphics information from the Graphics2D instance provided to me in the paint (...) method , but it's empty, isn't it?
a source to share
The question is: is your top-level component (i.e. you want to know what's on the desktop in the JFrame), or do you just want to know about the components that lie under the component?
If 1), then just take a screenshot with the java.awt.Robot
method createScreenCapture(Rectangle)
; Rectangle
should be your window constraint in this case.
If 2), then if you have a component reference at the bottom, you can use the fact that paint(Graphics)
doesn Don't worry where the object came from Graphics
. You can create BufferedImage
, call createGraphics()
(if awaited Graphics2D
) and pass the result to the paint(Graphics)
method of the component you want to capture.
Note that if the component is a container, it will also paint its children; it may or may not be what you want.
a source to share