Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing Graphics contents to a file

 
C Krit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to dump the contents of a Graphics component (or part of it) to an image file?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For any arbitrary graphics object? No. But if you create a BufferedImage, ask it for its Graphics object, then paint on that Graphics object, the drawing shows up in the BufferedImage, so you can use that class's API to get the screen bits out.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can use the javax.imageio.ImageIO class to write a BufferedImage to a PNG or JPEG: http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html
 
alex schulze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right, as soon as you are able to sublclass that - lets say - JComponent such thing is possible.

so override it and have a local bufferedImage instance. use getGraphics on that instance and let the desired JComponent paint itself on the returned graphics object via super(returned-Graphics-instance).
finally save everything with something like

ImageIO.write(local_bufferedImage, "png", file);
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't quite understand the part about subclassing. You can call paintComponent() (or paint()) directly on the Component yourself, without any cooperation.
 
alex schulze
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:I don't quite understand the part about subclassing. You can call paintComponent() (or paint()) directly on the Component yourself, without any cooperation.
true, my bad!
i was (wrongly) assuming that the graphical object should also get graphically changed on screen so that we would need to process the graphics object that is passed to paint() or paintComponent().
but as ernest said, to only get the graphics in order to dump them, no subclassing or cooperation is needed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!