I'm wondering about a line's exact width in JavaFX 2.1.
Creating a straight line starting at point (10;10) and ending at point (20;10) should have an expected width of 10px. When reading the line's width a value of 11px is returned. When starting the attached example and making a screenshot you'll see - at a higher zoom level - the line has a width of 12px and a height of 2px.
Using a LineBuilder
does not make any difference. I event tried to apply a different StrokeType
without success.
Creating a square with a side length of 10 returns the expected width of 10px.
- Why does
line.getBoundsInLocal().getWidth()
return different values from those I see in the rendered result (screenshot)? - Why is there a difference concerning 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);
}
}