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

validatePage returns wrong page dimension for custom paper size

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • client-libs
    • None
    • 2d
    • beta
    • x86
    • windows_nt

      The following source code displays a 0 width & height of paper size after calling validatePage for custom paper size.

      ---------------------------------------

      import java.awt.*;
      import java.awt.print.*;
      import java.awt.geom.*;
      import javax.swing.*;

      public class PlotterTester implements Pageable, Printable{

        private static double PIXELS_PER_INCH = 72.0;
        private static double WIDTH = 17.0; //width of paper in inches
        private static double LENGTH = 24.0; //length of paper in inches
        private static boolean VALIDATE = true;

        private PrinterJob printerJob;
        private PageFormat pageFormat;

        PlotterTester(){
          printerJob = PrinterJob.getPrinterJob();
          createPageFormat();
        }

        private void createPageFormat(){
          pageFormat = new PageFormat();
          Paper p = new Paper();
          double width = WIDTH*PIXELS_PER_INCH;
          double height = LENGTH*PIXELS_PER_INCH;
          double ix = PIXELS_PER_INCH;
          double iy = PIXELS_PER_INCH;
          double iwidth = width - 2.0*PIXELS_PER_INCH;
          double iheight = height - 2.0*PIXELS_PER_INCH;
          p.setSize(width, height);
          p.setImageableArea(ix, iy, iwidth, iheight);
          pageFormat.setPaper(p);
        }

        public Printable getPrintable(int index){
          return this;
        }

        public PageFormat getPageFormat(int index){
          return pageFormat;
        }

        public int getNumberOfPages(){
          return 1;
        }

        private void printPaperSize(PageFormat pf){
          Paper p = pf.getPaper();
          System.out.println("paper size = ("+p.getWidth()+", "+p.getHeight()+")");
        }

        private void printRectangle(Rectangle r){
          System.out.println("("+r.x+", "+r.y+", "+r.width+", "+r.height+")");
        }

        public void print(){
          if(printerJob.printDialog()){
            System.out.println("start printing");
            try{
              printPaperSize(pageFormat);
              if(VALIDATE){
                  this.pageFormat = printerJob.validatePage(this.pageFormat);
              }
              printPaperSize(pageFormat);
              printerJob.setPageable(this);
              printerJob.print();
            }catch(Exception e){e.printStackTrace();}
          }
          System.out.println("done printing");
        }

        public int print(Graphics g, PageFormat pf, int pageIndex){
          printRectangle(g.getClipBounds());
          if(pageIndex == 0){
            Graphics2D g2 = (Graphics2D)g;
            Rectangle2D r = new Rectangle2D.Double(PIXELS_PER_INCH, PIXELS_PER_INCH, P
      IXELS_PER_INCH, PIXELS_PER_INCH);
            g2.setStroke(new BasicStroke(1.0f));
            g2.draw(r);
            return PAGE_EXISTS;
          }else{
            return NO_SUCH_PAGE;
          }
        }

        public static void main(String[] args){
          System.out.println(System.getProperty("java.version"));
          PlotterTester pt = new PlotterTester();
          pt.print();
          try{
            System.in.read();
          }catch(Exception e){}
          System.exit(0);
        }

      }
      -------------------------------------------

            jgodinez Jennifer Godinez (Inactive)
            jgodinez Jennifer Godinez (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: