Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #5423

Re: Printing BufferedImage witch high resolution

From Knute Johnson <eternal@knutejohnson.com>
Newsgroups comp.lang.java.gui
Subject Re: Printing BufferedImage witch high resolution
Date 2014-04-12 16:54 -0700
Organization A noiseless patient Spider
Message-ID <licjmv$m32$1@dont-email.me> (permalink)
References <b02c5255.0308190527.6abf1998@posting.google.com> <e95a79fe-820e-498c-ac1a-a7edf5f3a2b5@googlegroups.com>

Show all headers | View raw


On 4/10/2014 05:03, agipstech@gmail.com wrote:
> On Tuesday, August 19, 2003 6:57:17 PM UTC+5:30, Andre Brunner wrote:
>> 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é
>

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

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar


Thread

Re: Printing BufferedImage witch high resolution agipstech@gmail.com - 2014-04-10 05:03 -0700
  Re: Printing BufferedImage witch high resolution Knute Johnson <eternal@knutejohnson.com> - 2014-04-12 16:54 -0700

csiph-web