Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Printing BufferedImage witch high resolution

1,392 views
Skip to first unread message

Andre Brunner

unread,
Aug 19, 2003, 9:27:14 AM8/19/03
to
Hello,

I want to print a JPanel with a lot of JTextField's and some JLabels.

Therefor I created a BufferedImage of the JPanel and tried to print
it. The problem is that the resolution of the print is really bad
because when creating the BufferedImage the resolution of the screen
is used and not the printer-resolution!

My question: Is it possible to print the image with a resolution of
600dpi or higher?

The original image is to big for one page so I have to create
subimages via BufferedImage.getSubImage():

The source of the print-method:


public int print(Graphics gra, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
mybuf = new BufferedImage((int) getSize().getWidth(),
(int) getSize().getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D bufGraphics = mybuf.createGraphics();

paint(bufGraphics);

Graphics2D graphics = (Graphics2D) gra;
.
.
.

subImage = mybuf.getSubimage((int) ((positionForWidth) *
width), (int)
(positionForHeight * height), width, height);
.
.
.

graphics.drawImage(subImage, 0, 0, null);
return PAGE_EXISTS;
}
else
{
return NO_SUCH_PAGE;
}

I would be really great if someone is able to help!


Thank you very much

André

Harald Hein

unread,
Aug 19, 2003, 4:46:04 PM8/19/03
to
"Andre Brunner" wrote:

> Therefor I created a BufferedImage of the JPanel and tried to print
> it. The problem is that the resolution of the print is really bad
> because when creating the BufferedImage the resolution of the screen
> is used and not the printer-resolution!
>
> My question: Is it possible to print the image with a resolution of
> 600dpi or higher?

I lost a little bit track of all the printing APIs in Java (1.4 got yet
another one, so we are up to three or four different printing APIs :-
((().

If you use java.awt.print, the easiest is to disable double buffering
of the JPanel. And directly use the paint method of the JPanel in
Printable.print(). To get the desired parts of the JPanel, you should
transform the Graphics2D object, and leave the cutoff to the cliping.

Andre Brunner

unread,
Aug 20, 2003, 3:48:28 AM8/20/03
to
Harald Hein <speec...@gmx.de> wrote in message news:<Xns93DCE7B3...@194.97.5.10>...

Hello,

Thank,s

The problem is that I have to print the panel on several pages. I'm
not allowed to resize it! Therefor I have to create subimages! So any
idea how to do that with your approach?

The other thing is, that when I'm trying the paint(), or
paintAll()-Method the text within the JTextFields is really bad
printed

Cheers

André

Babu Kalakrishnan

unread,
Aug 21, 2003, 1:46:40 AM8/21/03
to
On 20 Aug 2003 00:48:28 -0700, Andre Brunner <AndreB...@gmx.de> wrote:
> Harald Hein <speec...@gmx.de> wrote in message news:<Xns93DCE7B3...@194.97.5.10>...
>>
>> If you use java.awt.print, the easiest is to disable double buffering
>> of the JPanel. And directly use the paint method of the JPanel in
>> Printable.print(). To get the desired parts of the JPanel, you should
>> transform the Graphics2D object, and leave the cutoff to the cliping.
>
> The problem is that I have to print the panel on several pages. I'm
> not allowed to resize it! Therefor I have to create subimages! So any
> idea how to do that with your approach?
>
> The other thing is, that when I'm trying the paint(), or
> paintAll()-Method the text within the JTextFields is really bad
> printed
>

What Harald was saying was not to resize the Panel. When printing the
panel onto a Graphics surface, you can set an Affine transform (that
does only a translation) on the Graphics object. For printing the panel
on several pages, you need to change only the translation parameters of
this transform for each page so that the appropriate portion of the
panel gets printed to the Graphics object. (You can also set a clip on
the Graphics object to make sure that the actual painting takes place
only on the printable portion of the paper - but I think it is already
setup that way by default).

By the way, while printing it is better to call the print() method of
the component instead of the paint() method. The default "print()"
method implementation in JComponent performs some magic that turns off
the Swing double-buffering, and then delegates the actual task to the
paint method.

BK

Andre Brunner

unread,
Aug 21, 2003, 4:43:45 AM8/21/03
to
Babu Kalakrishnan <ka...@sankya.com> wrote in message news:<slrnbk8n6...@ganga.sankya.com>...

Thank's for,

do you have a few lines of code to see what to do?

Would be really nice!!

agip...@gmail.com

unread,
Apr 10, 2014, 8:03:29 AM4/10/14
to

Knute Johnson

unread,
Apr 12, 2014, 7:54:12 PM4/12/14
to
There are several issues involved, only some of which I understand
completely. When printing, the coordinate space is based upon 72nds of
an inch rather than the printer's 600ths of an inch. So if you wish to
print a large image for example, you must scale it to fit the printing
area (8.5 * 72 = 612 pixels by 11 * 72 = 720 pixels). The really
interesting thing that I don't thoroughly understand is that if you
print something by scaling it you can get much better resolution than
612x720. The test7 example below will demonstrate this clearly. It
prints 6 one inch squares made up of vertical lines. The first with no
spaces between lines and the last with 5 spaces between. Depending on
the quality of your printer and paper you should see individual lines by
the 4th square with the naked eye. With a magnifying glass however, I
can see individual lines in the second square. Clearly the resolution
is better than a 72nd of an inch.

The test6 example below demonstrates a way to print a JFrame and its
contents using the Component.printAll method. The tricky code is
checking for aspect ratio of the image and paper and then scaling
appropriately.

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

public class test7 implements Printable {
public test7() throws PrinterException {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
job.print();
}

public int print(Graphics g2D, PageFormat pf, int index) {
Graphics2D g = (Graphics2D)g2D;
if (index == 0) {
// start drawing at first printable pixel
g.translate(pf.getImageableX(),pf.getImageableY());
// convert printer scale to 72nds of an inch
g.scale(72.0/600.0,72.0/600.0);

int n = 1;
for (int i=0; i<6; i++) {
for (int j=0; j<600; j+=n)
g.drawLine(i*600+j,0,i*600+j,600);
n += 1;
}

return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}

public static void main(String[] args) throws Exception {
new test7();
}
}


import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;

public class test6 extends JFrame implements Printable {
public test6() {
super("test6");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2,2,2,2);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridy = 0;

for (int i=0; i<16; i++) {
add(new JTextField("textfield#" + i),c);
add(new JTextField("another textfield#" + i),c);
add(new JLabel("label#" + i),c);
add(new JLabel("another label#" + i),c);
++c.gridy;
}

c.gridwidth = 4;
c.fill = GridBagConstraints.NONE;
JButton b = new JButton("print");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(test6.this);
try {
if (job.printDialog())
job.print();
} catch (PrinterException pe) {
JOptionPane.showMessageDialog(test6.this,pe);
}
}
});
add(b,c);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}

public int print(Graphics g2D, PageFormat pf, int index) {
Graphics2D g = (Graphics2D)g2D;
if (index == 0) {
// move the drawing onto the printable area of the page
g.translate(pf.getImageableX(),pf.getImageableY());
// scale the drawing to fit into the printable area
double paperAspectRatio =
pf.getImageableWidth() / pf.getImageableHeight();
double compAspectRatio = (double)getWidth() / getHeight();
if (compAspectRatio > paperAspectRatio)
g.scale(pf.getImageableWidth()/getWidth(),
pf.getImageableWidth()/getWidth());
else
g.scale(pf.getImageableHeight()/getHeight(),
pf.getImageableHeight()/getHeight());

test6.this.printAll(g);
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new test6();
}
});
}
}


--

Knute Johnson
0 new messages