JAVAUSER
asked on
How can I use PrintJob in JDK1.2.2 Applet?
Now I have installed JDK1.2.2 , and I want write an applet which can do this:
1. It gets data from web server.
2. It uses local printer to print the data .
Who can help me to solve the problem?
thank you very much!
1. It gets data from web server.
2. It uses local printer to print the data .
Who can help me to solve the problem?
thank you very much!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much!
The following code is used to print from an applet.
Hope this is useful for u.
Bye....
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class printex extends Applet implements ActionListener { boolean isApplet = true; //
Application/Applet flag Button bPrint;
Font myFont1; Font myFont2; FontMetrics myFM;
Graphics myPG;
printFrame myFrame; Panel pSouth; PrintJob myPrintJob;
String myString;
public void init() { myString = new String( "Bye, Elvis!" );
myFrame = new printFrame( "Print Example" );
myFrame.setBackground( Color.lightGray );
pSouth = new Panel(); bPrint = new Button( "Print" ); bPrint.addActionListener( this ); pSouth.add( bPrint );
myFrame.add( "South", pSouth );
myFont1 = new Font( "Serif", Font.BOLD, 20 ); myFont2 = new Font( "SansSerif", Font.BOLD, 10 );
myFrame.addWindowListener(
false );
} // End init
public void start() { myFrame.show();
// get Frame dimensions myFrame.sizeIt();
} // End start
class printFrame extends Frame // the applet's frame { int height; int width;
printFrame( String theTitle ) { super( theTitle ); height = 0; width = 0; }
public void paint( Graphics g ) { g.setFont( myFont1 ); myFM = g.getFontMetrics();
g.drawString( myString, (width - myFM.stringWidth( myString ) ) / 2, (height - myFM.getHeight() ) / 2 );
} // End paint
void print() { myPrintJob = getToolkit().getPrintJob( this, getTitle(), null );
if ( myPrintJob != null ) { int x = 5, y = 0; int myPHeight, myPR; Dimension myPD;
myPG = myPrintJob.getGraphics();
if ( myPG != null ) { myPD = myPrintJob.getPageDimensio
myPG.setFont( myFont2 ); myFM = myPG.getFontMetrics(); myPHeight = myFM.getHeight();
y += ( myPHeight + 5 ); myPG.drawString( ( "Page width is: " + myPD.width + "." ), x, y );
y += ( myPHeight + 5 ); myPG.drawString( ( "Page height is: " + myPD.height + "." ), x, y );
y += ( myPHeight + 5 ); myPG.drawString( ( "Page resolution is: " + myPR + "." ), x, y );
// Print text in diminishing fonts for( x = 20, y = 1; x > 2; y++ ) { myPG.setFont( myFont1 ); myFM =
myPG.getFontMetrics();
myPG.drawString( myString, ( myPD.width - myFM.stringWidth( myString ) ) / 2, ( myPD.height -
myFM.getHeight() ) / 2 );
// Print page Number myPG.setFont( myFont2 ); myFM = myPG.getFontMetrics();
String aString = new String( "Page " + y ); myPG.drawString( aString, ( myPD.width - myFM.stringWidth(
aString ) ) / 2 , myPD.height );
// create diminishing font myFont1 = new Font( "Serif", Font.BOLD, (int)( x /= 2 ) );
myPG.dispose(); // flush page to printer
// get new graphics if we will print again if( x > 2 ) { myPG = myPrintJob.getGraphics(); }
} // end for
// reset myFont1 myFont1 = new Font( "Serif", Font.BOLD, 20 );
} // End if PrintGraphics not null
} // End if PrintJob not null
myPrintJob.end();
return; } // End print
// get this frame's drawable space void sizeIt( ) { Dimension d = getSize(); Insets i = getInsets();
height = d.height - ( i.right + i.left ); width = d.width - ( i.bottom + i.top );
} // End sizeIt
} // End class PrintFrame
// ActionListener public void actionPerformed(ActionEven
if (oSource == bPrint) { myFrame.print(); return; } } // End actionPerformed
// WindowListener Adapter public class myWAdapter extends WindowAdapter { public void
windowClosing(WindowEvent e) { if( isApplet ) { myFrame.dispose(); } else { System.exit(0); } } } // End
myWAdapter
public static void main(String args[]) { printex myApp = new printex(); // inform instance variable started as an
application myApp.isApplet = false;
myApp.init(); myApp.start();
return;
} // End main
} // End Class printex