Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Save Graphics2D object to file?

Posted on 2002-03-28
Last Modified: 2007-12-19
I have a Graphics2D object, and I know nothing about it beyond what information I can gather via the methods available to Graphics2D. How do I save this to file? I have been playing around with JIMI, but I have no idea how to convert it to the necessary ImageProducer class... If that's even possible. Any compressed image type is acceptable, jpeg, png, etc, I don't care. I just need this Graphics2D object dumped to file.

I need an answer ASAP - hence the high points.

Question by:dleslie
  • 4
  • 3
LVL 18

Expert Comment

ID: 6903282
try something like this:

// Create an image buffer in
//which to paint on.
BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// tx is your AffineTransform, apply it to image
Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(<your original image>, tx, null);

// JPEG-encode the image
//and write to file.
OutputStream os = new FileOutputStream(<name of new file>);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);

Author Comment

ID: 6903288
I can't do that since all I have is a Graphics2D object provided by an external class I have no access to, unless I could somehow convert this Graphics2D object to a buffered image, this method is impossible.

LVL 92

Expert Comment

ID: 6904066
What you are requesting does not make sense.
The Graphics2D class is merely a context for performing drawing operations. It does not contain any details about the drawing operations performed by it.
Sounds like instead what you need to do is pass your own Graphics2D object (created as bobbit31 described above) to your external class to paint to.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 6904649
An external class has already painted to the Graphics2D object, and has passed it along to me to dump it to file. As is painfully obvious, I'm new to this segment of Java, so please bear with me.

LVL 92

Accepted Solution

objects earned 200 total points
ID: 6904658
There is no way you can find out what's been painted to the Graphics2D object passed to you.
LVL 92

Expert Comment

ID: 6904663
To do what you want, you need to do the reverse and pass a Graphics object to be painted to.

Author Comment

ID: 6905363
I thought so...

Thanks for the help,
LVL 92

Expert Comment

ID: 6905842
No worries.

Brainbench MVP for Java 1

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 52
sql import cannot be resolved jsp 3 45
eclipse console opening separately 2 29
Html Table Looping (part 2) 5 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question