views:

183

answers:

1

I wrote a program that generates a BufferedImage to be displayed on the screen and then printed. Part of the image includes grid lines that are 1 pixel wide. That is, the line is 1 pixel, with about 10 pixels between lines. Because of screen resolution, the image is displayed much bigger than that, with several pixels for each line. I'd like to draw it smaller, but when I scale the image (either by using Image.getScaledInstance or Graphics2D.scale), I lose significant amounts of detail.

I'd like to print the image as well, and am dealing with the same problem. In that case, I am using this code to set the resolution:

HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PrinterResolution pr = new PrinterResolution(250, 250, ResolutionSyntax.DPI);
set.add(pr);
job.print(set);

which works to make the image smaller without losing detail. But the problem is that the image is cut off at the same boundary as if I hadn't set the resolution. I'm also confused because I expected a larger number of DPI to make a smaller image, but it's working the other way.

I'm using java 1.6 on Windows 7 with eclipse.

A: 

It sounds like your problem is that you are making the grid lines part of the BufferedImage and it doesn't look good when scaled. Why not use drawLine() to produce the grid after your image has been drawn?

Justin
I'm not sure exactly what you're suggesting. Maybe that I should scale the image, then draw the lines? I don't want to scale the image or the lines, and my screen and printer both have plenty of DPI to display all the detail. I mentioned the lines, but there are also lots of small (10 pixels square) icons which become indistinct if I scale by 1/2. It's not making sense to me that this isn't easy (presumably it is easy, and I just don't know how to do it).
Ingrid
Printable provides a Graphics2D object: `public int print(Graphics g, PageFormat pf, int page)`. Instead of rendering your page as a huge pixelated image, render it as vector graphics calls: e.g. multiple `g.drawLine()` and `g.drawImage()` calls.
Justin
I'll try that and see how it looks. If I'm understanding correctly though, this will just make the less detailed display look better. Is there no way to display and print all of the detail?
Ingrid
You can only print the detail which is in the image to begin with. This should add infinite detail to line elements. If individual icons have more detail than the resolution of your giant BufferedImage this will fix that too.
Justin
Thank you for your patience! I was mistaken about the screen display. You're right that it's just displaying what I'm asking it to, and there's no way to make it smaller without losing detail. But am still not able to print the detail I want.I put all of the detail that I wanted into my original image. When printing, it prints huge, using multiple dots for each pixel. I can use PrinterResolution to make it draw smaller without losing detail, but the image is being cut off where the page boundary would be if I hadn't set the resolution.
Ingrid
Figured it out! I needed to use Pageable instead of Printable. That way, I can set the Paper, and setImageableArea to bigger than the paper. That way, when I use PrinterResolution, it will not cut off my image.
Ingrid