The Artima Developer Community
Sponsored Link

Java Answers Forum
How to print in landscape?

1 reply on 1 page. Most recent reply: Apr 5, 2002 7:33 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Avin Sinanan

Posts: 28
Nickname: avin
Registered: Feb, 2002

How to print in landscape? Posted: Apr 5, 2002 6:11 PM
Reply to this message Reply
Advertisement
Hello am using the following code to print. However when the printing window comes up and I choose "Landscape" it never ptints in Landsacpe form. It always prints in "Portarit" what am I doing wrong?

hers the code. Thanks for reading.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
 
 
class Print1 extends JFrame
                          implements ActionListener {
  public static void main(String[] args) {
    new Print1();
  }
 
  public Print1() {
    super("Printing Swing Components");
    Container content = getContentPane();
    JButton printButton = new JButton("Print");
    printButton.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    setBackground(Color.white);
    buttonPanel.setBackground(Color.white);
    buttonPanel.add(printButton);
    content.add(buttonPanel, BorderLayout.SOUTH);
    JPanel drawingPanel = new JPanel();
    content.add(drawingPanel, BorderLayout.CENTER);
    pack();
    setVisible(true);
  }
 
  public void actionPerformed(ActionEvent event) {
    PrintUtilities.printComponent(this);
  }
}
    
class PrintUtilities implements Printable {
  private Component componentToBePrinted;
 
  public static void printComponent(Component c) {
    new PrintUtilities(c).print();
  }
  
  public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
  }
  
  public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
      try {
        printJob.print();
      } catch(PrinterException pe) {
        System.out.println("Error printing: " + pe);
      }
  }
 
  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }
 
  public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }
 
  public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }
}
 


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How to print in landscape? Posted: Apr 5, 2002 7:33 PM
Reply to this message Reply
You need to specify the PageFormat as landscape. I don't know how you get the page format from the printDialog, but you can get it from the pageDialog() call. So where you call setPrintable(), you just need to specify a PageFormat, for example:
printJob.setPrintable(this, printJob.pageDialog( new PageFormat() ));

or
printJob.setPrintable(this, printJob.pageDialog( printJob.defaultPage() ));

Popping up two dialogs is kind of annoying, so it would be better to somehow glean the information from the printDialog(). Or is it just an idiosyncracy of the Windows platform that you can also change the page layout in the printer setup dialog? If you set the layout in the pageDialog and then changed it in the printDialog, the second change would probably be ignored in the example above. I haven't fooled much with printing, so I don't know how that is done, offhand. So rather than read my ramblings, you might want to look at this printing tutorial: http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html

Flat View: This topic has 1 reply on 1 page
Topic: datagramSocket Previous Topic   Next Topic Topic: How to limit  the size of a JTextField?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use