Tricking Graphics2d into painting an image at a higher resolution

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
I have a JPanel that I am writing to a file by just directly calling the JPanel.paint() on the Graphics2D object of a bufferedimage (see below).

I would like to save the image at a higher resolution than the JPanel size. Is it possible to fool the Graphics2D object into scaling every draw action up? For example, the size of the JPanel is 800x600, I would like to have it write as a 3200x2400 pixel image. The paintComponent() that I am using is actually pretty complicated, so writing a special method to draw all my lines and text thicker and bigger would be annoying, and it would make modifying any code in the future a hassle.

The reason I want to do this, is that on screen the JPanel looks fine, however for print purposes the resolution needs to be much higher.

Any helpful suggestions?
Thanks.

writing JPanel graphics to file:

BufferedImage image = new BufferedImage(d.width,d.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
graph.paint(g2d);
ImageIO.write(image, "png", file);
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
You might be able to override the Graphics2D AffineTransform, but I haven't tried it. I imagine if you grab the graphics' tranform and apply a scale to it, you might get what you want.
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
Hey Mundane thanks for the suggestion. The AffineTransform works in scaling in the image up. The main problem I have now though, is the scale up of text. The fonts themselves are not increased and are still at low resolution. So they're big, but still blocky.

You wouldn't happen to know how I could go about extending Graphics2D? I was thinking I could override the drawString() method and insert something that would increase font size. That way I could just use this special Graphics2D whenever I wanted to scale up the image, and avoid changing any of the actual paintComponent() code.

The Graphics2D is an abstract class, and java uses the final class sun.java2d.SunGraphics2D but I can't find any documentation on that.

edit: I just found the source and javadoc at java2s.com, so I guess I'll look into that...