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ū)域是屬于哪個對象的。例如文本框之類的部件。
新聞熱點
疑難解答