2

I know that it is possible to capture lightweight Swing component picture by calling paint(Graphics g) method on the BufferedImage.getGraphics() instance. Like here: Swing: Obtain Image of JFrame

Is it possible to grab AWT component's contents as an Image?

So far, I think of java.awt.Robot solution only:

  1. Get AWT component bounds
  2. Make screenshot with specific bounds.
Community
  • 1
  • 1
Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
  • 1
    Simple answer: try it. But seeing as the AWT widgets are actually drawn by the OS, this might be a hugely platform-dependent question. – MvG Jul 26 '12 at 17:06
  • Huh.. Good question. Please try it and report back so we can try it on other OS. I had never considered AWT when I answered the linked question. – Andrew Thompson Jul 26 '12 at 22:05
  • @MvG I tried that in a variety of ways: `GraphicsEnvironment`, `CellRendererPane`, tuned `Graphics` object via `setColor(component.getForeground())` and `g.setFont(component.getFont());` calls. Even on windows it usually returns solid black rectangle or rectangle with the component foreground color. – Ostap Andrusiv Jul 27 '12 at 08:29
  • @AndrewThompson seems like grabbing screenshots is the only feasible solution, because the direct paint messages for the native control are usually deeply hidden inside the native code. – Ostap Andrusiv Jul 27 '12 at 10:18
  • @OstapAndrusiv, I'm not surprised by your findings. Perhaps you should turn them into an answer to this question. You probably should give details on *what* components you tried to grab, as swing components inherit from `Component` as well. So not all of them are heavyweight. – MvG Jul 27 '12 at 10:41

1 Answers1

0

Seems like the only working solution is to:

  1. Find component bounds in screen coordinate system.
  2. Use java.awt.Robot class to create screen capture of the area, which corresponds to the component.
Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38