Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing JFC windows to printer 1

Status
Not open for further replies.

VhdlForLife

Programmer
Aug 11, 1999
40
JP
Hi all,<br>
<br>
I'm trying to send a Java 2 window to the printer for output as graphics but I'm not able to get it to print. It can print only a dot. This is probably because I used a normal Frame instead of a JFrame. How should one go about doing this? Any reference book available?
 
If you mean a window in Windows 95, you can take a screen shot of it. Their is a print screen key, when the window you want to take a picture of is maximized, press that key. It places all of those pixels into graphic memory, just go to paint or some other graphical program and go edit, paste. It will place the entire screen into a graphical format so you might have to do some cropping. Also if you hold down the Windows key and then press the print screen key it takes a picture of just the window that is maximized. hope this helps<br>
<br>
ackka<br>
<A HREF="mailto:tmoses@iname.com">tmoses@iname.com</A>
 
If you are trying to print with AWT, find a 1.1 book with a chapter on printing.<br>
But, if you can upgrade to JFrame, life will be easier for you.<br>
Here's a test case that extends JEditorPane to be a printable editor.<br>
The first file just tests the second. See the print() method overridden in the second file, the PrintableEditor:<br>
---------------------------------------------<br>
import java.awt.*;<br>
import java.awt.event.*;<br>
import java.awt.print.*;<br>
import javax.swing.*;<br>
public class PrintTest extends JPanel{<br>
private String text = &quot;Once upon a time...\nin a galaxy far, far, away...&quot;+<br>
&quot;\nthere was a story to be told...\nsimilar to stories told many times before.&quot;;<br>
private JButton portrait = new JButton(&quot;Portrait&quot;);<br>
private JButton landscape = new JButton(&quot;Landscape&quot;);<br>
private PrintableEditor editor;<br>
private JToolBar toolbar = new JToolBar();<br>
public PrintTest() {<br>
try { editor = new PrintableEditor(&quot;&quot;,text); }<br>
catch (Exception e) {}<br>
portrait.addActionListener(editor);<br>
landscape.addActionListener(editor);<br>
toolbar.add(portrait);<br>
toolbar.add(landscape);<br>
setLayout(new BorderLayout());<br>
add(toolbar, BorderLayout.NORTH);<br>
add(new JScrollPane(editor), BorderLayout.CENTER);<br>
}<br>
public static void main(String args[]) {<br>
JFrame jf = new JFrame(&quot;Swing Print Test&quot;);<br>
jf.addWindowListener(new WindowAdapter() {<br>
public void windowClosing(WindowEvent e) {System.exit(0);}<br>
});<br>
jf.getContentPane().add(new PrintTest());<br>
jf.setSize(new Dimension(250,400));<br>
jf.show();<br>
}<br>
}<br>
-----------------------------------------------------<br>
import java.awt.event.*;<br>
import java.awt.print.*;<br>
import javax.swing.*;<br>
public class PrintableEditor extends JEditorPane<br>
implements Printable, ActionListener {<br>
public PrintableEditor() { super(); }<br>
public PrintableEditor(String type, String text) { super(type,text); }<br>
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {<br>
Graphics2D g2 = (Graphics2D) g;<br>
g2.translate(pf.getImageableX(), pf.getImageableY());<br>
this.paint (g2);<br>
return Printable.PAGE_EXISTS;<br>
}<br>
public void actionPerformed(ActionEvent e) {<br>
int orientation = ((e.getActionCommand()).equalsIgnoreCase(&quot;LANDSCAPE&quot;)) ?<br>
PageFormat.LANDSCAPE : PageFormat.PORTRAIT ;<br>
PrinterJob printerJob = PrinterJob.getPrinterJob();<br>
PageFormat pageFormat = printerJob.defaultPage();<br>
pageFormat.setOrientation(orientation);<br>
Book book = new Book();<br>
book.append((Printable)this, pageFormat);<br>
printerJob.setPageable(book);<br>
try { printerJob.print(); }<br>
catch (PrinterException pe) { System.out.println(pe); }<br>
}<br>
}<br>
-------------------------------------------------
 
Thanks wadewells. I'm using Java 2 so the example you gave really helps. Thanks a bunch!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top