The support for printing is provided since JDK version 1.1, which introduced the java.awt.PrintJob class. This class
provides an instance of a java.awt.Graphics object, which can be passed to the method paint(Graphics g) of
subclasses of GenericGraph to paint any chart generated by JetChart.
JDK version 1.2 introduced the java.awt.print.PrinterJob class and some interfaces, like
java.awt.print.Pageable and java.awt.print.Printable, which make up a more flexible and powerful printing
interface than that presented by the PrintJob class itself.
The following application prints a pie chart and is focused on the usage of the PrinterJob class. For detailed information
concerning the printing API of Java version 1.2, please refer to the documentation available at the Java official website.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.print.*; import com.jinsight.jetchart.*; public class Main extends JFrame implements ActionListener { PieGraph graph; public Main() { // Creates a pie chart context. graph=new PieGraph(); graph.setTitle(new String[]{"The JetChart Library","Printing charts"}); graph.set3DEnabled(true); // Creates a pie series. PieSerie ps=new PieSerie(); ps.setBorderEnabled(true); String[] titles={"slice1","slice2","slice3","slice4","slice5"}; double[] values={100,80,130,60,200}; Color[] colors={Color.red,Color.yellow,Color.green,Color.blue,Color.magenta}; // Creates five slices and adds them to the pie series object. for (int counter=0;counter<values.length;counter++) { Slice slice=new Slice(values[counter],titles[counter],colors[counter]); SliceLegend sl=slice.getSliceLegend(); sl.setEnabled(true); sl.setPosition(SliceLegend.OUTSIDE); ps.addSlice(slice); } graph.addSerie(ps); // Creates a top panel and adds a button to print chart. JPanel topPanel=new JPanel(new FlowLayout(FlowLayout.LEFT)); JButton b=new JButton("Print chart"); b.addActionListener(this); topPanel.add(b); Container ct=getContentPane(); ct.add("North",topPanel); ct.add("Center",graph); setSize(400,400); setVisible(true); } public void actionPerformed(ActionEvent evt) { // Gets a PrinterJob instance. PrinterJob pj=PrinterJob.getPrinterJob(); // Creates a Book instance, appending a GraphPrinter object that implements the // Printable interface. Book book=new Book(); book.append(new GraphPrinter(),new PageFormat()); // Sets the PrinterJob Pageable object(the Book instance). pj.setPageable(book); // Displays the printer dialog. boolean canPrint=pj.printDialog(); if (canPrint) { try { // Prints chart. pj.print(); } catch (PrinterException e) { e.printStackTrace(); } } } // This inner class implements the Printable interface and it is responsible for drawing the page content. class GraphPrinter implements Printable { public int print(Graphics g,PageFormat pageFormat,int pageIndex) { // Using Java2D, cast the Graphics object into a Graphics2D instance. Graphics2D g2d=(Graphics2D)g; /* Move the origin from the corner of the Paper to the corner * of the imageable area. */ g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); graph.paint(g2d); return Printable.PAGE_EXISTS; } } public static void main(String[] args) { new Main(); } }