package com.mindprod.example;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import static java.lang.System.*;
/**
* Demonstrate use of Print using PrinterJob.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2007-07-10
* @since 2007-07-10
*/
public final class TestPrinterJob extends JFrame implements Printable
{
/**
* constructor
*
* @param title label for title of frame.
*/
@SuppressWarnings( { "SameParameterValue", "WeakerAccess" } )
public TestPrinterJob( String title )
{
super( title );
}
/**
* print the entire frame, including the title bar with the close widget.
*/
private void printFrame()
{
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable( this );
if ( printerJob.printDialog() )
{
try
{
printerJob.print();
}
catch ( PrinterException e )
{
err.println( "Error printing: " + e );
}
}
}
/**
* standard main method to run the test.
*
* @param args not used
*/
public static void main( String[] args )
{
JFrame.setDefaultLookAndFeelDecorated( true );
final TestPrinterJob jframe = new TestPrinterJob( "TestPrinterJob" );
final Container contentPane = jframe.getContentPane();
jframe.setSize( 215, 100 );
contentPane.setBackground( Color.WHITE );
contentPane.setForeground( Color.BLUE );
contentPane.setLayout( new BorderLayout() );
jframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
final JTextArea t =
new JTextArea(
"All good men\n"
+ "should come to the aid of the party."
);
contentPane.add( t, BorderLayout.NORTH );
final JButton print = new JButton( "Print" );
print.setBorder( BorderFactory.createRaisedBevelBorder() );
print.setFocusPainted( false );
print.setToolTipText( "Print the page" );
print.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
print.addActionListener( new ActionListener()
{
/**
* user clicked save.
* @param event button push event
*/
public void actionPerformed( ActionEvent event )
{
jframe.printFrame();
}
} );
contentPane.add( print, BorderLayout.SOUTH );
jframe.validate();
jframe.setVisible( true );
}
/**
* Draw this component on a printer, a page at a time. This component may be so large it could never fit on a
* screen.
*
* @param gr the context into which the page is drawn, high res for printer.
* @param pageFormat the paper size and orientation (landscape/portrait) of the page being drawn.
* @param pageIndex the zero based index of the page to be drawn.
*
* @return Printable.PAGE_EXISTS if the page was rendered successfully or Printable.NO_SUCH_PAGE if pageIndex
* specifies a non-existent page. This is how the engine knows when you are done.
*/
@SuppressWarnings( { "UnnecessaryLocalVariable" } )
public int print( Graphics gr,
PageFormat pageFormat,
int pageIndex )
{
if ( pageIndex > 0 )
{
return Printable.NO_SUCH_PAGE;
}
final Graphics2D g2 = ( Graphics2D ) gr;
final AffineTransform origTransform = g2.getTransform();
final AffineTransform transform = origTransform;
transform.translate( 36.0d, 36.0d );
g2.setTransform( transform );
paint( gr );
g2.setTransform( origTransform );
return Printable.PAGE_EXISTS;
}
}