JavaFX 图形处理与动画编程指南
1. 图像加载与验证
在加载图像的过程中,需要验证文件是否具有有效的图像文件扩展名。以下是用于验证文件扩展名的 isValidImageFile()
方法:
private boolean isValidImageFile(String url) {
List<String> imgTypes = Arrays.asList(".jpg", ".jpeg",
".png", ".gif", ".bmp");
return imgTypes.stream()
.anyMatch(t -> url.toLowerCase().endsWith(t));
}
该方法使用了流 API 和 anyMatch()
方法,将 URL 字符串转换为小写,并检查字符串的结尾是否与图像类型扩展名列表匹配。
2. 自定义图像查看按钮控件
ImageViewButtons
类是一个自定义的 UI 控件,用于显示上一张和下一张图像的按钮,并管理图像列表。以下是该类的代码:
public class ImageViewButtons extends Pane {
private int currentIndex = -1;
public enum ButtonMove {NEX