Java教程 专题
专题目录
您的位置:java > Java教程专题 > JavaFX 线
JavaFX 线
作者:--    发布时间:2019-11-20

javafx教程 - javafx线


为了在javafx场景上渲染图形,我们需要基本的形状和颜色。

node 类是所有javafx场景图形节点的基本基类。它提供了转换,翻译和应用效果到任何节点的能力。

javafx.scene.shape.shape 类是 node 类的后代。

所有较旧的javafx 2.x builder类在javafx 8中已弃用。

javafx线

当在javafx场景图形上绘制时,使用屏幕坐标空间(系统)渲染线。

屏幕坐标系将(0,0)放在左上角。

x坐标沿x轴移动点。从上到下移动点时,y坐标值增加。

下图显示了右侧的屏幕坐标系。

null

在javafx中,场景图形对象(如线,圆和矩形)是shape类的派生类。

所有形状对象可以在两个成形区域之间执行几何操作,例如减法,相交和并集。

要在javafx中绘制线条,我们将使用 javafx.scene.shape.line 类。

要创建一个 line 对象,我们需要指定一个开始(x,y)坐标和结束坐标。

创建线节点时,有两种方法来设置起点和终点。

第一种方法使用具有参数startx,starty,endx和endy的构造函数所有参数的数据类型为 double 。

以下代码使用构造函数创建具有起点(100,10)和终点(10,110)的线。

line line = new line(100, 10,   10,   110);

创建行节点的第二种方法是使用空构造函数来实例化 line 类,然后使用setter方法设置每个属性。

以下代码显示如何创建线对象和使用setter方法设置行的起点和终点。

line line = new line(); 
line.setstartx(100); 
line.setstarty(10); 
line.setendx(10); 
line.setendy(110);

在场景图上绘制的线节点默认为1.0的笔触宽度和黑色的笔触颜色。

所有形状的笔触颜色都为null,这意味着除了line,polyline和path节点之外没有颜色。

要创建不同种类的线,我们可以设置属性继承自父类 javafx.scene.shape.shape 的属性。

下表显示了我们可以在一行上设置的属性。

要检索或修改每个属性,您将使用其适当的getter和setter方法。

属性数据类型 / 说明
filljavafx.scene.paint.paint
用于填充形状内的颜色。
smoothboolean
true表示打开反锯齿,false表示关闭反锯齿。
strokedashoffsetdouble
将距离设置为虚线图案。
strokelinecapjavafx.scene.shape.strokelinecap
在线或路径的末尾设置帽样式。有三种样式:
  • strokelinecap.butt
  • strokelinecap.round
  • strokelinecap.square
strokelinejoinjavafx.scene.shape.strokelinejoin
当线相遇时设置装饰。有三种类型:
  • strokelinejoin.miter
  • strokelinejoin.bevel
  • strokelinejoin.round
strokemiterlimitdouble
设置斜角接缝的限制以及斜角接缝装饰strokelinejoin.miter。
strokejavafx.scene.paint.paint
设置形状的笔划线的颜色。
stroketypejavafx.scene.shape.stroketype
设置在shape节点的边界周围绘制描边的位置。有三种类型:
  • stroketype.centered
  • stroketype.inside
  • stroketype.outside
strokewidthdouble
设置线的宽度。

例子

以下代码创建一个line对象,并使用setter方法设置开始和结束坐标。

import javafx.application.application;
import javafx.scene.scene;
import javafx.scene.layout.vbox;
import javafx.scene.shape.line;
import javafx.stage.stage;

public class main extends application {

    @override
    public void start(stage stage) {
        vbox box = new vbox();
        final scene scene = new scene(box,300, 250);
        scene.setfill(null);
        
        line line = new line();
        line.setstartx(0.0f);
        line.setstarty(0.0f);
        line.setendx(100.0f);
        line.setendy(100.0f);
        
        box.getchildren().add(line);
        
        stage.setscene(scene);
        stage.show();
    }

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

上面的代码生成以下结果。

null

例2

以下代码设置更多的线属性,包括笔触颜色,笔触宽度和线帽。

之后,它还设置了线的破折号样式。

import javafx.application.application;
import javafx.scene.group;
import javafx.scene.scene;
import javafx.scene.paint.color;
import javafx.scene.shape.line;
import javafx.scene.shape.strokelinecap;
import javafx.stage.stage;

public class main extends application {
  @override
  public void start(stage primarystage) {
    primarystage.settitle("drawing lines");

    group root = new group();
    scene scene = new scene(root, 300, 150, color.gray);

    line redline = new line(10, 10, 200, 10);

    redline.setstroke(color.red);
    redline.setstrokewidth(10);
    redline.setstrokelinecap(strokelinecap.butt);

    redline.getstrokedasharray().addall(15d, 5d, 15d, 15d, 20d);
    redline.setstrokedashoffset(10);

    root.getchildren().add(redline);

    primarystage.setscene(scene);
    primarystage.show();
  }
  public static void main(string[] args) {
    launch(args);
  }
}

上面的代码生成以下结果。

null
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册