当前位置: 首页 > 文档资料 > JavaFX 中文教程 >

图像(Images)

优质
小牛编辑
133浏览
2023-12-01

您可以使用JavaFX在javafx.scene.image包中提供的类来加载和修改图像。 JavaFX支持Bmp, Gif, Jpeg, Png等图像格式。

本章将教您如何将图像加载到JavaFX,如何在多个视图中投影图像以及如何更改图像的像素。

加载图片

您可以通过实例化包javafx.scene.image名为Image的类在JavaFX中加载图像。

对于类的构造函数,您必须通过以下任一方法 -

  • 要加载的图像的InputStream对象,或

  • 包含图像URL的字符串变量。

//Passing FileInputStream object as a parameter 
FileInputStream inputstream = new FileInputStream("C:\\images\\image.jpg"); 
Image image = new Image(inputstream); 
//Loading image from URL 
//Image image = new Image(new FileInputStream("url for the image));

加载图像后,您可以通过实例化ImageView类并将图像传递给其构造函数来设置图像的视图,如下所示 -

ImageView imageView = new ImageView(image);

例子 (Example)

以下是演示如何在JavaFX中加载图像并设置视图的示例。

将此代码保存在名为ImageExample.java的文件中。

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.image.ImageView;  
import javafx.stage.Stage;  
public class ImageExample extends Application {  
   @Override 
   public void start(Stage stage) throws FileNotFoundException {         
      //Creating an image 
      Image image = new Image(new FileInputStream("path of the image"));  
      //Setting the image view 
      ImageView imageView = new ImageView(image); 
      //Setting the position of the image 
      imageView.setX(50); 
      imageView.setY(25); 
      //setting the fit height and width of the image view 
      imageView.setFitHeight(455); 
      imageView.setFitWidth(500); 
      //Setting the preserve ratio of the image view 
      imageView.setPreserveRatio(true);  
      //Creating a Group object  
      Group root = new Group(imageView);  
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 500);  
      //Setting title to the Stage 
      stage.setTitle("Loading an image");  
      //Adding scene to the stage 
      stage.setScene(scene);
      //Displaying the contents of the stage 
      stage.show(); 
   }  
   public static void main(String args[]) { 
      launch(args); 
   } 
}

使用以下命令从命令提示符编译并执行保存的java文件。

Javac ImageExample.java 
java ImageExample

执行时,上面的程序生成一个JavaFX窗口,如下所示 -

加载图片

图像的多个视图

您还可以为同一场景中的图像设置多个视图。 以下程序是演示如何在JavaFX中为场景中的图像设置各种视图的示例。

将此代码保存在名为MultipleViews.java的文件中。

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.image.ImageView; 
import javafx.stage.Stage;  
public class MultipleViews extends Application {  
   @Override 
   public void start(Stage stage) throws FileNotFoundException {         
      //Creating an image 
      Image image = new Image(new FileInputStream("file path"));  
      //Setting the image view 1 
      ImageView imageView1 = new ImageView(image); 
      //Setting the position of the image 
      imageView1.setX(50); 
      imageView1.setY(25); 
      //setting the fit height and width of the image view 
      imageView1.setFitHeight(300); 
      imageView1.setFitWidth(250);         
      //Setting the preserve ratio of the image view 
      imageView1.setPreserveRatio(true); 
      //Setting the image view 2 
      ImageView imageView2 = new ImageView(image);
      //Setting the position of the image 
      imageView2.setX(350); 
      imageView2.setY(25); 
      //setting the fit height and width of the image view 
      imageView2.setFitHeight(150); 
      imageView2.setFitWidth(250);          
      //Setting the preserve ratio of the image view 
      imageView2.setPreserveRatio(true); 
      //Setting the image view 3 
      ImageView imageView3 = new ImageView(image);  
      //Setting the position of the image 
      imageView3.setX(350); 
      imageView3.setY(200); 
      //setting the fit height and width of the image view 
      imageView3.setFitHeight(100); 
      imageView3.setFitWidth(100);         
      //Setting the preserve ratio of the image view 
      imageView3.setPreserveRatio(true);  
      //Creating a Group object  
      Group root = new Group(imageView1, imageView2, imageView3);  
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 400);  
      //Setting title to the Stage 
      stage.setTitle("Multiple views of an image");  
      //Adding scene to the stage 
      stage.setScene(scene);  
      //Displaying the contents of the stage
      stage.show(); 
   }  
   public static void main(String args[]) { 
      launch(args); 
   } 
}        

使用以下命令从命令提示符编译并执行保存的java文件。

Javac MultipleViews.java 
java MultipleViews

执行时,上面的程序生成一个JavaFX窗口,如下所示 -

多视图

写像素

JavaFX提供了名为PixelReaderPixelWriter类的类来读取和写入图像的像素。 WritableImage类用于创建可写图像。

以下是演示如何读取和写入图像像素的示例。 在这里,我们正在读取图像的颜色值并使其更暗。

将此代码保存在名为WritingPixelsExample.java的文件中。

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.image.ImageView; 
import javafx.scene.image.PixelReader; 
import javafx.scene.image.PixelWriter; 
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color; 
import javafx.stage.Stage;  
public class WritingPixelsExample extends Application {  
   @Override 
   public void start(Stage stage) throws FileNotFoundException {         
      //Creating an image 
      Image image = new Image(new FileInputStream("C:\\images\\logo.jpg")); 
      int width = (int)image.getWidth(); 
      int height = (int)image.getHeight(); 
      //Creating a writable image 
      WritableImage wImage = new WritableImage(width, height); 
      //Reading color from the loaded image 
      PixelReader pixelReader = image.getPixelReader(); 
      //getting the pixel writer 
      PixelWriter writer = wImage.getPixelWriter();           
      //Reading the color of the image 
      for(int y = 0; y < height; y++) { 
         for(int x = 0; x < width; x++) { 
            //Retrieving the color of the pixel of the loaded image   
            Color color = pixelReader.getColor(x, y); 
            //Setting the color to the writable image 
            writer.setColor(x, y, color.darker());              
         }
      }	
      //Setting the view for the writable image 
      ImageView imageView = new ImageView(wImage); 
      //Creating a Group object  
      Group root = new Group(imageView);  
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 500);  
      //Setting title to the Stage 
      stage.setTitle("Writing pixels ");  
      //Adding scene to the stage 
      stage.setScene(scene);  
      //Displaying the contents of the stage 
      stage.show();  
   }
   public static void main(String args[]) { 
      launch(args); 
   } 
}

使用以下命令从命令提示符编译并执行保存的java文件。

Javac WritingPixelsExample.java 
java WritingPixelsExample 

执行时,上面的程序生成一个JavaFX窗口,如下所示 -

写像素