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

Displayed content freeze soon after Canvas.snapshot(...).

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      jdk 1.8.0_144 x64


      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 64 SP1

      A DESCRIPTION OF THE PROBLEM :
      Initially posted here:
      http://mail.openjdk.java.net/pipermail/openjfx-dev/2017-September/020809.html

      There is a (recoverable) freeze issue related to Canvas.snapshot(...),
      when either width or height (or both) used for Scene constructor
      is zero, even though it's not indicated as an invalid value.

      It's trivial to work around, by not using 0, but people might waste much time, as I did, to figure out what to do to avoid the freeze.

      With negative values, which are considered as uninitialized,
      the problem doesn't occur, so considering 0 as uninitialized
      as well might fix it, even though it might not address some
      deeper issue making such a freeze possible.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the test case code, and click multiple times on the canvas.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Content is copy-shift-pasted after each click.
      ACTUAL -
      Content freezes after first click, and some events (click events, etc.) seem to no longer occur.


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.canvas.Canvas;
      import javafx.scene.canvas.GraphicsContext;
      import javafx.scene.image.WritableImage;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.layout.Pane;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;

      public class JfxSnapshotMain extends Application {
          private static final boolean WORKAROUND_1 = false;
          private static final boolean WORKAROUND_2 = false;
          public static void main(String[] args) {
              launch(args);
          }
          @Override
          public void start(Stage stage) {
              Pane root = new Pane();
              double initWidth = 0.1;
              double initHeight = (WORKAROUND_1 ? 0.1 : 0.0);
              Scene scene = new Scene(root, initWidth, initHeight);
              double width = 200.0;
              double height = 100.0;
              Canvas canvas = new Canvas(width, height);
              canvas.addEventHandler(
                      MouseEvent.MOUSE_PRESSED,
                      new EventHandler<MouseEvent>() {
                  @Override
                  public void handle(MouseEvent event) {
                      System.out.println("handle(" + event + ")");
                      takeSnapshotAndDrawShifted(canvas);
                      if (WORKAROUND_2) { stage.hide(); stage.show(); }
                  }
              });
              root.getChildren().add(canvas);
              stage.setWidth(width);
              stage.setHeight(height);
              stage.setScene(scene);
              drawStuffs(canvas);
              stage.show();
          }
          private static void drawStuffs(Canvas canvas) {
              GraphicsContext gc = canvas.getGraphicsContext2D();
              gc.setStroke(Color.BLACK);
              gc.strokeText("Click This", 10.0, 10.0);
          }
          private static void takeSnapshotAndDrawShifted(Canvas canvas) {
              WritableImage img = canvas.snapshot(null, null);
              canvas.getGraphicsContext2D().drawImage(img, 10.0, 10.0);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Programmatic workaround other than not using 0: doing stage.hide() and stage.show() after the snapshot.
      Resizing the stage manually (but not programmatically) also works, but not iconifying/deiconifying it.


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: