Java语言程序设计——篇十四(2)

在这里插入图片描述
在这里插入图片描述

       🌿🌿🌿跟随博主脚步,从这里开始→博主主页🌿🌿🌿

  • 欢迎大家:这里是我的学习笔记、总结知识的地方,喜欢的话请三连,有问题可以私信🌳🌳🌳
    您的点赞👍、关注➕、收藏⭐️、评论📝、私信📧是我最大的支持与鼓舞!!!🌻🌻🌻

在这里插入图片描述

Color和Font类

Color类

  • 使用javafx.scene.paint.Color类可以创建颜色对象,并为形状或文本指定不同的颜色。
  • 可以通过Color类的常量创建颜色,还可通过构造方法和Color类的静态方法创建颜色。
  • Color类中定义了构造方法可以创建颜色对象。
Color(double red, double green, double blue, double opacity)
Color c = new Color(0, 0, 1, 1.0);
  • Color类还定义了多种静态方法可以返回颜色对象,如color()方法、rgb()方法和web()方法等,这些方法都可以省略(默认值是1.0)或指定颜色的透明度。
Color c = Color.color(0,0,1.0); 
Color c = Color.rgb(0,0,255);
Color c = Color.web("0x0000FF",1.0);

Font类

Font类的实例表示字体,包含字体的相关信息,如字体名字体
粗细
字体形态和大小
构造方法:

Font(double size)
Font(String name, double size)
  • 用静态方法返回字体对象:
static Font font(String family,FontWeight weight,double size)
static Font font(String family, FontWeight weight, 
                                //FontWeight枚举常量:
BOLD,粗体
LIGHT,轻体
NORMAL,正常体
FontPosture posture,double size)
//FontPosture枚举常量:
ITALIC,斜体
REGULAR,正常体
  • 使用getFamily()实例方法返回系统默认的字体。
static List<String> getFamilies()
static List<String> getFontNames()

在这里插入图片描述

实战演练

【练习 】 实现FontDemo.java JavaFX程序

import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Label;  
import javafx.scene.layout.VBox;  
import javafx.scene.text.Font;  
import javafx.stage.Stage;  
  
public class FontDemo extends Application {  
  
    @Override  
    public void start(Stage primaryStage) {  
        // 创建一个VBox作为根节点,用于垂直排列标签  
        VBox vBox = new VBox(10); // 10是子节点之间的间距  
  
        // 创建并配置第一个标签,使用Arial字体  
        Label label1 = new Label("Arial Font");  
        label1.setFont(new Font("Arial", 20)); // 设置字体和大小  
        vBox.getChildren().add(label1);  
  
        // 创建并配置第二个标签,使用Serif字体  
        Label label2 = new Label("Serif Font");  
        label2.setFont(new Font("Serif", 24, Font.BOLD)); // 设置字体、大小和样式  
        vBox.getChildren().add(label2);  
  
        // 创建并配置第三个标签,使用Monospaced字体  
        Label label3 = new Label("Monospaced Font");  
        label3.setFont(new Font("Monospaced", 18, Font.ITALIC)); // 设置字体、大小和样式  
        vBox.getChildren().add(label3);  
  
        // 创建一个场景,将根节点和尺寸传递给场景  
        Scene scene = new Scene(vBox, 300, 200);  
  
        // 配置并显示舞台  
        primaryStage.setTitle("Font Demo"); // 设置舞台标题  
        primaryStage.setScene(scene); // 将场景添加到舞台  
        primaryStage.show(); // 显示舞台  
    }  
  
    public static void main(String[] args) {  
        launch(args); // 启动JavaFX应用程序  
    }  
}

JavaFX形状

Line类及实例

  • Line类创建直线,需要为Line实例指定起始坐标和终点坐标。
    构造方法:
public Line()
public Line(double startX, double startY, double endX, double endY)
Line line = new Line();line.setStartX(100); // 设置起点坐标
line.setStartY(10);
line.setEndX(10); // 设置终点坐标
line.setEndY(110);

在这里插入图片描述

实战演练

实现LineDemo.java JavaFX程序

import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.layout.Pane;  
import javafx.scene.shape.Line;  
import javafx.stage.Stage;  
  
public class LineDemo extends Application {  
  
    @Override  
    public void start(Stage primaryStage) {  
        // 创建一个Pane作为根节点,Pane是一个可以包含子节点的布局容器  
        Pane pane = new Pane();  
  
        // 创建并配置第一条线,从(10, 10)到(100, 100)  
        Line line1 = new Line(10, 10, 100, 100);  
        line1.setStrokeWidth(2); // 设置线条的宽度  
        line1.setStroke(javafx.scene.paint.Color.BLACK); // 设置线条的颜色  
  
        // 创建并配置第二条线,从(100, 10)到(10, 100),与第一条线交叉  
        Line line2 = new Line(100, 10, 10, 100);  
        line2.setStrokeWidth(2);  
        line2.setStroke(javafx.scene.paint.Color.RED); // 使用不同的颜色以区分  
  
        // 将线条添加到Pane中  
        pane.getChildren().addAll(line1, line2);  
  
        // 创建一个场景,将根节点和尺寸传递给场景  
        Scene scene = new Scene(pane, 200, 200);  
  
        // 配置并显示舞台  
        primaryStage.setTitle("Line Demo"); // 设置舞台标题  
        primaryStage.setScene(scene); // 将场景添加到舞台  
        primaryStage.show(); // 显示舞台  
    }  
  
    public static void main(String[] args) {  
        launch(args); // 启动JavaFX应用程序  
    }  
}

