JavaFX setFill() 方法
setFill()
方法用于在 JavaFX 中填充形状和其他元素的颜色。本教程演示了在 JavaFX 中使用 setFill()
方法。
JavaFX setFill()
方法
setFill()
方法可以将统一的图像图案和渐变图案填充到 JavaFX 中的形状中。要使用 setFill()
方法,我们需要 JavaFX.scene.paint 包
。
setFill()
可用于将颜色填充到 Shape
、Text
等类中。
语法:
//Setting color to the text
Color color = new Color.Red
text.setFill(color);
上面的语法使用 paint 包中的 Color
类来指定颜色并使用 setFill()
方法将其填充到文本中。以下是使用 setFill
方法将颜色填充到场景中的步骤。
- 创建一个扩展 Application 类并实现
start()
方法的类。 - 通过实例化
Group
类来创建一个组。 - 通过实例化
Scene
类并将group
传递给它来创建一个场景。 - 使用
setFill
方法将颜色填充到场景中。 - 创建一个形状、圆形、矩形等,并将该形状添加到
组
。 - 将
scene
传递给舞台,并通过Show
方法显示舞台。 - 在
main
方法中启动应用程序。
让我们有一个基于上述步骤的示例。
示例代码:
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
方法为场景填充颜色。
输出:
让我们尝试使用 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
方法。
输出:
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);
}
}
上面的代码将图像渐变填充为方形。
输出:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。