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

drawString on a translated Image fails when y<-17

    Details

    • Subcomponent:
      2d
    • CPU:
      x86
    • OS:
      windows_nt

      Description



      Name: bk70084 Date: 09/17/98


      Clipping is incorrect on translated Images when using drawString.
      In the following test case the Graphics object of the Image is used to draw
      several strings into the Image. If the String is drawn to a y-coordinate of
      less than -17 (which equals the getHeight() of the font), then the
      string is not drawn.

      Run the following test case, enter a value of -18, and hit return. You will
      see line 0 disappear in the Image drawn in the new Window that pops up.
      Try -34 and -35, and Line 1 will also be seen to disappear.


      //-----------------Offscreen.java------------------------
      import java.awt.*;
      import java.awt.event.*;

      public class Offscreen
      {
          TextField tf;

          public static void main(String[] args) {
              new Offscreen();
          }

          public Offscreen() {
              Frame f = new Frame();
              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
              Label l = new Label("Offset:");
              tf = new TextField("-17");
              tf.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      try {
                          int new_base = Integer.valueOf(tf.getText()).intValue();
                          createWindow(new_base);
                      } catch (Exception exc) {
                          System.out.println(
                              "Warning: invalid integer");
                      }
                  }
              });
              f.setLayout(new GridLayout(1,2));
              f.add(l);
              f.add(tf);
              f.pack();
              f.show();
              createWindow();
          }

          public void createWindow() {
              createWindow(-17);
          }

          public void createWindow(int base) {
              Frame f = new Frame("Offset of " + base);
              f.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      ((Frame)e.getSource()).setVisible(false);
                  }
              });
              Canvas c = new ImgCanvas(base);
              c.setSize(400, 300);
              f.add(c);
              f.pack();
              f.setLocation(150, 10);
              f.show();
          }
      }

      class ImgCanvas extends Canvas
      {
          int base = -17;
         
          public ImgCanvas() {
              super();
          }

          public ImgCanvas(int new_base) {
              super();
              base = new_base;
          }

          public int paintInfo(Graphics g, String l) {
              /*
               * Translate the Graphics object.
               * Fails with offset = 100,
               * works if offset = -100
               */
              int offset = 100;
              g.translate(0,offset);
              
              g.drawLine(0,0,600,0);

              Font f = g.getFont();
              FontMetrics fm = g.getFontMetrics();

                  // paint 4 lines
              int maxAscent = fm.getMaxAscent();
              int y = maxAscent;
              for (int k = 0; k < 4; k++) {
                  int yval = base + y - maxAscent;
                  System.out.println("Line " + k + " at (0," + yval + ")");
                  g.drawString(l+" line "+k, 0, yval);
                  y += fm.getHeight();
              }
              System.out.println();

              g.translate(0,-offset);
              return y;
          }

          public void paint(Graphics g)
          {
              // Print to canvas
              // int y = paintInfo(g, "Drawing on Canvas");

              Color oldColor = g.getColor();
              g.setColor(Color.green);
              g.fillRect(-100,-100,1000,1000);
              g.setColor(oldColor);
              // Print to image
              Image im = createImage(getSize().width, 600);
              paintInfo(im.getGraphics(), "Drawing on offscreen image");
              g.drawImage(im, 20, 20, this);
          }
      }

      (Review ID: 35853)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pcharltosunw Paul Charlton (Inactive)
                Reporter:
                bklocksunw Brian Klock (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: