JavaFX——添加图片ImageView
程序员文章站
2024-02-06 08:12:28
...
Image: 用于加载图片文件
ImageView: 用于显示图片
位置:Javafx.scence.image.*
Image 目前支持BMP、GIF、JPEG、PNG 、JPG图片格式
Image image = new Image(URL)
其中,URL: Uniform Resource Locator
URL 可以指向网络图片、资源图片或本地tu'pian
网络图片
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Image image = new Image("http://afanihao.cn/tu.jpg");
ImageView imageView = new ImageView();
imageView.setImage(image);
root.setCenter(imageView);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
运行结果:
本地图片:
放在本地文件系统里的图片文件
要在文件路径前面加上 “file:" 字样
注:和网络图片比较
一个是http: 开头,一个是 file: 开头
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Image image = new Image("file:C:\\Users\\Administrator\\Desktop\\龙猫.jpg");
ImageView imageView = new ImageView();
imageView.setImage(image);
root.setCenter(imageView);
//取出宽度和高度
double width = image.getWidth();
double height = image.getHeight();
Scene scene = new Scene(root,width,height);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
运行结果:
转化为字符串实现:
import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
File f = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\蓝莲花.jpg");
String url = f.toURI().toString();
Image image = new Image(url);
ImageView imageView = new ImageView();
imageView.setImage(image);
root.setCenter(imageView);
//取出宽度和高度
double width = image.getWidth();
double height = image.getHeight();
Scene scene = new Scene(root,width,height);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
资源图片
放在CLASSPATH下的文件称为资源文件(与class文件一起)
资源文件用包路径定位,例如
my/res/abc.jpg
其中,my.res 是 package路径
注:Eclipse会自动把src下的文件复制到bin目录,程序运行时访问的是bin下的文件。(bin在CLASSPATH里)
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Image image =new Image("application/蓝莲花.jpg");
ImageView imageview = new ImageView();
imageview.setImage(image);
root.setCenter(imageview);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}