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

Example in LinearGradient JavaDoc has syntax errors and does not match description

XMLWordPrintable

      Problems are:

      1. CycleMethod.No_CYCLE should be CycleMethod.NO_CYCLE
      2. The comment says the color goes from black to red but the gradient is defined from white to black.
      3. From the comment, the code is obviously supposed to show two rectangles next to each other for comparison, but it can't because:
         a) The first and second rectangles have the same variable name r (same for linear gradient lg)
         b) The location and size of the first rectangle is not set.
      4. The comments say (twice) that proportional is the default, but there is no default and proportional must be explicitly set.

      --------------------

      Excerpt from current javadoc http://download.oracle.com/javafx/2.0/api/javafx/scene/paint/LinearGradient.html =>

      The two filled rectangles in the example below will render the same. The one on the left uses proportional coordinates (the default) to specify the gradient's end points. The one on the right uses absolute coordinates. Both of them fill the specified rectangle with a horizontal gradient that varies from black to red

      // object bounding box relative (proportional:true)
      Stop[] stops = new Stop[] { new Stop(0, Color.WHITE), new Stop(1, Color.BLACK)};
      LinearGradient lg = new LinearGradient(0, 0, 1, 0, true, CycleMethod.No_CYCLE, stops);
      Rectangle r = new Rectangle();
      r.setFill(lg);

      // user space relative (proportional:false)
      LinearGradient lg = new LinearGradient(125, 0, 225, 0, false, CycleMethod.No_CYCLE, stops);
      Rectangle r = new Rectangle();
      r.setX(125.0);
      r.setY(0.0);
      r.setWidth(100);
      r.setHeight(100);
      r.setFill(lg);

      -------------------

      Proposed update to the code if it is to fit the description =>

          Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.RED)};

          // object bounding box relative (proportional:true)
          LinearGradient gradient1 = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
          Rectangle r1 = new Rectangle(0, 0, 100, 100);
          r1.setFill(gradient1);

          // user space relative (proportional:false)
          LinearGradient gradient2 = new LinearGradient(125, 0, 225, 0, false, CycleMethod.NO_CYCLE, stops);
          Rectangle r2 = new Rectangle(125, 0, 100, 100);
          r2.setFill(gradient2);

            flar Jim Graham
            josmithjfx John Smith (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: