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

Setting the font size to a larger value throws a IllegalArgumentException

XMLWordPrintable

    • 2d
    • tiger
    • x86
    • windows_2000

      In a simple Java Swing Application like below:

      /*
       * ShowUnicode.java
       *
       * Created on 2003??(:5????31????, ????????12:48
       */
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.event.*;
      /**
       *
       * @author wuliangqiao
       */
      public class ShowUnicode extends JFrame implements ActionListener, CaretListener
      {
          JTextField f;
          JLabel b;
          JLabel label;
          /** Creates a new instance of ShowUnicode */
          public ShowUnicode()
          {
      int i = 0;
              setTitle("Unicode");
              Container p = getContentPane();
              f = new JTextField();
              f.addCaretListener(this);
              f.addActionListener(this);
              b = new JLabel();
              b.setFont(new Font("Dialog", 2, 144));
              b.setHorizontalAlignment(0);
              label = new JLabel();
              label.setPreferredSize(new Dimension(0, 30));
              p.add(f, BorderLayout.NORTH);
              p.add(b);
              p.add(label, BorderLayout.SOUTH);
              setBounds(100, 100, 800, 600);
          }

          public void paint(Graphics g) {
           System.out.println("paint");
           super.paint(g);
          }

          public void update(Graphics g) {
           System.out.println("update");
           super.update(g);
          }
          
          protected void processWindowEvent(WindowEvent e)
          {
              if (e.getID() == e.WINDOW_CLOSING)
              {
                  System.exit(0);
              }
          }
          
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args)
          {
              try {
               new ShowUnicode().show();
              } catch (Exception e) {
               e.printStackTrace();
              }
          }
         
          
          /** Called when the caret position is updated.
           *
           * @param e the caret event
           */
          public void caretUpdate(CaretEvent e)
          {
              showChar(false);
          }
          
          /** Invoked when an action occurs.
           */
          public void actionPerformed(ActionEvent e)
          {
              showChar(true);
          }
          
          private void showChar(boolean first)
          {
              String s = f.getText();
              if (s.length() != 0)
              {
                  char c = 0;
                  if (!first)
                  {
                      try
                      {
                          c = (char)Integer.parseInt(f.getText(), 16);
                      }
                      catch (Exception ex)
                      {
                          first = true;
                      }
                  }
                  if (first)
                  {
                      c = s.charAt(0);
                  }
                  s = String.valueOf(c) + '(' + Integer.toHexString(c) + ')';
              }
              b.setText(s);
          }
      }
             
      b.setFont(new Font("Dialog", 2, 144));
      if the third argument's value is larger than 100, there will be an IllegalArgumentException thrown like below:

      java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.
      at java.awt.font.TextLayout.<init>(TextLayout.java:471)
      at sun.java2d.pipe.OutlineTextRenderer.drawString(OutlineTextRenderer.java:67)
      at sun.java2d.pipe.GlyphListPipe.drawString(GlyphListPipe.java:32)
      at sun.java2d.pipe.ValidatePipe.drawString(ValidatePipe.java:123)
      at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2588)
      at javax.swing.plaf.basic.BasicGraphicsUtils.drawStringUnderlineCharAt(BasicGraphicsUtils.java:234)
      at javax.swing.plaf.basic.BasicLabelUI.paintEnabledText(BasicLabelUI.java:81)
      at javax.swing.plaf.basic.BasicLabelUI.paint(BasicLabelUI.java:164)
      at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
      at javax.swing.JComponent.paintComponent(JComponent.java:541)
      at javax.swing.JComponent.paint(JComponent.java:808)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paint(JComponent.java:817)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paint(JComponent.java:817)
      at javax.swing.JLayeredPane.paint(JLayeredPane.java:552)
      at javax.swing.JComponent.paintChildren(JComponent.java:647)
      at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4778)
      at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4724)
      at javax.swing.JComponent.paint(JComponent.java:798)
      at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
      at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
      at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
      at java.awt.Container.paint(Container.java:1309)
      at ShowUnicode.paint(ShowUnicode.java:41)
      at sun.awt.RepaintArea.paint(RepaintArea.java:177)
      at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
      at java.awt.Component.dispatchEventImpl(Component.java:3658)
      at java.awt.Container.dispatchEventImpl(Container.java:1623)
      at java.awt.Window.dispatchEventImpl(Window.java:1585)
      at java.awt.Component.dispatchEvent(Component.java:3439)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
      at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)


      The sun.java2d.pipe.OutlineTextRender has a static final field called THRESHHOLD, and the its value is 100. This class also has a method called drawString(), and the prototype is like below:

      public void drawString(SunGraphics2D g2d, String str, float x, float y);

      In this method, a java.awt.font.TextLayout object is created using the contructor: public TextLayout(String string, Font font, FontRenderContext frc)

      This constructor will test whether the first argument "string" is null or empty. If it is, the IllegalArgumentException is thrown.

      During the initializtion phase of the frame, the paint() method is called. And subsequently the drawString method of an instance of class sun.java2d.SunGraphics2D will be called. The SunGraphics2D object has a member object, whose type is sun.java2d.pipe.TextType. TextType is an interface, and an object of class sun.java2d.pipe.SolidTextRenderer will be used, and its method drawString is called. SolidTextRenderer is a sub class of GlyphListPipe.
      And in the method drawString of GlyphListPipe, you can see the code below:

              if (info.pixelHeight > OutlineTextRenderer.THRESHHOLD) {
                  SurfaceData.outlineTextRenderer.drawString(sg2d, s, x, y);
                  return;
              }

      In this case, the variable s is empty, so the exception is raised.

            prr Philip Race
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: