Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4234257

ClassCastException when printing Applet

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.1
    • client-libs
    • x86
    • windows_nt



      Name: krT82822 Date: 04/29/99


      The code for an applet is given at the end of this description.
      This applet has a main with a frame and applet stub so that it
      can run as an application. I did this to avoid having to sign
      the applet. If I run the applet as an application (using javac)
      and press the Print button, I get the following exception.
      NOTE: I ran it with
         -Djava.compiler=NONE
      so that I would get line numbers.
      This was compiled and executed using JDK1.2.1.
      The full version is JDK-1.2.1-A



      Unable to print: java.lang.ClassCastException: sun.java2d.PeekGraphics
      java.lang.ClassCastException: sun.java2d.PeekGraphics
      at sun.awt.windows.WComponentPeer.print(WComponentPeer.java:92)
      at java.awt.Component.printAll(Component.java:1966)
      at java.awt.Container.printOneComponent(Container.java:914)
      at java.awt.Container.printComponents(Container.java:904)
      at PrintMe.print(PrintMe.java:150)
      at sun.java2d.RasterPrinterJob.printPage(RasterPrinterJob.java:486)
      at sun.java2d.RasterPrinterJob.print(RasterPrinterJob.java:235)
      at PrintMe.print(PrintMe.java:119)
      at PrintMe.button1_ActionPerformed(PrintMe.java:102)
      at PrintMe$SymAction.actionPerformed(PrintMe.java:94)
      at java.awt.Button.processActionEvent(Button.java:308)
      at java.awt.Button.processEvent(Button.java:281)
      at java.awt.Component.dispatchEventImpl(Component.java:2376)
      at java.awt.Component.dispatchEvent(Component.java:2289)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:258)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:68)




      /*
      A basic extension of the java.applet.Applet class
       */

      import java.awt.*;
      import java.applet.*;
      import java.util.Enumeration;
      import java.net.URL;

      public class PrintMe extends Applet implements java.awt.print.Printable
      {
      /**
      * When printing, the Graphics must be translated to allow for the
      * top and left margins. If no translation is done, then part of
      * the image will be clipped off because most printers can not print
      * to the edge of the paper.
      */
      public static final float PRINT_TOP_MARGIN_IN_INCHES = 0.75F;
      public static final float PRINT_LEFT_MARGIN_IN_INCHES = 0.75F;


      /**
      * This constructor is used when running standalone.
      * It sets the AppletContext to an instance of the DummyAppletContext
      * which is an inner class defined in this file!
      */
      public PrintMe(String dummyArgument)
      {
      setStub(new DummyAppletStub());
      }

      public void init()
      {
      // This code is automatically generated by Visual Cafe when you add
      // components to the visual environment. It instantiates and initializes
      // the components. To modify the code, only use code syntax that matches
      // what Visual Cafe can generate, or Visual Cafe may be unable to back
      // parse your Java file into its visual environment.
      //{{INIT_CONTROLS
      setLayout(new GridLayout(0,1,20,20));
      setSize(426,298);
      list1 = new java.awt.List(0);
      list1.add("yellow");
      list1.add("red");
      list1.add("green");
      list1.add("blue");
      list1.setBounds(0,0,426,33);
      add(list1);
      textField1 = new java.awt.TextField();
      textField1.setText("I am a text field");
      textField1.setBounds(0,53,426,33);
      add(textField1);
      choice1 = new java.awt.Choice();
      choice1.addItem("yellow");
      choice1.addItem("red");
      choice1.addItem("green");
      choice1.addItem("blue");
      choice1.setBounds(0,106,426,33);
      add(choice1);
      label1 = new java.awt.Label("I am a label.");
      label1.setBounds(0,159,426,33);
      add(label1);
      checkbox1 = new java.awt.Checkbox("Check Me");
      checkbox1.setBounds(0,212,426,33);
      add(checkbox1);
      button1 = new java.awt.Button();
      button1.setLabel("Print");
      button1.setBounds(0,265,426,33);
      button1.setBackground(new Color(12632256));
      add(button1);
      //}}

      //{{REGISTER_LISTENERS
      SymAction lSymAction = new SymAction();
      button1.addActionListener(lSymAction);
      //}}
      }

      //{{DECLARE_CONTROLS
      java.awt.List list1;
      java.awt.TextField textField1;
      java.awt.Choice choice1;
      java.awt.Label label1;
      java.awt.Checkbox checkbox1;
      java.awt.Button button1;
      //}}

      class SymAction implements java.awt.event.ActionListener
      {
      public void actionPerformed(java.awt.event.ActionEvent event)
      {
      Object object = event.getSource();
      if (object == button1)
      button1_ActionPerformed(event);
      }
      }

      void button1_ActionPerformed(java.awt.event.ActionEvent event)
      {
      try
      {
      print();
      }
      catch (Exception e)
      {
      System.out.println("Unable to print: " + e);
      e.printStackTrace(System.out);
      }
      }




          void print() throws java.awt.print.PrinterException
            {
            java.awt.print.PrinterJob printerJob = java.awt.print.PrinterJob.getPrinterJob();
            
            printerJob.setPrintable(this);
            printerJob.print();
            }
         
         
         /**
          * This method is called when there is an applet
          * to print which does not use the grid.
          */
         public int print
         (
         Graphics graphics,
         java.awt.print.PageFormat pageFormat,
         int pageIndex
         )
            {
            if (pageIndex > 0)
               {
               return java.awt.print.Printable.NO_SUCH_PAGE;
               }
               
            int myPR = 72;
            float printTopMargin = myPR * PRINT_TOP_MARGIN_IN_INCHES;
            float printLeftMargin = myPR * PRINT_LEFT_MARGIN_IN_INCHES;
            Dimension usableSize = new Dimension(
                                           (int) Math.round(pageFormat.getWidth() - 2*printLeftMargin),
                                           (int) Math.round(pageFormat.getHeight() - 2*printTopMargin));
            
            graphics.translate(
                      Math.round(printLeftMargin),
                      Math.round(printTopMargin));

            this.printComponents(graphics);
            
            return java.awt.print.Printable.PAGE_EXISTS;
            }

              
         /**
          * This class is used when running standalone.
          * It will supply methods that are called via an
          * AppletContext (such as showDocument) but most of these
          * methods will not do anything!
          */
         public class DummyAppletStub implements AppletStub, AppletContext
            {
            
            //
            // AppletStub methods
            //
            
            public String getParameter(String name)
               {
      return null;
      }
            
            public boolean isActive() {return true;}
            public URL getDocumentBase() {return null;}
            public AppletContext getAppletContext() {return this; }
            public void appletResize(int width, int height) { }
            
            public URL getCodeBase()
               {
               String currentDirectory = System.getProperty("user.dir", "c:");
               
               try
                  {
                  return new URL("file:" + currentDirectory);
                  }
               catch (Exception e)
                  {
                  return null;
                  }
               }
            
            
            //
            // AppletContext methods
            //
            
            public AudioClip getAudioClip(URL url) {return null;}
            public Image getImage(URL url) {return null;}
            public Applet getApplet(String name) {return null;}
            public Enumeration getApplets() {return null;}
            public void showDocument(URL url) { }
            public void showDocument(URL url, String target) { }
            public void showStatus(String status) { }
            
            }
            
            
            
      public static void main(String args[])
      {
      PrintMe a = new PrintMe("select special constructor");
      Frame f = new Frame("I am holding an applet!");

      f.setSize(300, 300);
      f.add("Center", a);

      a.init();
      f.show();
      a.start();
      }
         }
      (Review ID: 57331)
      ======================================================================

            dmendenhsunw David Mendenhall (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: