JavaFX 线段宽度

标签 java javafx 2d line

我有一些代码可以响应按键,并相应地绘制点和线段。然而,即使我没有触及代码中的线条笔划宽度,线段的宽度似乎是交替的。我想知道为什么会发生这种情况。

这是代码:

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();

    }

}

一张图片也许可以更好地解释我的问题:

enter image description here

最佳答案

您可能想阅读 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。

关于JavaFX 线段宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29866744/

相关文章:

java - 从下载的文件中提取字符串

java - 使用 Java 将变量值插入 SQL Server

python - 在 python 中搜索二维数组 - 最佳方法+缩进错误

javascript - 如何在JavaScript中计算旋转3D立方体的x和y坐标?

OpenGL 拉伸(stretch)形状 - 纵横比

java - 使用替代品在 centos 上安装 java

java - 如何计算单词之间有空格的字符串中单词的确切数量?

单击 1 次后 JavaFX 刷新按钮消失

Java:不能引用非最终变量

java - 动态添加内容到菜单