3 Replies Latest reply: Oct 15, 2010 2:25 PM by jduprez RSS

    Point2D.Float cast to Float

      I noticed in the swingx BusyPainter code (fragments below) that Point2D.Float is being assigned/cast to Float, how does this work?
      How can they do this Float center = new Float(sp.x, sp.y) ?
      In paintRotatedCenteredShapeAtPoint p which was defined as Point2D.Float is being passed as a Float.

      Can someone please explain?
              Point2D.Float sp = new Point2D.Float();
              Float center = new Float(sp.x, sp.y);
              center.x = ((float) width) / 2;
              center.y = ((float) height) / 2;
              // draw the stuff
              int i = 0;
              g.translate(center.x, center.y);
              for (Point2D.Float p : pList) {
                  drawAt(g, i++, p, center);
          private void drawAt(Graphics2D g, int i, Point2D.Float p, Float c) {
              paintRotatedCenteredShapeAtPoint(p, c, g);
          private void paintRotatedCenteredShapeAtPoint(Float p, Float c, Graphics2D g) {
              Shape s = getPointShape();
              double hh = s.getBounds().getHeight() / 2;
              double wh = s.getBounds().getWidth() / 2;
              double t, x, y;
              double a = c.y - p.y;
              double b = p.x - c.x;
        • 1. Re: Point2D.Float cast to Float
          <nonsense deleted />
          Edited by: jduprez on Oct 15, 2010 2:23 PM
          Look at the imports:
          import java.awt.geom.Point2D;
          import java.awt.geom.Point2D.Float;
          So they can use Float and Point2D.Float interchangeably, to mean java.awt.geom.Point2D.Float

          What I don't get is, how does either of that not conflict with java.lang.Float?

          Edited by: jduprez on Oct 15, 2010 2:25 PM
          • 2. Re: Point2D.Float cast to Float
            jduprez wrote:
            What I don't get is, how does either of that not conflict with java.lang.Float?
            From the JLS
            [url http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.1]Single-Type-Import Declaration
            A single-type-import declaration d in a compilation unit c of package p that imports a type named n shadows the declarations of:
            <li> any type named n imported by a type-import-on-demand declaration in c.

            [url http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.5]Automatic Imports
            Each compilation unit automatically imports all of the public type names declared in the predefined package java.lang, as if the declaration:
            <tt>import java.lang.*;</tt>
            appeared at the beginning of each compilation unit, immediately following any package statement.

            The java.lang automatic import constitutes a type-import-on-demand declaration.

            • 3. Re: Point2D.Float cast to Float
              Accurate. Concise. Neat. Thanks Darryl.