我試圖垂直鏡像像素圖我在Java中;這裏是我的代碼:垂直鏡像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()));
}
}
}
,我仍然得到同樣的出界異常;不知道我哪裏錯了這段代碼^
那麼,'-100'是否在圖像的範圍內呢? – MadProgrammer
@MadProgrammer哦,哇,沒有想到這一點,我只是想座標,只是試圖翻轉它的Y軸。考慮到從0到coords.width的所有x值,我該如何做到這一點的任何建議都是正面的? – Rohan
'coords.width - x'?但你只想做一半的寬度。請記住,你一定要交換像素 – MadProgrammer