1. Java Printing
As we all know, java's printing function is very weak, but sometimes it has to be used out of its needs. Xiao Ke checked the jdk API documentation two days ago, which is indeed not easy. But there are still some solutions.
Java printing classes are all in java. awt. in the print package, there are four main classes and two interfaces: PrinterJob, PageFormat, Paper, Book; Printable, Pageable. interfaces include Printable: The interface used for printing, its print () method is continuously called until NO_SUCH_PAGE is returned.
PrinterJob: Initialize the print operation to display the system-specific Print dialog box, such as windows.
PageFormat: Description of the printable area. For example, how many methods are used in my program?
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 bytearrayofw.file a valid byte array of a jpg file (can be directly from the camera)
***********************************/
Public void printByteArray (byte [] bytearrayof1_file ){
// Toolkit tool = Toolkit. getToolkit ();
// Image = tool. createImage (bytearrayof1_file );
Image = (new ImageIcon (bytearrayof1_file). 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. Run an external program to capture and Output
Run the ping program and print its output to the screen.
Class Main {
Public static void main (String [] args ){
Try {
String cmd = "ping ";
String param = "202.112.58.200 ";
Process child = runtime.getruntime(cmd.exe c (cmd + param );
// Obtain the ping output
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. Clipboard implementation using Java
In Java, the Clipboard function is implemented using the java. awt. datatransfer. Clipboard class. This class implements a virtual clipboard. It can write content in the clipboard and retrieve the content on the clipboard. It also specifies the object in which the clipboard memory area belongs. Such as text boxes.
InterfaceTransferable provides data for transmission operations.
The real data content is represented by the java. awt. datatransfer. DataFlavor class. It can represent two different data formats: plain text and Java Unicode String class.
The following program example is a complete clipboard.
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 actionreceivmed (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 (