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

LinearGradient bug

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      macOs 10.14.6 (18G2022)

      A DESCRIPTION OF THE PROBLEM :
      his program demonstrates how a 3D object with a transparent gradient texture applied to it loses the gradient when the scene is regenerated

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use attached program
       * Steps to demonstrate bug:
       * 1. Set DISABLE_WORKAROUND to true
       * 1. Run code and notice transparent gradient getting lighter from left to right
       * 2. Select "Rebuild" from the menu and notice how gradient vanishes, but transparency remains
       *
       * Steps to demonstrate work around:
       * 1. Set DISABLE_WORKAROUND to false
       * 1. Run code and notice transparent gradient getting lighter from left to right
       * 2. Select "Rebuild" from the menu and notice how gradient remains


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Gradient should not go away.
      ACTUAL -
      Gradient stops working after scene is regenerated.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.event.EventType;
      import javafx.scene.*;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.image.WritableImage;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.StackPane;
      import javafx.scene.paint.*;
      import javafx.scene.shape.Box;
      import javafx.stage.Stage;
      import javafx.stage.StageStyle;

      /*
       * This program demonstrates how a 3D object with a transparent gradient texture applied to it
       * loses the gradient when the scene is regenerated.
       *
       * Steps to demonstrate bug:
       * 1. Set DISABLE_WORKAROUND to true
       * 1. Run code and notice transparent gradient getting lighter from left to right
       * 2. Select "Rebuild" from the menu and notice how gradient vanishes, but transparency remains
       *
       * Steps to demonstrate work around:
       * 1. Set DISABLE_WORKAROUND to false
       * 1. Run code and notice transparent gradient getting lighter from left to right
       * 2. Select "Rebuild" from the menu and notice how gradient remains
       */

      public class GradientBug extends Application {
        private static final boolean DISABLE_WORKAROUND = true;
        private WritableImage texture;

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

        // Build Camera
        private PerspectiveCamera getCamera () {
          PerspectiveCamera camera = new PerspectiveCamera(true);
          Group cameraXGroup = new Group();
          cameraXGroup.getChildren().add(camera);
          camera.setTranslateZ(-100);
          camera.setNearClip(0.1);
          camera.setFarClip(10000.0);
          camera.setFieldOfView(40);
          return camera;
        }

        private SubScene get3dScene () {
          Group root = new Group();
          Color color = Color.GOLD;
          Color trans = new Color(color.getRed(), color.getGreen(), color.getBlue(), .7f);
          // Create background object to show transparency
          Box back = new Box(600, 200, 10);
          back.setTranslateZ(1100);
          if (DISABLE_WORKAROUND || texture == null) {
            // Build Gradient Texture with Alpha Channel
            Stop[] stop = {new Stop(0, Color.BLACK), new Stop(1, color)};
            LinearGradient gradient = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stop);
            Scene aux = new Scene(new StackPane(), 512, 4, gradient);
            texture = aux.snapshot(null);
          }
          // Build foreground object using transparent gradient texture
          Box box = new Box(400, 400, 10);
          PhongMaterial material = new PhongMaterial(trans);
          material.setDiffuseMap(texture);
          box.setMaterial(material);
          box.setTranslateZ(1000);
          root.getChildren().addAll(back, box);
          SubScene subScene = new SubScene(root, 600, 600, true, SceneAntialiasing.BALANCED);
          subScene.setFill(new Color(0.0, 0.0, 0.0, 1.0));
          subScene.setCamera(getCamera());
          return subScene;
        }

        @Override
        public void start (Stage stage) {
          stage.setTitle("Gradient Texture Bug");
          BorderPane borderPane = new BorderPane();
          Scene scene = new Scene(borderPane);
          borderPane.setCenter(new Group(get3dScene()));
          stage.setScene(scene);
          MenuBar menuBar = new MenuBar();
          borderPane.setTop(menuBar);
          Menu menu = new Menu("Action");
          menuBar.getMenus().add(menu);
          MenuItem item = new MenuItem("Rebuild");
          menu.getItems().add(item);
          item.addEventHandler(EventType.ROOT, ev -> {
            try {
              Group grp = (Group) borderPane.getCenter();
              grp.getChildren().clear();
              grp.getChildren().add(get3dScene());
            } catch (Exception ex) {
              ex.printStackTrace();
            }
          });
          stage.initStyle(StageStyle.TRANSPARENT);
          stage.show();
          stage.setOnCloseRequest(e -> {
            Platform.exit();
            System.exit(0);
          });
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      See notes in code and in "Steps to Reproduce"

      FREQUENCY : always


            lkostyra Lukasz Kostyra
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: