gpt4 book ai didi

java - 获取 JavaFX 图像的子图像以在 Canvas 上使用

转载 作者:行者123 更新时间:2023-11-29 06:55:06 26 4
gpt4 key购买 nike

我正在用 JavaFX 进行游戏编程,我为图形制作了一个 spritesheet。目前,我正在将 JavaFX Image 更改为 BufferedImage,然后使用 BufferedImage#subImage函数获取子图像,然后使用 SwingFXUtils 将其改回 JavaFX Image。我试图寻找一种更简单的方法,但没有找到。他们是获取 JavaFX 子图像的更简单方法吗?

最佳答案

/*
suppose i have an image strip of 10 frames of 32x32 ( 320x32 ), then i want to separate all frames in individual images
getImage( 10, 32,32,"myStrip.png" );
*/
public static Image[] getImage( int frames, int w, int h, String pathFile )
{

Image[] imgs = new Image[ frames ];

//img that contains all frames
Image stripImg = new Image( pathFile );
PixelReader pr = stripImg.getPixelReader();
PixelWriter pw = null;

for( int i = 0; i < frames ; i++)
{

WritableImage wImg = new WritableImage( w, h );

pw = wImg.getPixelWriter();

for( int readY = 0 ; readY < h; readY++ )
{

int ww = (w * i);
for( int readX = ww; readX < ww + w; readX++ )
{
//get pixel at X & Y position
Color color = pr.getColor( readX, readY );

//set pixel to writableimage through pixel writer
pw.setColor(readX - ww, readY, color);

}//X


}//Y


//finally new image is stored
imgs[ i ] = wImg;
}//


stripImg = null;
pr = null;
pw = null;

return imgs;
}//

关于java - 获取 JavaFX 图像的子图像以在 Canvas 上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36047458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com