Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaFX8中的游戏矩阵表示为像素_Java_Image_Matrix_Javafx - Fatal编程技术网

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

将JavaFX8中的游戏矩阵表示为像素,java,image,matrix,javafx,Java,Image,Matrix,Javafx,我正在构建一个游戏模拟器,我有一个表示显示器像素的矩阵。我如何使用JavaFX8来表示它(Swing给了我很多关于键绑定和声音的问题)。图像应该经常刷新。谢谢大家! 例如: 我有一个矩阵[64][32],它携带布尔值。1代表白色,0代表黑色。我想调用一个方法(paint()),将矩阵的值表示为显示像素。最快的方法可能是使用。你可以打电话 WritableImage.getPixelWriter().setPixels(...); 从某些数据更新图像。对于您的案例,最方便的方法可能是将数据(“矩

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

例如:


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

最快的方法可能是使用。你可以打电话

WritableImage.getPixelWriter().setPixels(...);
从某些数据更新图像。对于您的案例,最方便的方法可能是将数据(“矩阵”)表示为一维字节数组,并使用
PixelFormat
将字节值映射为颜色数组(表示为argb值)。您可以调用该方法的适当版本

(因此,您的
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();

    }

}
导入java.nio.ByteBuffer;
导入javafx.animation.animation;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.scene.scene;
导入javafx.scene.image.ImageView;
导入javafx.scene.image.PixelFormat;
导入javafx.scene.image.WritableImage;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类DirectImageFromArray扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
最终整数宽度=64;
最终整数高度=32;
最终int boxSize=8;
最终整数blackArgb=0xFF
firstLitPixel.set((firstLitPixel.get()+1)%(宽度*高度))
));
animation.setCycleCount(animation.unfinite);
动画。播放();
ImageView ImageView=新ImageView(img);
StackPane根=新的StackPane(imageView);
场景=新场景(根,400400);
初级阶段。场景(场景);
primaryStage.show();
}
}