JavaFX 线段宽度
JavaFX Line segment width
我有一些代码可以响应按键,并相应地绘制点和线段。然而,线段的宽度似乎交替出现,即使我没有在代码中触及线描宽度。我想知道为什么会这样。
代码如下:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.shape.Line;
public class DrawLineSegments extends Application {
static double x = 0.0;
static double y = 0.0;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane p = new Pane();
Rectangle border = new Rectangle(0, 0, 300, 100);
border.setFill(Color.TRANSPARENT);
Rectangle initialPoint = new Rectangle(border.getWidth() / 2, border.getHeight() / 2, 2, 2);
initialPoint.setFill(Color.BLACK);
x = border.getWidth() / 2;
y = border.getHeight() / 2;
p.getChildren().addAll(border, initialPoint);
p.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP :
Line upLine = new Line(x + 1, y + 1, x + 1, y + 1 - 7.5);
y = y - 7.5;
upLine.setStroke(Color.BLUEVIOLET);
upLine.setFill(Color.SANDYBROWN);
p.getChildren().add(upLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
case DOWN :
Line downLine = new Line(x + 1, y + 1, x + 1, y + 1 + 7.5);
y = y + 7.5;
downLine.setStroke(Color.BLUEVIOLET);
downLine.setFill(Color.SANDYBROWN);
p.getChildren().add(downLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
case LEFT :
Line leftLine = new Line(x - 7.5, y + 1, x + 1, y + 1);
x = x - 7.5;
leftLine.setStroke(Color.BLUEVIOLET);
leftLine.setFill(Color.SANDYBROWN);
p.getChildren().add(leftLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
case RIGHT :
Line rightLine = new Line(x + 7.5, y + 1, x + 1, y + 1);
x = x + 7.5;
rightLine.setStroke(Color.BLUEVIOLET);
rightLine.setFill(Color.SANDYBROWN);
p.getChildren().add(rightLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
default:
break;
}
});
Scene scene = new Scene(p);
primaryStage.setScene(scene);
primaryStage.setTitle("Draw Line Segments");
primaryStage.show();
p.requestFocus();
}
}
一张图片或许能更好地解释我的问题:
您可能想阅读 Coordinate System of a Node
Coordinate System
The Node class defines a traditional computer graphics "local"
coordinate system in which the x axis increases to the right and the y
axis increases downwards. The concrete node classes for shapes provide
variables for defining the geometry and location of the shape within
this local coordinate space. For example, Rectangle provides x, y,
width, height variables while Circle provides centerX, centerY, and
radius.
At the device pixel level, integer coordinates map onto the corners
and cracks between the pixels and the centers of the pixels appear at
the midpoints between integer pixel locations. Because all coordinate
values are specified with floating point numbers, coordinates can
precisely point to these corners (when the floating point values have
exact integer values) or to any location on the pixel. For example, a
coordinate of (0.5, 0.5) would point to the center of the upper left
pixel on the Stage. Similarly, a rectangle at (0, 0) with dimensions
of 10 by 10 would span from the upper left corner of the upper left
pixel on the Stage to the lower right corner of the 10th pixel on the
10th scanline. The pixel center of the last pixel inside that
rectangle would be at the coordinates (9.5, 9.5).
关于您的问题:不要使用 7.5。而是使用 7 或 8。
我有一些代码可以响应按键,并相应地绘制点和线段。然而,线段的宽度似乎交替出现,即使我没有在代码中触及线描宽度。我想知道为什么会这样。
代码如下:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.shape.Line;
public class DrawLineSegments extends Application {
static double x = 0.0;
static double y = 0.0;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane p = new Pane();
Rectangle border = new Rectangle(0, 0, 300, 100);
border.setFill(Color.TRANSPARENT);
Rectangle initialPoint = new Rectangle(border.getWidth() / 2, border.getHeight() / 2, 2, 2);
initialPoint.setFill(Color.BLACK);
x = border.getWidth() / 2;
y = border.getHeight() / 2;
p.getChildren().addAll(border, initialPoint);
p.setOnKeyPressed(e -> {
switch (e.getCode()) {
case UP :
Line upLine = new Line(x + 1, y + 1, x + 1, y + 1 - 7.5);
y = y - 7.5;
upLine.setStroke(Color.BLUEVIOLET);
upLine.setFill(Color.SANDYBROWN);
p.getChildren().add(upLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
case DOWN :
Line downLine = new Line(x + 1, y + 1, x + 1, y + 1 + 7.5);
y = y + 7.5;
downLine.setStroke(Color.BLUEVIOLET);
downLine.setFill(Color.SANDYBROWN);
p.getChildren().add(downLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
case LEFT :
Line leftLine = new Line(x - 7.5, y + 1, x + 1, y + 1);
x = x - 7.5;
leftLine.setStroke(Color.BLUEVIOLET);
leftLine.setFill(Color.SANDYBROWN);
p.getChildren().add(leftLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
case RIGHT :
Line rightLine = new Line(x + 7.5, y + 1, x + 1, y + 1);
x = x + 7.5;
rightLine.setStroke(Color.BLUEVIOLET);
rightLine.setFill(Color.SANDYBROWN);
p.getChildren().add(rightLine);
p.getChildren().add(new Rectangle(x, y, 2, 2));
break;
default:
break;
}
});
Scene scene = new Scene(p);
primaryStage.setScene(scene);
primaryStage.setTitle("Draw Line Segments");
primaryStage.show();
p.requestFocus();
}
}
一张图片或许能更好地解释我的问题:
您可能想阅读 Coordinate System of a Node
Coordinate System
The Node class defines a traditional computer graphics "local" coordinate system in which the x axis increases to the right and the y axis increases downwards. The concrete node classes for shapes provide variables for defining the geometry and location of the shape within this local coordinate space. For example, Rectangle provides x, y, width, height variables while Circle provides centerX, centerY, and radius.
At the device pixel level, integer coordinates map onto the corners and cracks between the pixels and the centers of the pixels appear at the midpoints between integer pixel locations. Because all coordinate values are specified with floating point numbers, coordinates can precisely point to these corners (when the floating point values have exact integer values) or to any location on the pixel. For example, a coordinate of (0.5, 0.5) would point to the center of the upper left pixel on the Stage. Similarly, a rectangle at (0, 0) with dimensions of 10 by 10 would span from the upper left corner of the upper left pixel on the Stage to the lower right corner of the 10th pixel on the 10th scanline. The pixel center of the last pixel inside that rectangle would be at the coordinates (9.5, 9.5).
关于您的问题:不要使用 7.5。而是使用 7 或 8。