将JavaFX 8中的游戏矩阵表示为像素

时间:2014-10-16 01:13:19

标签: java image matrix javafx

我正在构建一个游戏模拟器,我有一个代表显示器像素的矩阵。我如何使用JavaFX 8来表示它(Swing给了我很多关于键绑定和声音的问题)。图像应该经常刷新。谢谢!

示例:

我有一个矩阵[64] [32],它带有布尔值。 A 1表示白色,0表示黑色。我想调用一个方法(paint())来表示矩阵的值作为显示像素。

1 个答案:

答案 0 :(得分:1)

执行此操作的最快方法可能是使用WritableImage。你可以打电话

WritableImage.getPixelWriter().setPixels(...);

从某些数据更新图像。为您的案例执行此操作的最方便的方法可能是将您的数据(" matrix")表示为一维字节数组,并使用PixelFormat将字节值映射为颜色数组(表示为argb值)。您可以调用适当版本的setPixels(...)方法。

(因此,您的paint()方法只需调用setPixels方法,可能会进行一些计算,以确切了解图像的哪些部分需要更新。)

此示例使用此技术为图像上的小(8x8)矩形设置动画。

import java.nio.ByteBuffer;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelFormat;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class DirectImageFromArray extends Application {

    @Override
    public void start(Stage primaryStage) {
        final int width = 64 ;
        final int height = 32 ;

        final int boxSize = 8 ;

        final int blackArgb = 0xFF << 24 ;
        final int whiteArgb = 0xFF << 24 | 0xFF << 16 | 0xFF << 8 | 0xFF ;

        byte[] data = new byte[width*height];
        for (int y = 0 ; y < boxSize; y++) {
            int scanLineStride = y * width ;
            for (int x = 0 ; x < boxSize; x++) {
                data[x+scanLineStride] = 1 ;
            }
        }

        WritableImage img = new WritableImage(width, height);

        // indexed colors 0 -> black, 1 -> white:
        PixelFormat<ByteBuffer> pixelFormat = 
                PixelFormat.createByteIndexedInstance(new int[] {blackArgb, whiteArgb});

        // write entire image:
        img.getPixelWriter().setPixels(0, 0, width, height, pixelFormat, data, 0, width);

        // represents the first pixel that is white:
        IntegerProperty firstLitPixel = new SimpleIntegerProperty();

        // update the data array and then the image when the property changes:
        firstLitPixel.addListener((obs, oldValue, newValue) -> {

            // track portion that changes:
            int minChangedX = width ;
            int minChangedY = height ;
            int maxChangedX = 0 ;
            int maxChangedY = 0 ;

            // move box
            for (int y = 0; y < boxSize; y++) {

                // set left edge of box to black:
                int oldIndex = (oldValue.intValue() + y * width) % (width * height) ;
                data[oldIndex] = 0 ;

                // set right edge of new box location to white:
                int newIndex = (newValue.intValue() + y * width + boxSize - 1) % (width*height) ;
                data[newIndex] = 1 ;

                // update changed portion:
                int oldX = oldIndex % width ;
                int oldY = oldIndex / width ;
                int newX = newIndex % width ;
                int newY = newIndex / width ;
                minChangedX = Math.min(minChangedX, Math.min(oldX, newX));
                maxChangedX = Math.max(maxChangedX, Math.max(oldX, newX));
                minChangedY = Math.min(minChangedY, Math.min(oldY, newY));
                maxChangedY = Math.max(maxChangedY, Math.max(oldY, newY));
            }
            int minIndex = minChangedX + minChangedY * width ;
            int changedWidth = maxChangedX - minChangedX + 1;
            int changedHeight = maxChangedY - minChangedY + 1;

            // update image
            img.getPixelWriter().setPixels(minChangedX, minChangedY, 
                    changedWidth, changedHeight, 
                    pixelFormat, data, minIndex, width);
        });

        // animate the property:
        Timeline animation = new Timeline(new KeyFrame(Duration.millis(10), event -> 
            firstLitPixel.set((firstLitPixel.get() + 1) % (width * height))
        ));

        animation.setCycleCount(Animation.INDEFINITE);
        animation.play();

        ImageView imageView = new ImageView(img);
        StackPane root = new StackPane(imageView);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

}