Epi*_*ous 5 java javafx shapes
我正在尝试编写将在网格上对角绘制 3 个形状的代码。前两个形状是正方形和圆形,我能够做到。
然而,第三种形状让我有些悲伤。我应该画一个十字(T 版本,而不是 X),每次我写出代码时,它看起来像一个侧面,?。我知道我只是缺少一些简单的东西,但我真的很感激你的帮助!
这是我的Shapes
程序的完整代码。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
public class Shapes extends Application {
public void start(Stage primaryStage) {
// This will build the shapes which include a Square, Circle, and 2 Lines.
// All shapes will have a width of 3.
// This Rectangle will be colored like the Square on a playstation controller
Rectangle square = new Rectangle(65, 65, 65, 65);
square.setStroke(Color.rgb(243, 211, 234));
square.setStrokeWidth(3);
square.setFill(Color.rgb(243, 211, 234));
// A circle colored like the Circle on the playstation controller.
Circle circle = new Circle(40);
circle.setStroke(Color.rgb(241, 188, 194));
circle.setStrokeWidth(3);
circle.setFill(Color.rgb(241, 188, 194));
// Lines colored like the Cross button on a playstation controller.
Line line1 = new Line(-50, 75, 50, 75);
line1.setStroke(Color.rgb(165, 191, 214));
line1.setStrokeWidth(3);
Line line2 = new Line(0, 0, 0, 100);
line2.setStroke(Color.rgb(165, 191, 214));
line2.setStrokeWidth(3);
// Setup the GridPane in the center of the stage which will also pad out from the edge of the window.
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
// Place each object in it's respective place on the pane.
// Square top left, Circle, middle, Cross bottom right.
pane.add(square, 0, 0);
pane.add(circle, 1, 1);
pane.add(line1, 2, 2);
pane.add(line2, 2, 2);
// Create the scene to display the program.
Scene scene = new Scene(pane);
primaryStage.setTitle("Shapes");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我遇到问题的特定片段。
// Lines colored like the Cross button on a playstation controller.
Line line1 = new Line(-50, 75, 50, 75);
line1.setStroke(Color.rgb(165, 191, 214));
line1.setStrokeWidth(3);
Line line2 = new Line(0, 0, 0, 100);
line2.setStroke(Color.rgb(165, 191, 214));
line2.setStrokeWidth(3);
Run Code Online (Sandbox Code Playgroud)
我确实需要水平线在窗格上高一点。它应该类似于“基督教十字架”。
任何帮助深表感谢。
看起来几何形状没问题,但对齐方式line2
错误。在使其居中的几种方法中,
为相关GridPane
子节点显式设置对齐方式:
pane.setHalignment(line2, HPos.CENTER);\n
Run Code Online (Sandbox Code Playgroud)将线条添加到Pane
具有所需布局的线条中;StackPane
,例如,默认为Pos.CENTER
:
StackPane lines = new StackPane(line1, line2);\npane.add(lines, 2, 2);\n
Run Code Online (Sandbox Code Playgroud)顺便说一句,明智地使用常量将使修补变得更容易一些。例如,使用比例值来保持大小成比例,如下所示:
\n\nprivate static final int N = 50;\n\xe2\x80\xa6\nRectangle square = new Rectangle(2 * N, 2 * N);\nCircle circle = new Circle(N);\nLine line1 = new Line(-N, 0, N, 0);\nLine line2 = new Line(0, -N, 0, N);\n
Run Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n\n我确实需要水平线在窗格上稍微高一点。它应该类似于“基督教十字架”。
\n
使用@fabian建议的方法,根据需要调整水平线的端点;请注意拉丁十字的变化,如下图所示:
\n\nLine line1 = new Line(-N, 0, N, 0); // Greek\nLine line1 = new Line(-N, -N/3, N, -N/3); // Latin\n\xe2\x80\xa6\npane.add(new Group(line1, line2), 2, 2);\n
Run Code Online (Sandbox Code Playgroud)\n\n\n
归档时间: |
|
查看次数: |
1437 次 |
最近记录: |