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

public class TesteSimples extends JFrame implements ActionListener, Printable
{
	public TesteSimples()
  {
  	super("Teste Simples");

  	//Log area
    final JTextArea log = new JTextArea(5,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);

	  //Create the print button
	  JButton printButton = new JButton("Print...");
 		printButton.addActionListener(this);

    //For layout purposes, put the button in a separate panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(printButton);

    //Add the button and the log to the frame
    Container contentPane = getContentPane();
    contentPane.add(buttonPanel, BorderLayout.NORTH);
    contentPane.add(logScrollPane, BorderLayout.CENTER);
  }

  public static void main(String[] args)
  {
  	JFrame frame = new TesteSimples();

		frame.addWindowListener(new WindowAdapter() {
    	public void windowClosing(WindowEvent e)
    	{
      	System.exit(0);
      }
    });

    frame.pack();
    frame.setVisible(true);
  }

	// Interface ActionListener
	public void actionPerformed(ActionEvent e)
	{
		PrinterJob printJob = PrinterJob.getPrinterJob();
		printJob.setPrintable(this);
		if (printJob.printDialog())
		{
			try
			{
				printJob.print();
			}
			catch (Exception exc)
			{
				exc.printStackTrace();
			}
		}
  }

	// Interface Printable
  public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
  {
		if (pi >=1)
		{
			return Printable.NO_SUCH_PAGE;
		}

		Graphics2D g2 = (Graphics2D) g;
		g2.translate(pf.getImageableX(), pf.getImageableY());
		getContentPane().paint(g2);

		return Printable.PAGE_EXISTS;
	}
}

