Printing from Applet

Discussion in 'Java' started by umerkle@gis-consulting.de, Jul 15, 2003.

  1. Guest

    Hello,


    i'm trying to print from my applet under Lotus Notes. There is a
    JavaScript function that calls
    the applet's print-method which looks like that:

    public void printApplet () {
    //Panel printPanel = new Panel();
    Panel printTitlePanel = new Panel();
    Panel printTitle = new Panel();

    Color oldColor;
    Color oldForeground;
    Font oldFont;
    Frame frame = new Frame(TransStrings.printDialog);

    Properties printprefs = new Properties();
    Toolkit toolkit = this.getToolkit();
    try{
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
    security.checkPrintJobAccess();
    security.checkPropertiesAccess();
    security.checkTopLevelWindow(frame);
    }
    }catch(SecurityException e){
    e.printStackTrace();
    return;
    }

    PrintJob job = null;
    try{
    job = toolkit.getPrintJob(frame, TransStrings.printDialog,
    printprefs);
    }catch(Exception e){
    e.printStackTrace();
    }
    if (job == null)
    return;
    printTitlePanel.setBackground(Color.white);
    oldColor = this.getBackground();
    this.setBackground(Color.white);
    oldForeground = this.getForeground();
    this.setForeground(Color.black);
    oldFont = this.getFont();
    this.setFont(new Font("Helvetica", Font.PLAIN,10));
    Graphics g = job.getGraphics();
    if (g != null) {
    g.translate(25, 25);
    Dimension size = this.getSize();
    PlanerOptions.planer.add(printTitlePanel,"PRINT");
    PlanerOptions.planer.doLayout();
    PlanerOptions.planer.repaint();
    if (PlanerOptions.ONEUSERMODE) {
    g.setClip(0, 0, PlanerOptions.rowDescrWidth +
    (32*PlanerOptions.xDist),
    size.height);
    }
    else {
    g.setClip(0, 0, size.width, size.height);
    }

    if(PlanerOptions.bShowMessages)
    System.out.println("--> Druck!");

    this.printAll(g);
    PlanerOptions.planer.remove(printTitlePanel);
    PlanerOptions.planer.doLayout();
    PlanerOptions.planer.repaint();
    g.dispose();
    }
    job.end();

    this.setBackground(oldColor);
    this.setForeground(oldForeground);
    this.setFont(oldFont);
    }



    under R5 with JDK 1.1 everything is OK, but under R6 with JDK 1.3.x
    the system hangs while calling job = toolkit.getPrintJob(frame,
    TransStrings.printDialog, printprefs)

    Are there any differences in this function between JDK 1.1 and 1.3 ? What
    is wrong with my code ?



    Uwe
     
    , Jul 15, 2003
    #1
    1. Advertising

  2. Ike Guest

    Uwe,

    Below is simple code that does a beautiful job printing from an Applet under
    1.3.x -- to paint the contents of a Panel. Specifically, I am using it for
    Swing JPanels. Perhaps you may wish to copy your specifics into it. -Ike

    public void doPrint(){
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pageFormat = job.defaultPage();
    job.setPrintable( this , pageFormat );
    if(job.printDialog()){
    try{
    job.print();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    public int print(Graphics g, PageFormat pf, int pageIndex) throws
    PrinterException{
    Graphics2D g2=(Graphics2D)g;
    g2.translate( (int)pf.getImageableX() ,(int)pf.getImageableY() );
    boolean db=isDoubleBuffered();
    //turn off double buffering in swing components before you print or
    it looks like alt-printscrn
    setDoubleBuffered(false);
    paint( g2 );
    setDoubleBuffered(db);
    if( pageIndex > 0 )
    return Printable.NO_SUCH_PAGE;
    else
    return Printable.PAGE_EXISTS;
    }
     
    Ike, Jul 15, 2003
    #2
    1. Advertising

  3. Guest

    Antwort: Re: Printing from Applet

    Hi Ike,

    does this work under 1.1 too?

    My applet has to work under 1.1 and 1.3 !



    Uwe
     
    , Jul 15, 2003
    #3
  4. Ike Guest

    Re: Antwort: Re: Printing from Applet

    Use Panel then instead of JPanel.
    -Ike

    <> wrote in message
    news:bf173c$3t2$04$-online.com...
    > Hi Ike,
    >
    > does this work under 1.1 too?
    >
    > My applet has to work under 1.1 and 1.3 !
    >
    >
    >
    > Uwe
     
    Ike, Jul 15, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Love

    applet vs. canvas in applet

    Rob Love, Jul 7, 2003, in forum: Java
    Replies:
    1
    Views:
    642
    Liu, Chunyen
    Jul 7, 2003
  2. webster
    Replies:
    0
    Views:
    877
    webster
    Jul 20, 2003
  3. Andrew Thompson
    Replies:
    0
    Views:
    430
    Andrew Thompson
    Aug 5, 2003
  4. Blah Blah
    Replies:
    0
    Views:
    1,446
    Blah Blah
    Aug 13, 2003
  5. Jade
    Replies:
    3
    Views:
    944
    Roedy Green
    Aug 14, 2003
Loading...

Share This Page