Newsgroup: it.comp.java
Titolo: Stampare un JPanel

Qualcuno sa come si può stampare il contenuto di un JPanel?
Grazie!
-----------------------------------------------------
NICOLA "Magic touch" FOCCI - 
n.f...@libero.it
http://digilander.iol.it/pumait
http://www.gruppopro.it/proqualitas
-----------------------------------------------------
"Life is what happens when you're making other plans"
				(John Lennon)

Qualcuno sa come si può stampare il contenuto di un JPanel?
Prova a studiare questo esempio costruito con jbuilder 3 e tratto
dalla documentazione online della Sun.
Ha l'importante difetto di non adattare le dimensioni del frame a
quelle delle pagina. Per migliorare le cose ho dovuto aggiungere il
metodo scale(0.5, 0.5)  , se qualcuno ha di meglio lo ringrazio
anticipatamente.
Ciao, 
Francesco Mascarello.
//file Frame1.java
package PrintApp;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
public class Frame1 extends JFrame  implements Printable{
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel1 = new JPanel();
  JButton jButtonStampa = new JButton();
  BorderLayout borderLayout2 = new BorderLayout();
  JPanel jPanel2 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  BorderLayout borderLayout3 = new BorderLayout();
  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try  {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    this.getContentPane().setLayout(borderLayout1);
    Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
   // frame grande
    this.setSize(screenSize.width *8/9 ,screenSize.height*8/9); 
  //    this.setSize(new Dimension(400, 300)); // frame piccolo
    this.setTitle("Frame Title");
    jButtonStampa.setText("Stampa");
    jButtonStampa.addActionListener(new
java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButtonStampa_actionPerformed(e);
      }
    });
    jPanel1.setLayout(borderLayout2);
    jLabel1.setText("jLabel1");
    jLabel2.setText("jLabel2");
    jPanel2.setLayout(borderLayout3);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jButtonStampa, BorderLayout.SOUTH);
    jPanel1.add(jPanel2, BorderLayout.NORTH);
    jPanel2.add(jLabel2, BorderLayout.WEST);
    jPanel2.add(jLabel1, BorderLayout.EAST);
  }
  //Overridden so we can exit on System Close
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
  public int print(Graphics g, PageFormat pf, int pageIndex) {
    if (pageIndex != 0) return NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D)g;
    double d1,d2;
    d1=pf.getImageableX();
    d2=pf.getImageableY();
    g2.translate(d1,d2 );
    g2.scale(0.45,0.45); // migliorare!!
    g2.setColor(Color.black);
    getContentPane().paint(g2);
    return PAGE_EXISTS;
  }
  void jButtonStampa_actionPerformed(ActionEvent e) {
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(Frame1.this);
        if (pj.printDialog()) {
          try { pj.print(); }
          catch (PrinterException pe) {
            System.out.println(pe);
          }
        }
  }
} // end class
//file Application1.java
package PrintApp;
import javax.swing.UIManager;
public class Application1 {
  boolean packFrame = false;
  //Construct the application
  public Application1() {
    Frame1 frame = new Frame1();
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from
their layout
    if (packFrame)
      frame.pack();
    else
      frame.validate();
    frame.setVisible(true);
  }
  //Main method
  public static void main(String[] args) {
    try  {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
    }
    new Application1();
  }
}