1。Java的打印 众所周知,java的打印功能很弱,但有时出于需要,不得不接触它的用法。前俩天小柯查看了jdk 的API文档,的确不容易。但解决方法还是有的。 java的打印类都在java.awt.print包下,主要有四个类和两" name="description" />
MILY: 宋体; mso-bidi-font-size: 10.0pt"> 接口Transferable主要是为传输操作提供数据。 System.out.println("pageIndex"+pageIndex);
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类。该类就是实现了虚拟的剪贴板,它有想剪贴板内写内容,以及取出剪贴板上内容的方法,同时还指明这个剪贴板内存区域是属于哪个对象的。例如文本框之类的部件。
真正的数据内容是由java.awt.datatransfer.DataFlavor类来表示的。它可以表示plain text和Java Unicode String class两种不同数据的格式。
下面的程序例子就是一个完整的剪贴板。
import java.awt.*;
import java.io.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
class Main extends Frame implements ActionListener, ClipboardOwner {
TextArea textArea = new TextArea();
Main() {
super("Clipboard Example");
MenuBar mb = new MenuBar();
Menu m = new Menu("Edit");
// Add text area.
setLayout(new BorderLayout());
add("Center", textArea);
// Prepare menu and menubar.
m.add("Cut");
m.add("Copy");
m.add("Paste");
m.add("Exit");
mb.add(m);
setMenuBar(mb);
// Listen to events from the menu items.
for (int i=0; i < m.getItemCount(); i++) {
m.getItem(i).addActionListener(this);
}
setSize(300, 300);
show();
}
public void actionPerformed(ActionEvent evt) {
if ("Paste".equals(evt.getActionCommand())) {
boolean error = true;
Transferable t =
getToolkit().getSystemClipboard().getContents(this);
try {
if (t != null
&& t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
textArea.setBackground(Color.white);
textArea.setForeground(Color.black);
textArea.replaceRange(
(String)t.getTransferData(DataFlavor.stringFlavor),
textArea.getSelectionStart(),
textArea.getSelectionEnd());
error = false;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
// Display an error message.
if (error) {
textArea.setBackground(Color.red);
textArea.setForeground(Color.white);
textArea.repaint();
textArea.setText("ERROR: \nEither the clipboard"
+ " is empty or the contents is not a string.");
}
} else if ("Copy".equals(evt.getActionCommand())) {
setContents();
} else if ("Cut".equals(evt.getActionCommand())) {
setContents();
textArea.replaceRange("", textArea.getSelectionStart(),
textArea.getSelectionEnd());
} else if ("Exit".equals(evt.getActionCommand()))
{
this.dispose();
System.exit(0);
}
}
void setContents() {
String s = textArea.getSelectedText();
StringSelection contents = new StringSelection(s);
getToolkit().getSystemClipboard().setContents(contents, this);
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
System.out.println("lost ownership");
}
public static void main(String args[]) {
new Main();
}
}
3。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 {
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);
}
}
}
如果文章有错误之处还望网友来信指出bbcolour@21cn.com小柯。