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

Component.getSize() returns new Dimension

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.2.0
    • 1.1.1
    • client-libs
    • None
    • 1.2
    • sparc
    • solaris_2.5
    • Not verified

      If I have (or am) a java.awt.Component, the only way to find out the height, or width of the Component is to call getSize(). The problem is that the API says that returns a Dimension, which means that I'm doing an allocation. Wouldn't it be better to have getHeight() and getWidth() methods, so if I were only interested in one, I could get that without allocating?

      For example, here's the paint method from applets/SortDemo/1.0.2/SortItem.java, written by our own JAG (sometimes called the best programmer on the planet :-).

          /**
           * Paint the array of numbers as a list
           * of horizontal lines of varying lenghts.
           */
          public void paint(Graphics g) {
      int a[] = arr;
      int y = size().height - 1;

      // Erase old lines
      g.setColor(getBackground());
      for (int i = a.length; --i >= 0; y -= 2) {
      g.drawLine(arr[i], y, size().width, y);
      }

      // Draw new lines
      g.setColor(Color.black);
      y = size().height - 1;
      for (int i = a.length; --i >= 0; y -= 2) {
      g.drawLine(0, y, arr[i], y);
      }

      if (h1 >= 0) {
      g.setColor(Color.red);
      y = h1 * 2 + 1;
      g.drawLine(0, y, size().width, y);
      }
      if (h2 >= 0) {
      g.setColor(Color.blue);
      y = h2 * 2 + 1;
      g.drawLine(0, y, size().width, y);
      }
          }

      where he calls size() (the deprecated version of getSize()) 4 times outside of loops, and once for each line insde the first loop. Sure, he could have called it once at the top of the method and cached the height and width in local ints, but the current API encourages this kind of stupid allocation and collection of multi-value return objects.

      peter.kessler@Eng 1997-03-14

            ehawkessunw Eric Hawkes (Inactive)
            pbk Peter Kessler
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: