Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Printing From Java Applet

Started by Thoover Mar 1, 2010 at 5:02 PM 0 replies 2.1k views
Original Post
Thoover
Thoover
I have been looking for about 3 hours, it will come up with the print dialog but never prints, here is the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
import java.awt.print.PageFormat;
import java.lang.String;
import java.io.*;
import java.util.*;
import java.applet.Applet;
import java.awt.GridLayout;
import java.awt.TextArea;


public class label extends JApplet implements Printable
{
	String one = "";
	String two = "";
	public void init() 
	{
		one=getParameter("one");
		two=getParameter("two");
		PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
              System.out.println("ERR");/* The job did not successfully complete */
             }
         }
	}
	
     public int print(Graphics g, PageFormat pf, int page) throws
                                                        PrinterException {

        if (page > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
	g.setColor(Color.decode("0x000000"));
        /* Now we perform our rendering */
        g.drawString(one, 2, 10);
        g.drawString(two, 2, 50);
        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }
}

any help appreciated P.S. this is printing to a SLP240
FlaiseSaffron
FlaiseSaffron
You might be running into a security exception. Run your applet one more time, and check your Java console (on Windows, right-click the Java icon on your task bar and select "Open Java Console"). If you are indeed running into a security exception, then you will have to sign your applet.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.