java基础:10.4 Java FX之形状

JavaFX 提供了多种形状类,用于绘制文本、直线、圆、矩形、椭圆、孤、多边形以及折线。

Shape 类是一个抽象基类,定义了所有形状的共同属性。这些属性有fill、stroke,strokeWidth。

fill 属性指定一个填充形状内部区域的颜色。

Stroke 属性指定用于绘制形状边缘的颜色。

strokeWidth 属性指定形状边缘的宽度。

Shape的子类:

 

1、Text

    Pane pane = new Pane();
    Text text1 = new Text(20,20,"Java is interesting");
    text1.setFont(Font.font("Courier",FontWeight.BOLD,FontPosture.ITALIC,15));
    text1.setFill(Color.RED);
    text1.setUnderline(true);       // underline
    text1.setStrikethrough(true);   // strikethrough
    pane.getChildren().add(text1);
    
    
    Scene scene1 = new Scene(pane, 500, 500);   // create a scene   
    primaryStage.setTitle("ShowBorderdPane");
    primaryStage.setScene(scene1);     // place the scene in the stage
    primaryStage.show();

 

2、Line

1条直线通过4 个参数(startX、startY、endX 以及endY) 连接两个点.

    //line
    Line line1 = new Line(70,70,120,120);   
    Line line2 = new Line(70,120,120,70);  
   // line1.endXProperty().bind(pane.widthProperty().divide(2));
   // line1.endYProperty().bind(pane.heightProperty().divide(2));    
    line1.setStrokeWidth(5);
    line2.setStrokeWidth(5);
    line1.setStroke(Color.MEDIUMAQUAMARINE);
    line2.setStroke(Color.MEDIUMAQUAMARINE);
    pane.getChildren().addAll(line1,line2);

 

3、Rectangle

— 个矩形通过参数x、y、width、height、arcWidth 以及arcHeight 定义。矩形的左上角点处于(x,y), 参数aw(arcWidth) 表示圆角处弧的水平直径,ah(arcHeight)表示圆角处弧的垂直直径。

    Rectangle r1 = new Rectangle(10,130,40,50);
    r1.setStroke(Color.TAN);
    r1.setFill(Color.WHITE);           // r1.setFill(null);
    pane.getChildren().add(r1);

 

4、Circle   Ellipse

    Circle circle = new Circle(90,160,15);
    Ellipse ellipse = new Ellipse(160,160,20,10);

 

还有一些子类,例如Arc 一段弧,Polygon 多边形,Polyline 不会自动闭合的多点连线。

总程序如下:

public class MyJavaFX extends Application {
	
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {     //set a primary stage
    
    Pane pane = new Pane();
    
    // text
    Text text1 = new Text(50,50,"Java is interesting");
    text1.setFont(Font.font("Courier",FontWeight.BOLD,FontPosture.ITALIC,15));
    text1.setFill(Color.RED);
    text1.setUnderline(true);       // underline
    text1.setStrikethrough(true);   // strikethrough
    
    //line
    Line line1 = new Line(70,70,120,120);   
    Line line2 = new Line(70,120,120,70);  
    // line1.endXProperty().bind(pane.widthProperty().divide(2));
    // line1.endYProperty().bind(pane.heightProperty().divide(2));    
    line1.setStrokeWidth(5);
    line2.setStrokeWidth(5);
    line1.setStroke(Color.MEDIUMAQUAMARINE);
    line2.setStroke(Color.MEDIUMAQUAMARINE);
    
    // rectangle
    Rectangle r1 = new Rectangle(10,130,40,50);
    r1.setStroke(Color.TAN);
    r1.setFill(Color.WHITE);           // r1.setFill(null);
    
    // circle
    Circle circle = new Circle(90,160,15);
    Ellipse ellipse = new Ellipse(160,160,20,10);
    
    
    pane.getChildren().addAll(text1,line1,line2,r1,circle,ellipse);
    
    Scene scene1 = new Scene(pane, 500, 500);   // create a scene   
    primaryStage.setTitle("ShowBorderdPane");
    primaryStage.setScene(scene1);     // place the scene in the stage
    primaryStage.show();
    
    }
}

转载于:https://www.cnblogs.com/l20902/p/10610914.html

你可能感兴趣的:(java基础:10.4 Java FX之形状)