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

Printing of scaled text is wrong / dissapearing

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/15/99


      /* When you start the following program you will see on a panel 5 equal boxes with text.
      But the result of the simultanious printing differs from that !! The distances of the
      characters are differing and in the bottom box characters are missing.

      The program paints a String in different fontsizes and scalings.

      It seems that
      printing of fonts can't handle fractional sizes of fonts - perhaps they are
      rounded in the rendering process.*/

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

      public class PrintFont extends JPanel implements Printable{

        private static final int RES = 72;

        /**
         * Print implementation of interface Printable.
         */
        public int print(Graphics g, PageFormat pf, int index){
          if (index != 0){
            return NO_SUCH_PAGE;
          } else {
            printScales(g);

            return PAGE_EXISTS;
          }
        }

        /**
         * Paint overriding of class JPanel.
         */
        public void paint(Graphics g){
          g.setColor(Color.white);
          g.fillRect(0, 0, getWidth(), getHeight());
          printScales(g);
        }

        /**
         * Print several scales.
         */
        private void printScales(Graphics g){
          Graphics2D g2D = (Graphics2D) g;
          g2D.setColor(Color.black);
          g2D.setStroke(new BasicStroke(0));
          Font font = new Font("Arial", Font.PLAIN, 1);

          float scale;
          float x;
          float y;

          scale = 1.0f;
          x = 3.0f;
          y = 3.0f;
          printScale(g2D, font, scale, x, y);

          scale = 2.0f;
          x = 3.0f;
          y = 3.5f;
          printScale(g2D, font, scale, x, y);

          scale = 4.0f;
          x = 3.0f;
          y = 4.0f;
          printScale(g2D, font, scale, x, y);

          scale = 8.0f;
          x = 3.0f;
          y = 4.5f;
          printScale(g2D, font, scale, x, y);

          scale = 16.0f;
          x = 3.0f;
          y = 5.0f;
          printScale(g2D, font, scale, x, y);

          scale = 32.0f;
          x = 3.0f;
          y = 5.5f;
          printScale(g2D, font, scale, x, y);
        }

        /**
         * The graphics is scaled and the font and the positions
         * are reduced in respect to the scaling, so that all
         * printing should be the same.
         *
         * @param g2D graphics2D to paint on
         * @param font font to paint
         * @param scale scale for the painting
         * @param x x position
         * @param y y position
         */
        private void printScale(Graphics2D g2D, Font font, float scale, float x, float y){
          g2D.scale (scale, scale);

          g2D.setFont (font.deriveFont(10.0f / scale));
          g2D.drawString("Scale " + scale,
                         x * RES / scale, y * RES / scale);
          g2D.draw (new Rectangle2D.Float(x * RES / scale, (y * RES - 10.0f) / scale,
                                               RES/scale, 10.0f/scale));
          g2D.scale(1/scale, 1/scale);
        }

        /**
         * Starter.
         */
        public static void main (String[] args){

          JFrame frame = new JFrame("PrintFont");

          frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
              System.exit(0);
            }
          });

          frame.getContentPane().add(new PrintFont());
          frame.pack();
          frame.setVisible(true);


          try{
            PrinterJob job = PrinterJob.getPrinterJob();

            job.setPrintable(new PrintFont());

            job.print();
          } catch (PrinterException ex){
          }
        }
      }
      (Review ID: 96900)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: