Javafx-images

提供:Dev Guides
移動先:案内検索

JavaFX-画像

パッケージ javafx.scene.image でJavaFXが提供するクラスを使用して、イメージをロードおよび変更できます。 JavaFXは、 Bmp、Gif、Jpeg、Png などの画像形式をサポートしています。

この章では、画像をJavaFXにロードする方法、画像を複数のビューに投影する方法、画像のピクセルを変更する方法について説明します。

画像の読み込み

パッケージ javafx.scene.imageImage という名前のクラスをインスタンス化することにより、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);

以下は、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は、画像のピクセルを読み書きするための PixelReader および PixelWriter クラスという名前のクラスを提供します。 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ウィンドウを生成します-

ピクセルの書き込み