What are the exact row sizes in JavaFX 2?

I am interested to know about the string width of a string in JavaFX 2.1.

Creating a straight line starting at (10; 10) and ending at (20; 10) should have an expected width of 10 pixels. When reading the line width, the value 11px is returned. When you start the attached example and take a screenshot, you will see that at a higher zoom level, the line has a width of 12 pixels and a height of 2 pixels.

Use LineBuilderis irrelevant. I tried to apply another StrokeTypewithout success.

Creating a square with a side length of 10 returns the expected width of 10 pixels.

  • Why does it line.getBoundsInLocal().getWidth()return different values ​​from those that I see in the rendering (screenshot)?
  • Why is there a difference in width when creating lines and polygons?

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;

public class ShapeWidthDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group();

        int startX = 10;
        int startY = 10;
        int length = 10;

        Line line = new Line(startX, startY, startX + length, startY);
        System.out.println("line width: " + line.getBoundsInLocal().getWidth());
        //->line width: 11.0
        System.out.println("line height: " + line.getBoundsInLocal().getHeight());
        //->line height: 1.0
        root.getChildren().add(line);

        int startY2 = startY + 2;

        Polygon polygon = new Polygon(
            startX, startY2,
            startX + 10, startY2, 
            startX + 10, startY2 + 10, 
            startX, startY2 + 10);
        System.out.println("polygon width: " + polygon.getBoundsInLocal().getWidth());
        //->polygon width: 10.0
        System.out.println("polygon height: " + polygon.getBoundsInLocal().getHeight());
        //->polygon height: 10.0
        root.getChildren().add(polygon);

        stage.setScene(new Scene(root, 100, 100));
        stage.show();
    }

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

, , . .

StrokeType CENTERED, StrokeLineCap SQUARE ( ), , , .

1 . JavaFX , . , , , . , StrokeLineCap of SQUARE, , , . .

, , , , . , , , .

, . 10 . , 10 .

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;

public class LineBounds extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) throws Exception {
    double startX = 10;
    double startY = 10;
    double length = 10;

    Line lineSpanningPixelsSquareEnd = new Line(startX, startY, startX + length, startY);
    System.out.println("lineSpanningPixels (square) bounding box: " + lineSpanningPixelsSquareEnd.getBoundsInLocal());

    startX = 10;
    startY = 20;
    length = 10;

    Line lineSpanningPixelsButtEnd = new Line(startX, startY, startX + length, startY);
    lineSpanningPixelsButtEnd.setStrokeLineCap(StrokeLineCap.BUTT);
    System.out.println("lineSpanningPixels (butt) bounding box:   " + lineSpanningPixelsButtEnd.getBoundsInLocal());

    startX = 10;
    startY = 29.5;
    length = 10;

    Line lineOnPixelsSquareEnd = new Line(startX, startY, startX + length, startY);
    System.out.println("lineOnPixels (square) bounding box:       " + lineOnPixelsSquareEnd.getBoundsInLocal());

    startX = 10;
    startY = 39.5;
    length = 10;

    Line lineOnPixelsButtEnd = new Line(startX, startY, startX + length, startY);
    lineOnPixelsButtEnd.setStrokeLineCap(StrokeLineCap.BUTT);
    System.out.println("lineOnPixels (butt) bounding box:         " + lineOnPixelsButtEnd.getBoundsInLocal());

    stage.setScene(
      new Scene(
        new Group(
          lineSpanningPixelsSquareEnd, lineSpanningPixelsButtEnd, lineOnPixelsSquareEnd, lineOnPixelsButtEnd
        ), 100, 100
      )
    );
    stage.show();
  }
}

-:

lineSpanningPixels (square) bounding box: BoundingBox [minX:9.5, minY:9.5, minZ:0.0, width:11.0, height:1.0, depth:0.0, maxX:20.5, maxY:10.5, maxZ:0.0]
lineSpanningPixels (butt) bounding box:   BoundingBox [minX:10.0, minY:19.5, minZ:0.0, width:10.0, height:1.0, depth:0.0, maxX:20.0, maxY:20.5, maxZ:0.0]
lineOnPixels (square) bounding box:       BoundingBox [minX:9.5, minY:29.0, minZ:0.0, width:11.0, height:1.0, depth:0.0, maxX:20.5, maxY:30.0, maxZ:0.0]
lineOnPixels (butt) bounding box:         BoundingBox [minX:10.0, minY:39.0, minZ:0.0, width:10.0, height:1.0, depth:0.0, maxX:20.0, maxY:40.0, maxZ:0.0]

line bounds sample program output

+1

1) StrokeLineCap.
, ""

line.setStrokeType(StrokeType.CENTERED);
line.setStrokeLineCap(StrokeLineCap.BUTT);
... that the line has a width of 12px and a height of 2px.

, StrokeType.OUTSIDE NOT StrokeLineCap.BUTT. 2px . ..

2) - NULL. polygon.setStroke(Color.RED);, .

+2

All Articles