迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

JavaFX setFill() 方法

作者:迹忆客 最近更新:2023/09/17 浏览次数:

setFill() 方法用于在 JavaFX 中填充形状和其他元素的颜色。本教程演示了在 JavaFX 中使用 setFill() 方法。


JavaFX setFill() 方法

setFill() 方法可以将统一的图像图案和渐变图案填充到 JavaFX 中的形状中。要使用 setFill() 方法,我们需要 JavaFX.scene.paint 包

setFill() 可用于将颜色填充到 ShapeText 等类中。

语法:

//Setting color to the text
Color color = new Color.Red
text.setFill(color);

上面的语法使用 paint 包中的 Color 类来指定颜色并使用 setFill() 方法将其填充到文本中。以下是使用 setFill 方法将颜色填充到场景中的步骤。

让我们有一个基于上述步骤的示例。

示例代码:

package delftstack;

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class JavaFX_SetFill extends Application {
    @Override
    public void start(Stage DemoStage) {
        Group DemoGroup = new Group();
        Scene DemoScene = new Scene(DemoGroup, 200, 150);
        DemoScene.setFill(Color.LIGHTBLUE);

        Circle DemoCircle = new Circle(100, 100, 80, Color.RED);

        DemoGroup.getChildren().add(DemoCircle);
        DemoStage.setScene(DemoScene);
        DemoStage.show();
    }

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

上面的代码将创建一个圆形的场景。它使用 setFill 方法为场景填充颜色。

输出:

JavaFX SetFill

让我们尝试使用 setFill() 方法为形状和文本填充颜色。

示例代码:

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class JavaFX_SetFill extends Application {
    @Override
    public void start(Stage DemoStage) {
        //Draw a Square
        Rectangle Square = new Rectangle();

        //Set the properties of the Square
        Square.setX(200.0f);
        Square.setY(200.0f);
        Square.setWidth(300.0f);
        Square.setHeight(300.0f);

        //Set color to the Square
        Square.setFill(Color.LIGHTBLUE);

        //Set the stroke width
        Square.setStrokeWidth(3);

        //Set color to the stroke
        Square.setStroke(Color.LIGHTGREEN);

        //Draw a text
        Text DemoText = new Text("This is a colored Square");

        //Set the font of the text
        DemoText.setFont(Font.font("Edwardian Script ITC", 60));

        //Set the position of the text
        DemoText.setX(155);
        DemoText.setY(50);

        //Set color to the text
        DemoText.setFill(Color.BEIGE);
        DemoText.setStrokeWidth(2);
        DemoText.setStroke(Color.LIGHTBLUE);

        //Create a Group object
        Group Group_Root = new Group(Square, DemoText);

        //Create a scene object
        Scene DemoScene = new Scene(Group_Root, 600, 300);

        //Set title to the Stage
        DemoStage.setTitle("SetFill Example");

        //Add scene to the stage
        DemoStage.setScene(DemoScene);

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

上面的代码将创建一个正方形和一个文本,然后使用 setfill 方法为正方形填充颜色。它还对边框使用 setStroke 方法。

输出:

JavaFX 设置填充形状

setFill 方法还可以将图像渐变填充到形状或文本中。

示例代码:

package delftstack;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class JavaFX_SetFill extends Application {
    @Override
    public void start(Stage DemoStage) throws FileNotFoundException {
        //Draw a Square
        Rectangle Square = new Rectangle();

        //Set the properties of the Square
        Square.setX(200.0f);
        Square.setY(200.0f);
        Square.setWidth(300.0f);
        Square.setHeight(300.0f);

        //Draw a text
        Text DemoText = new Text("This is a Gradient Square");

        //Set the font of the text
        DemoText.setFont(Font.font("Edwardian Script ITC", 60));

        //Set the position of the text
        DemoText.setX(155);
        DemoText.setY(50);

        //Set the image pattern
        Image DemoImage = new Image(new FileInputStream("Delftstack.png"));
        ImagePattern Image_Gradient = new ImagePattern(DemoImage, 80, 80, 160, 160, false);

        //Set the linear gradient to the Square
        Square.setFill(Image_Gradient);

        //Create a Group object
        Group Group_Root = new Group(Square, DemoText);

        //Create a scene object
        Scene DemoScene = new Scene(Group_Root, 600, 300);

        //Set title to the Stage
        DemoStage.setTitle("SetFill Example");

        //Add scene to the stage
        DemoStage.setScene(DemoScene);

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

上面的代码将图像渐变填充为方形。

输出:

JavaFX 设置填充渐变

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便