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类。该类就是实现了虚拟的剪贴板,它有想剪贴板内写内容,以及取出剪贴板上内容的方法,同时还指明这个剪贴板内存区域是属于哪个对象的。例如文本框之类的部件。
接口transferable主要是为传输操作提供数据。
真正的数据内容是由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 {
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);
}
}
}
如果文章有错误之处还望网友来信指出bbcolour@21cn.com小柯。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器