1。Java的打印 众所周知,java的打印功能很弱,但有时出于需要,不得不接触它的用法。前俩天小柯查看了jdk 的API文档,的确不" />
龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

容易忽视的Java三用法小议

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
?XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" /> 1。Java的打印 众所周知,java的打印功能很弱,但有时出于需要,不得不接触它的用法。前俩天小柯查看了jdk 的API文档,的确不

<?XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" /> 


  1。Java的打印  
  
  
  众所周知,java的打印功能很弱,但有时出于需要,不得不接触它的用法。前俩天小柯查看了jdk 的API文档,的确不轻易。但解决方法还是有的。
  java的打印类都在java.awt.print包下,主要有四个类和两个interface:PrinterJob,PageFormat, Paper,Book; Printable,Pageable ,Interface包括 Printable:主要是用来打印的接口,在打印的时候,它的print()方法不断地被调用,直到返回NO_SUCH_PAGE为止。
  
  PrinterJob:初始化打印操作,可以显示系统特定的打印对话框,例如windows的。
  
  PageFormat:描述可打印区。例如我的程序用的几个方法
  
  
  public double getImageableX();    
  public double getImageableY();
  public double getImageableWidth();
  public double getImageableHeight();
     
  
  package jinicup.printer;
  import java.awt.*;
  import java.awt.print.*;
  import java.awt.event.*;
  import javax.swing.JPanel;
  import javax.swing.JFrame;
  import javax.swing.ImageIcon;
  import java.io.*;
  
  /**********************************
  * Implemenation of the printer service
  ***********************************/
  public class PrinterImpl extends JPanel
  implements Printable {
  private Image image;
  private PrinterJob printJob;
   
  private double x,y,w,h;
  private int  imagew,imageh;
  
  PrinterImpl () {
  
   printJob = PrinterJob.getPrinterJob();
   printJob.setPrintable(this);
   printJob.printDialog();
  }
  
  public int print (Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
   System.out.println("pageIndex"+pageIndex);
  if (pageIndex >= 1) {
    return Printable.NO_SUCH_PAGE;
   }
   x = pageFormat.getImageableX();
  y = pageFormat.getImageableY();
  w = pageFormat.getImageableWidth();
  h = pageFormat.getImageableHeight();
   
   if(imagew >= imageh){
   h=w*imageh/imagew;
  }else{
   w=h*imagew/imageh;
  }
  
  System.out.println(x+" "+y);
   System.out.println(w+" "+h);
  
  
  drawGraphics(graphics);
   return Printable.PAGE_EXISTS;
  }
  
  public void paint (Graphics graphics) {
   drawGraphics(graphics);
  }
  
  private void drawGraphics (Graphics graphics) {
  
  graphics.drawImage(image, (int)x,(int)y,(int)w,(int)h, null);
   // graphics.drawOval(10, 10, 100, 50);
  }
  
  /**********************************
  * 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 = Toolkit.getToolkit();
  // image=tool.createImage(byteArrayOfJPEGFile);
  image = (new ImageIcon(byteArrayOfJPEGFile)).getImage();
  
   imagew=image.getWidth(null);
  imageh=image.getHeight(null);
  System.out.println(imagew+" "+imageh);
  
  System.out.println("kkk");
   try {
    System.out.println("start printing");
    printJob.print();
    System.out.println("printing was spooled to the printer");
   } catch (Exception ex) {
    System.out.println(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:/test.jpg");
  System.out.println(fs.available());
    byte[] array = new byte[fs.available()];
    fs.read(array);
    pi.printByteArray(array);
     } catch (Exception e) {
    System.out.println(e);
   }
  }
  }
  
  
  
  2。运行一个外部程序捕捉并输出  
  
  首先运行ping程序,然后把它的输出打印到屏幕上。
  
  
  class Main {
   public static void main(String[] args) {
     try {
       String cmd = "ping ";
  String param ="202.112.58.200";
       Process child = Runtime.getRuntime().exec(cmd+param);
       // 获得ping的输出
      InputStream child_in = child.getInputStream();
       int c;
       while ((c = child_in.read()) != -1) {
  //   System.out.println("kkk");
         System.out.print((char)c);
       }
       child_in.close();
     } catch (IOException e) {
       System.err.println(e);
     }
   }
  }
   
  
  3。用Java实现剪贴板  
  
  
  
  在Java中实现剪贴板功能是使用java.awt.datatransfer.Clipboard类。该类就是实现了虚拟的剪贴板,它有想剪贴板内写内容,以及取出剪贴板上内容的方法,同时还指明这个剪贴板内存区域是属于哪个对象的。例如文本框之类的部件。


  
精彩图集

赞助商链接