JavaFX 2D

106 篇文章 5 订阅

The JavaFX 2D shape classes enables you to add 2D graphical shapes to the JavaFX scene graph, just as if they were any other type of JavaFX control. This enables you to draw e.g. circles or squares on top of your GUI, or create new JavaFX controls composed from 2D shapes and other more regular controls etc.

Keep in mind that JavaFX also has the JavaFX Canvas control which you can draw 2D graphics on. The main difference is, that a Canvas is a single JavaFX Node regardless of how many shapes you draw on it, whereas each JavaFX 2D shape is a separate control.

JavaFX 2D Example

Here is a quick example that shows you how to use the JavaFX 2D shapes in the JavaFX scene graph.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Gfx2DExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        Circle circle = new Circle();
        circle.setCenterX(100);
        circle.setCenterY(100);
        circle.setRadius(125);
        circle.setStroke(Color.valueOf("#ff00ff"));
        circle.setStrokeWidth(5);
        circle.setFill(Color.TRANSPARENT);

        Rectangle rectangle = new Rectangle();
        rectangle.setX(200);
        rectangle.setY(200);
        rectangle.setWidth(300);
        rectangle.setHeight(400);
        rectangle.setStroke(Color.TRANSPARENT);
        rectangle.setFill(Color.valueOf("#00ffff"));

        Pane pane = new Pane();
        pane.getChildren().add(circle);
        pane.getChildren().add(rectangle);

        Scene scene = new Scene(pane, 1024, 800, true);
        primaryStage.setScene(scene);
        primaryStage.setTitle("2D Example");

        primaryStage.show();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值