我的目标是通过将像素图绘制到纹理中生成游戏的背景图像。目前我已经能够做到这一点,但现在我需要将像素图翻转到纹理中的X或Y轴上。然而,我无法找到任何可以实现这样操作的方法。像素图类不提供该功能。然后我想到,我可以将翻转的纹理区域绘制到纹理中,但到目前为止我还没有找到如何实现这一点的方法。因此,我想知道如何才能做到这一点,是否可能使用其他Java库来翻转PNG图像,然后从翻转后的图像创建一个像素图?
public Pixmap flipPixmap(Pixmap src) {
final int width = src.getWidth();
final int height = src.getHeight();
Pixmap flipped = new Pixmap(width, height, src.getFormat());
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
flipped.drawPixel(x, y, src.getPixel(width - x - 1, y));
}
}
return flipped;
}
public static void flipPixmap( Pixmap p ){
int w = p.getWidth();
int h = p.getHeight();
int hold;
//change blending to 'none' so that alpha areas will not show
//previous orientation of image
p.setBlending(Pixmap.Blending.None);
for (int y = 0; y < h / 2; y++) {
for (int x = 0; x < w / 2; x++) {
//get color of current pixel
hold = p.getPixel(x,y);
//draw color of pixel from opposite side of pixmap to current position
p.drawPixel(x,y, p.getPixel(w-x-1, y));
//draw saved color to other side of pixmap
p.drawPixel(w-x-1,y, hold);
//repeat for height/width inverted pixels
hold = p.getPixel(x, h-y-1);
p.drawPixel(x,h-y-1, p.getPixel(w-x-1,h-y-1));
p.drawPixel(w-x-1,h-y-1, hold);
}
}
//set blending back to default
p.setBlending(Pixmap.Blending.SourceOver);
}
flipped.drawPixel(x, y, src.getPixel(x, height - y - 1));
。 - Sawny