Image和ImageView类

Image类

  • 可以在JavaFX的场景图中显示标准的图像,有多种标准格式图像,
    如.jpg、.png、.gif和.bmp等,这需要两步:
    ①使用javafx.scene.image.Image类从本地系统或远程服务器加载图像
    ②使用javafx.scene.image.ImageView节点显示图像
  • 构造方法:
public Image(String url)
public Image(InputStream inputStream)
public Image(String url, boolean backLoading)
Image image = new Image("images/flower.png");
File file = new File("D:\\images\\koala.png");
String localUrl = file.toURI().toURL().toString();
Image localImage = new Image(localUrl, false);
Image image = new Image(getClass().getResourceAsStream("koala.png"),true);
String remoteUrl = "http://mycompany.com/myphoto.jpg";
Image remoteImage = new Image(remoteUrl, true);

ImageView类

  • 显示图像:使用ImageView节点对象
    构造方法:
public ImageView()
public ImageView(Image image)
public ImageView(String fileUrl)
  • 由于ImageView也是Node对象,因此也可以对它应用变换、缩放、模糊等特效。
  • 在这里插入图片描述

实战演练

【练习 】 实现FontDemo.java JavaFX程序

import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Label;  
import javafx.scene.image.Image;  
import javafx.scene.image.ImageView;  
import javafx.scene.layout.HBox;  
import javafx.scene.layout.VBox;  
import javafx.scene.text.Font;  
import javafx.stage.Stage;  
  
public class FontDemo extends Application {  
  
    @Override  
    public void start(Stage primaryStage) {  
        // 创建一个VBox作为主容器  
        VBox vBox = new VBox(10);  
  
        // 创建并配置ImageView来显示图像  
        Image image = new Image("file:path/to/your/image.png"); // 替换为你的图像路径  
        ImageView imageView = new ImageView(image);  
        imageView.setFitWidth(200); // 可选:设置图像的适应宽度  
        imageView.setPreserveRatio(true); // 保持图像的宽高比  
  
        // 创建并配置一个标签,展示不同字体的文本  
        Label label = new Label("这里展示不同字体的文本");  
        label.setFont(new Font("Arial", 18, Font.BOLD_ITALIC)); // 设置字体、大小和样式  
  
        // 创建一个HBox来水平排列ImageView和Label  
        HBox hBox = new HBox(10, imageView, label);  
  
        // 将HBox添加到VBox中  
        vBox.getChildren().add(hBox);  
  
        // 创建一个场景,将根节点和尺寸传递给场景  
        Scene scene = new Scene(vBox, 300, 200);  
  
        // 配置并显示舞台  
        primaryStage.setTitle("Font Demo with Image"); // 设置舞台标题  
        primaryStage.setScene(scene); // 将场景添加到舞台  
        primaryStage.show(); // 显示舞台  
    }  
  
    public static void main(String[] args) {  
        launch(args); // 启动JavaFX应用程序  
    }  
}

特效实现

阴影效果及实例

  • 使用DropShadow对象实现节点内容的阴影效果,可以指定阴影
    颜色半径偏移量。调用节点的setEffect()方法设置这些效果。
text.setEffect(dropShadow);

在这里插入图片描述

实战演练

【练习 】 实现DropShadowDemo.java JavaFX程序

import javafx.application.Application;  
import javafx.scene.Scene;  
import javafx.scene.control.Button;  
import javafx.scene.effect.DropShadow;  
import javafx.scene.layout.StackPane;  
import javafx.stage.Stage;  
  
public class DropShadowDemo extends Application {  
  
    @Override  
    public void start(Stage primaryStage) {  
        // 创建一个按钮  
        Button btn = new Button("带阴影的按钮");  
  
        // 创建一个DropShadow效果  
        // 你可以设置多个参数,如偏移量、颜色、模糊半径、扩展半径等  
        DropShadow dropShadow = new DropShadow(10, Color.BLACK); // 默认模糊半径和扩展半径为0  
        dropShadow.setColor(Color.BLUE); // 设置阴影颜色为蓝色  
        dropShadow.setBlurType(BlurType.TWO_PASS_BOX); // 设置模糊类型  
        dropShadow.setRadius(15); // 设置模糊半径  
        dropShadow.setSpread(0.5); // 设置扩展半径  
  
        // 将DropShadow效果应用到按钮上  
        btn.setEffect(dropShadow);  
  
        // 使用StackPane作为布局容器  
        StackPane root = new StackPane();  
        root.getChildren().add(btn); // 将按钮添加到布局容器中  
  
        // 创建一个场景,将布局容器和尺寸传递给场景  
        Scene scene = new Scene(root, 300, 250);  
  
        // 配置并显示舞台  
        primaryStage.setTitle("DropShadow Demo"); // 设置舞台标题  
        primaryStage.setScene(scene); // 将场景添加到舞台  
        primaryStage.show(); // 显示舞台  
    }  
  
    public static void main(String[] args) {  
        launch(args); // 启动JavaFX应用程序  
    }  
}  
  
// 注意:上面的代码示例缺少Color类的导入,因为JavaFX的Color类没有直接包含在上面的代码中。  
// 你需要添加以下导入语句来修复这个问题:  
// import javafx.scene.paint.Color;  
// import javafx.scene.effect.BlurType;

博主用心写,读者点关注,互动传真情,知识不迷路。

  • 79
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 44
    评论
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

倾听一世,繁花盛开

你的鼓励是我创作最大的动力与支

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值