Source Code : Another print demo

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Another print demo

Another print demo
    

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnotherPrintDemo extends JFrame {
  DrawingCanvas canvas;

  JButton setUpButton = new JButton("Page Setup");

  JButton printButton = new JButton("Print");

  JButton cancelButton = new JButton("Cancel");

  public AnotherPrintDemo() {
    super();
    Container container = getContentPane();

    canvas = new DrawingCanvas();
    container.add(canvas);

    JPanel panel = new JPanel(new GridLayout(1, 3));

    ButtonListener buttonListener = new ButtonListener();
    setUpButton.addActionListener(buttonListener);
    panel.add(setUpButton);

    printButton.addActionListener(buttonListener);
    panel.add(printButton);

    cancelButton.addActionListener(buttonListener);
    panel.add(cancelButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    setSize(650, 275);
    setVisible(true);
  }

  class ButtonListener implements ActionListener {
    PrinterJob printJob;

    PageFormat pageFormat;

    PrintableCanvas printableCanvas;

    ButtonListener() {
      printJob = PrinterJob.getPrinterJob();
      pageFormat = printJob.defaultPage();
    }

    public void actionPerformed(ActionEvent e) {
      JButton tempButton = (JButton) e.getSource();

      if (tempButton.equals(setUpButton)) {
        pageFormat = printJob.pageDialog(pageFormat);
        printJob.validatePage(pageFormat);
      } else if (tempButton.equals(printButton)) {
        printableCanvas = new PrintableCanvas(pageFormat);
        printJob.setPrintable(printableCanvas);

        boolean ok = printJob.printDialog();
        if (ok) {
          try {
            printJob.print();
          } catch (Exception pe) {
            System.out.println("Printing Exception Occured!");
            pe.printStackTrace();
          }
        }
      } else if (tempButton.equals(cancelButton)) {
        printJob.cancel();
      }
    }
  }

  public static void main(String arg[]) {
    new AnotherPrintDemo();
  }
}

class DrawingCanvas extends JPanel {
  Font font;

  FontMetrics fontMetrics;

  int w, h;

  DrawingCanvas() {
    setBackground(Color.white);
    setSize(400, 275);

    w = this.getWidth();
    h = this.getHeight();

    font = new Font("Dialog", Font.BOLD, 50);
    fontMetrics = getFontMetrics(font);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g); 
    Graphics2D g2D = (Graphics2D) g;

    paintContent(g2D, w, h);
  }

  public void paintContent(Graphics2D g2D, int w, int h) {
    g2D.setFont(font);

      g2D.drawString("Java Source and Support", 0,
        (float) (0.5 * h - 1.25 * fontMetrics.getHeight()));

  }
}

class PrintableCanvas implements Printable {
  DrawingCanvas canvas;

  PageFormat pageFormat;

  public PrintableCanvas(PageFormat pf) {
    pageFormat = pf;
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex)
      throws PrinterException {
    if (pageIndex >= 1) {
      return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2D = (Graphics2D) g;

    canvas = new DrawingCanvas();

    canvas.paintContent(g2D, (int) pageFormat.getImageableWidth(),
        (int) pageFormat.getImageableHeight());

    // successful printing of the page
    return Printable.PAGE_EXISTS;
  }
}



           
         
    
    
    
  

Thank with us