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

Windows printers don't print RenderableImages (and probably not RenderedImages)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • client-libs
    • 2d
    • beta
    • x86
    • windows_nt



      Name: mc57594 Date: 11/23/99


      C:\javanew>java -version
      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

      //
      // This program demonstrates at least one bug in jdk 1.3beta printer
      // support.
      //
      // The program creates a RenderableImage (an X 300 points by 300
      // points displayed at coordinates 100, 100). This image is displayed
      // in a Frame on the screen, it is also printed to the default printer
      // using exactly the same drawing code (in
      // PrintTest$TestFrame.doPrint(Graphics))
      //
      // The problem I have is that the printer produces a blank piece of
      // paper, instead of one with an image of the X.
      //
      // This is my version info:
      // C:\javanew>java -version
      // java version "1.3beta"
      // Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      // Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)
      //
      // I am running on Windows NT 4.0sp5 with 128MB memory the printer is
      // a HPLaserJet4L running on a different NT 4.0 server and is accessed
      // over the LAN.
      //
      // Compile this program and then run it with no parameters.
      //
      //
      import java.awt.*;
      import java.awt.event.*;
      import java.awt.image.*;
      import java.awt.image.renderable.*;
      import java.awt.print.*;
      import java.awt.geom.*;
      import java.util.Vector;

      public class PrintTest implements RenderableImage
      {
         private int imageWidth;
         private int imageHeight;
         
         public PrintTest(int w, int h)
         {
            this.imageWidth = w;
            this.imageHeight = h;
         }
         
         static class PrintTestRendered implements RenderedImage
         {
            private int x;
            private int y;
            private int w;
            private int h;
            private ColorModel cm;
            
            private byte imageBuffer[];

            public PrintTestRendered(int x, int y, int w, int h)
            {
               this.x = x;
               this.y = y;
               this.w = w;
               this.h = h;
               this.cm = cm;

               imageBuffer = new byte[w * h];

               for(int i = 0; i < w; i++) {
                  if(i < h) {
                     imageBuffer[i * w + i] = 1;
                  }
               }
               for(int i = h - 1; i >= 0; i--) {
                  int j = h - 1 - i;
                  if(j < w) {
                     imageBuffer[i * w + j] = 1;
                  }
               }
               byte c[] = new byte[2];
               c[0] = (byte)0xff;
               c[1] = (byte)0;
               
               cm = new IndexColorModel(8, 2, c, c, c);
            }

            public Vector getSources() {return null;}
            public Object getProperty(String name) {return Image.UndefinedProperty;}
            public String[] getPropertyNames() {return new String[0];}
            
            public ColorModel getColorModel()
            {
               return cm;
            }
         
            public SampleModel getSampleModel()
            {
               return getData().getSampleModel();
            }
            
            public int getWidth() {return w;}
            public int getHeight() {return h;}
            public int getMinX() {return x;}
            public int getMinY() {return y;}
            public int getNumXTiles() {return 1;}
            public int getNumYTiles() {return 1;}
            public int getMinTileX() {return 0;}
            public int getMinTileY() {return 0;}
            public int getTileWidth() {return getWidth();}
            public int getTileHeight() {return getHeight();}
            public int getTileGridXOffset() {return 0;}
            public int getTileGridYOffset() {return 0;}
            public Raster getTile(int tileX,int tileY) {return getData();}
            
            public Raster getData()
            {
               Rectangle r= new Rectangle(x, y, w, h);
               return getData(r);
            }
         
            public Raster getData(Rectangle rect)
            {
               int masks[] = {0xff};
               SinglePixelPackedSampleModel sm =
                  new SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE,
                                                   rect.width,
                                                   rect.height,
                                                   w, masks);
                  
               DataBufferByte dbb = new DataBufferByte(imageBuffer,
                                                       (rect.height - 1 )* w +
      rect.width,
                                                       (-y + rect.y) * w - x +
      rect.x);
                  
               return Raster.createRaster(sm, dbb, new Point(rect.x, rect.y));
            }
         
            public WritableRaster copyData(WritableRaster raster)
            {
               Raster src;
               if(null == raster) {
                  src = getData();
                  raster = Raster.createWritableRaster(src.getSampleModel(), null);
               }
               else {
                  Rectangle r= new Rectangle(raster.getMinX(), raster.getMinX(),
                                             raster.getWidth(), raster.getHeight());
                  src = getData(r);
               }
               raster.setRect(src);
               return raster;
            }
         }
         

         public Vector getSources() {return new Vector(0);}
         public Object getProperty(String name) {return Image.UndefinedProperty;}
         public String[] getPropertyNames() {return new String[0];}
         public boolean isDynamic() {return false;}
         public float getWidth() {return 1.0f;}
         public float getHeight() {return 1.0f;}
         public float getMinX() {return 0.0f;}
         public float getMinY() {return 0.0f;}
         public RenderedImage createDefaultRendering()
         {
            AffineTransform at = new AffineTransform();
            RenderContext rc = new RenderContext(at);
            return createRendering(rc);
         }
         public RenderedImage createRendering(RenderContext renderContext)
         {
            AffineTransform xform = renderContext.getTransform();
            int sw = imageWidth;
            int sh = imageHeight;
            
            Point s[] = new Point[4];
            s[0] = new Point(0,0);
            s[1] = new Point(sw, 0);
            s[2] = new Point(sw, sh);
            s[3] = new Point(0, sh);
            Point2D d[] = new Point2D[4];
            xform.transform(s, 0, d, 0, 4);
            
            Rectangle2D.Double r = new Rectangle2D.Double(d[0].getX(), d[0].getY(),
      0, 0);
            r.add(d[1]);
            r.add(d[2]);
            r.add(d[3]);
            
            int dw = (int)Math.ceil(r.width);
            int dh = (int)Math.ceil(r.height);

            return new PrintTestRendered((int)r.x, (int)r.y, dw, dh);
         }
         public RenderedImage createScaledRendering(int w, int h, RenderingHints
      hints)
         {
            AffineTransform at = AffineTransform.getScaleInstance(w, h);
            RenderContext rc = new RenderContext(at, hints);
            return createRendering(rc);
         }

         public static class TestFrame extends Frame
                                               implements Printable
         {
            private void doPaint(Graphics g)
            {
               Graphics2D g2 = (Graphics2D)g;
               RenderableImage i = new PrintTest(300, 300);
               AffineTransform x = AffineTransform.getTranslateInstance(100, 100);
               g2.drawRenderableImage(i, x);
            }

            public void paint(Graphics g)
            {
               doPaint(g);
            }
            
            public int print(Graphics g, PageFormat f, int pageNum)
            {
               if(pageNum != 0) {
                  return Printable.NO_SUCH_PAGE;
               }
               doPaint(g);
               return Printable.PAGE_EXISTS;
            }
         }

         public static void main(String args[])
         {
            TestFrame f = new TestFrame();
            f.addWindowListener(new WindowListener(){
               public void windowOpened(WindowEvent e) {return;}
               public void windowClosing(WindowEvent e) {
                  e.getWindow().dispose();
                  System.exit(0);
               }
               public void windowClosed(WindowEvent e) {return;}
               public void windowIconified(WindowEvent e) {return;}
               public void windowDeiconified(WindowEvent e) {return;}
               public void windowActivated(WindowEvent e) {return;}
               public void windowDeactivated(WindowEvent e) {return;}
               
            });
            
            f.setSize(500, 500);
            f.show();
            try {
               PrinterJob pj = PrinterJob.getPrinterJob();
               pj.setPrintable(f);
               pj.print();
            }
            catch(Exception ex) {
               ex.printStackTrace(System.err);
            }
         }
      }
      (Review ID: 97415)
      ======================================================================

            prr Philip Race
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: