熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

對於java的打印問題

2022-06-13   來源: JSP教程 

  對於java的打印問題各種書上談的很少我想主要原因可能是java的打印功能太弱了沒有什麼可介紹的 最近我因為工作的關系用到了java的打印圖象功能不過因為缺少可參考的例子和教材我只有查看jdk 的API文檔的確不是很爽下面就把我的程序給大家講講順便白話一下java的打印(是jdk的) java的打印類都在javaawtprint包下主要有四個類和兩個interfacePrinterJobPageFormat PaperBook; PrintablePageable(詳細的情況請查看jdk的API文檔我只講我程序中用到的部分)
    Interface包括 Printable主要是用來打印的接口在打印的時候它的print()方法不斷地被調用直到 返回NO_SUCH_PAGE為止
  
  PrinterJob:初始化打印操作可以顯示系統特定的打印對話框例如windows的
  
  
  PageFormat:描述可打印區例如我的程序用的幾個方法
  
  
  public double getImageableX();
  public double getImageableY();
  public double getImageableWidth();
  public double getImageableHeight();
  
  
  
  package jinicupprinter;
  
  
  import javaawt*;
  import javaawtprint*;
  import javaawtevent*;
  import javaxswingJPanel;
  import javaxswingJFrame;
  import javaxswingImageIcon;
  import javaio*;
  
  
  /**********************************
  * Implemenation of the printer service
  ***********************************/
  public class PrinterImpl extends JPanel
  implements Printable {
  private Image image;
  private PrinterJob printJob;
  
  private double xywh;
  private int imagewimageh;
  
  
  PrinterImpl () {
  
  
  printJob = PrinterJobgetPrinterJob();
  printJobsetPrintable(this);
  printJobprintDialog();
  }
  
  
  public int print (Graphics graphics PageFormat pageFormat int pageIndex) throws PrinterException {
  Systemoutprintln(pageIndex+pageIndex);
  if (pageIndex >= ) {
  return PrintableNO_SUCH_PAGE;
  }
  x = pageFormatgetImageableX();
  y = pageFormatgetImageableY();
  w = pageFormatgetImageableWidth();
  h = pageFormatgetImageableHeight();
  
  if(imagew >= imageh){
  h=w*imageh/imagew;
  }else{
  w=h*imagew/imageh;
  }
  
  Systemoutprintln(x+ +y);
  Systemoutprintln(w+ +h);
  
  
  
  drawGraphics(graphics);
  return PrintablePAGE_EXISTS;
  }
  
  
  public void paint (Graphics graphics) {
  drawGraphics(graphics);
  }
  
  
  private void drawGraphics (Graphics graphics) {
  
  
  graphicsdrawImage(image (int)x(int)y(int)w(int)h null);
  // graphicsdrawOval( );
  }
  
  
  /**********************************
  * starts the printing
  * @param byteArrayOfJPEGFile a valid byte array of a jpg file (can be directly from the camera)
  ***********************************/
  public void printByteArray (byte[] byteArrayOfJPEGFile) {
  // Toolkit tool = ToolkitgetToolkit();
  // image=toolcreateImage(byteArrayOfJPEGFile);
  image = (new ImageIcon(byteArrayOfJPEGFile))getImage();
  
  imagew=imagegetWidth(null);
  imageh=imagegetHeight(null);
  Systemoutprintln(imagew+ +imageh);
  
  
  Systemoutprintln(kkk);
  try {
  Systemoutprintln(start printing);
  printJobprint();
  Systemoutprintln(printing was spooled to the printer);
  } catch (Exception ex) {
  Systemoutprintln(ex);
  }
  return;
  }
  
  
  /**********************************
  * main method only for text purposes
  * @param args no args are used
  ***********************************/
  public static void main (String[] args) {
  PrinterImpl pi = new PrinterImpl();
  try {
  FileInputStream fs = new FileInputStream(e:/testjpg);
  Systemoutprintln(fsavailable());
  byte[] array = new byte[fsavailable()];
  fsread(array);
  piprintByteArray(array);
  } catch (Exception e) {
  Systemoutprintln(e);
  }
  }
  }
  

From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19135.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.