ivan_ivanovich_ivanoff
ivan_ivanovich_ivanoff

Reputation: 19453

Swing: How to read graphic information underneath a component?

How could I "read" graphic information underneath a component (let's say as BufferedImage)?

I want to make that component half-translucent (already done) and apply graphic effects on underlying stuff, such as blur all elements under that component (but not the component itself).

My approach is probable wrong:
I try to get graphic information from Graphics2D instance given to me in the paint(...) method, but it's empty, right?

Upvotes: 1

Views: 188

Answers (1)

Michael Myers
Michael Myers

Reputation: 191895

Question: is your component top-level (i.e., do you want to know what's on the desktop under a JFrame), or do you just want to know about components that are layered underneath the component?

If 1), then it's a simple matter to grab a screenshot with the java.awt.Robot method createScreenCapture(Rectangle); the Rectangle should be your window bounds in this case.

If 2), then if you have a reference to the component underneath, you can make use of the fact that paint(Graphics) doesn't care where the Graphics object came from. You can create a BufferedImage, call createGraphics() (in case a Graphics2D is expected), and pass the result to the paint(Graphics) method of the component that you want to capture.
Note that if the component is a container, it will paint its children also; this may or may not be what you want.

Upvotes: 1

Related Questions