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

[Canvas] Canvas can draw outside its limits when width/height is bound

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P5
    • Resolution: Fixed
    • Affects Version/s: 8u5
    • Fix Version/s: 8u20
    • Component/s: javafx
    • Labels:
    • Environment:

      Windows 7, Java 8u5

      Description

      The Canvas class can draw outside its bounds if you issue a draw command, then update its bounds afterwards via a binding to width and height. Example that draws a visible red line on Windows 7 with 8u5:

      =====
      import javafx.application.Application;
      import javafx.beans.property.SimpleDoubleProperty;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.canvas.Canvas;
      import javafx.scene.canvas.GraphicsContext;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;

      public class TestCanvas extends Application
      {

          @Override
          public void start(Stage primaryStage) throws Exception {
              Canvas c = new Canvas(0, 0);
              SimpleDoubleProperty width = new SimpleDoubleProperty(0);
              SimpleDoubleProperty height= new SimpleDoubleProperty(0);
              c.widthProperty().bind(width);
              c.heightProperty().bind(height);
              
              GraphicsContext gc = c.getGraphicsContext2D();
              
              gc.setLineWidth(0.75);
              gc.setStroke(Color.RED);
              gc.strokeLine(0, 10, 40, 10);
              
              
              primaryStage.setScene(new Scene(new Group(c)));
              primaryStage.show();
              width.set(50);
              height.set(40);
          }

      }
      =====

      It draws a red line on a canvas of size 0x0, then resizes the canvas to reveal a red line. I think it must be to do with the binding aspect -- if I use setWidth and setHeight directly at the end of the method instead of the width binding as above, I don't see the same problem.

        Attachments

          Activity

            People

            Assignee:
            flar Jim Graham
            Reporter:
            nbrownjfx Neil Brown (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: