Path: csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.gui Subject: Re: Printing BufferedImage witch high resolution Date: Sat, 12 Apr 2014 16:54:12 -0700 Organization: A noiseless patient Spider Lines: 198 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 12 Apr 2014 23:54:07 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="22626"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WlaYXVgEY9KCTioj0kQj+" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: Cancel-Lock: sha1:Q1m0nU6r0joM8Rn6u8Cqf8VePvY= Xref: csiph.com comp.lang.java.gui:5423 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