我正在尝试垂直镜像我在 Java 中拥有的像素图;这是我的代码:
Dimension coords = pixmap.getSize();
for (int x = 0; x < coords.width; x++) {
for (int y = 0; y < coords.height; y++) {
Color newest = pixmap.getColor(-x, y);
pixmap.setColor(x, y,
new Color(newest.getRed(),
newest.getGreen(),
newest.getBlue()));
}
}
“pixmap”是这个实例方法的参数,它本质上是加载的图像。这是我尝试翻转图像时遇到的运行时错误:
线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException 中的异常:坐标超出范围!
有小费吗?谢谢!
**编辑**
我将代码更改为:
Dimension coords = pixmap.getSize();
for (int x = 0; x < coords.width; x++) {
for (int y = 0; y < coords.height; y++) {
Color newest = pixmap.getColor(x, y);
if (x < coords.width / 2) {
pixmap.setColor(((((coords.width/2) - x) * 2) + x), y,
new Color(newest.getRed(),
newest.getGreen(),
newest.getBlue()));
} else {
pixmap.setColor((x - (((x - (coords.width/2)) * 2))), y,
new Color(newest.getRed(),
newest.getGreen(),
newest.getBlue()));
}
}
}
而且我仍然遇到同样的越界异常;不知道我在这段代码上哪里出错了^