国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

容易忽視的Java三用法小議

2019-11-18 11:04:38
字體:
供稿:網(wǎng)友

 


1。java的打印  


眾所周知,java的打印功能很弱,但有時出于需要,不得不接觸它的用法。前倆天小柯查看了jdk 的API文檔,的確不輕易。但解決方法還是有的。
java的打印類都在java.awt.PRint包下,主要有四個類和兩個interface:PrinterJob,PageFormat, Paper,Book; Printable,Pageable ,Interface包括 Printable:主要是用來打印的接口,在打印的時候,它的print()方法不斷地被調(diào)用,直到返回NO_SUCH_PAGE為止。

PrinterJob:初始化打印操作,可以顯示系統(tǒng)特定的打印對話框,例如windows的。

PageFormat:描述可打印區(qū)。例如我的程序用的幾個方法


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實現(xiàn)剪貼板  



在Java中實現(xiàn)剪貼板功能是使用java.awt.datatransfer.Clipboard類。該類就是實現(xiàn)了虛擬的剪貼板,它有想剪貼板內(nèi)寫內(nèi)容,以及取出剪貼板上內(nèi)容的方法,同時還指明這個剪貼板內(nèi)存區(qū)域是屬于哪個對象的。例如文本框之類的部件。



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 焦作市| 忻州市| 台州市| 龙门县| 新龙县| 额尔古纳市| 辉县市| 巴彦县| 贵州省| 华阴市| 水城县| 城步| 谢通门县| 吴川市| 德兴市| 库伦旗| 玉龙| 霍邱县| 紫阳县| 鞍山市| 绵竹市| 龙山县| 台安县| 神木县| 抚远县| 绿春县| 屏东县| 阿鲁科尔沁旗| 湘西| 乌鲁木齐市| 乌拉特后旗| 清水河县| 合水县| 莱芜市| 阿图什市| 始兴县| 淮南市| 文成县| 舞钢市| 仁寿县| 加查县|