Javafx 简明教程

JavaFX - Stroke Property

Stroke Fill 属性用于更改 2D 对象本身的颜色。但是,我们还可以更改 2D 对象边界的颜色。

The Stroke Fill property is used to change the colours of 2D object itself. However, we can also change the colour of a 2D object’s boundary.

使用 JavaFX 创建 2D 对象时,如果你想改善形状的质量,可以使用的部分只有两个;即,形状的封闭区域和形状的边界。因此,JavaFX 提供的属性包括设计这两个部分。

In JavaFX, while creating a 2D object, there are only two parts you can work with, if you want to improve the quality of the shape; i.e., the enclosed area of the shape and the boundary of the shape. Hence, the properties provided by JavaFX include designing both these parts.

在本章中,我们将详细了解 Stroke 属性。

In this chapter, we will learn about the Stroke property in detail.

Stroke Property

JavaFX 中的 Stroke 属性用于更改形状边界的颜色。此属性属于 Paint 类型,它表示形状边界线的颜色。可以使用 setStroke() 方法向此属性设置值。此方法是 javafx.scene.paint 包的一部分,并采用 Color 对象作为参数值,如下所示:

The Stroke property in JavaFX is used to change colours of the shape boundary. This property is of the type Paint and it represents the color of the boundary line of the shape. You can set a value to this property using the method setStroke(). This method is a part of javafx.scene.paint package and takes the Color object as a parameter value as shown below −

path.setStroke(Color.RED)

默认情况下,描边的颜色为黑色。下图是一个具有不同描边颜色的三角形。

By default, the color of the stroke is black. Following is a diagram of a triangle with different stroke colors.

stroke

Example

我们将看到一个示例演示如何在 JavaFX 中为 2D 形状边界着色。在此,我们绘制一个 2D 形状,比如一个矩形。默认情况下,矩形将为黑色;我们将尝试将其边界颜色更改为 Violet(紫罗兰色)。将文件保存在名为 StrokeExample.java 的文件中。

We will see an example demonstrating how to colour the boundary of a 2D shape in JavaFX. In here, we are drawing a 2D shape, say a rectangle. By default, the rectangle will be coloured black; we will try to change its boundary colour to Violet. Save the file with the name StrokeExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class StrokeExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Rectangle
      Rectangle rectangle = new Rectangle(50.0f, 50.0f, 200.0f, 100.0f);

      rectangle.setStroke(Color.VIOLET);
      rectangle.setStrokeWidth(7.0);

      //Creating a Group object
      Group root = new Group(rectangle);

      //Creating a scene object
      Scene scene = new Scene(root, 300, 300);

      //Setting title to the Stage
      stage.setTitle("Drawing a Rectangle");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeExample

Output

执行后,上述程序将生成一个 JavaFX 窗口,显示一个具有颜色为紫罗兰色的描边矩形,如下所示:

On executing, the above program generates a JavaFX window displaying a rectangle with a stroke coloured violet as shown below.

stroke output

Example

在此示例中,我们绘制一个多边形,并将它的边界颜色更改为 RED(红色)。将文件保存在名为 StrokePolygonExample.java 的文件中。

In this example, we are drawing a polygon and change its boundary colour to RED. Save the file with the name StrokePolygonExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class StrokePolygonExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Polygon
      Polygon polygon = new Polygon();

      //Adding coordinates to the polygon
      polygon.getPoints().addAll(new Double[]{
         100.0, 50.0,
         200.0, 50.0,
         250.0, 150.0,
         200.0, 250.0,
         100.0, 250.0,
      });

      polygon.setStroke(Color.RED);
      polygon.setStrokeWidth(5.0);

      //Creating a Group object
      Group root = new Group(polygon);

      //Creating a scene object
      Scene scene = new Scene(root, 300, 300);

      //Setting title to the Stage
      stage.setTitle("Drawing a Polygon");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokePolygonExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokePolygonExample

Output

执行后,上述程序将生成一个 JavaFX 窗口,显示一个具有颜色为紫罗兰色的描边矩形,如下所示:

On executing, the above program generates a JavaFX window displaying a rectangle with a stroke coloured violet as shown below.

stroke polygon