Java初学者坐标出界
我一直在学习如何旋转图片,我必须将图片旋转180度和270度。我还没有开始270,我似乎不知道如何修复越界错误
public Picture rotate180()
{
Picture rotated = new Picture ();
for (int x = 0, roty = 0; x < getWidth(); x++, roty++)
{
for (int y = 0, rotx = getWidth()-1; y<getHeight(); y++, rotx--)
{
Pixel oldPixel = getPixel(x,y);
Pixel newPixel = getPixel(rotx,roty);
newPixel.setColor(oldPixel.getColor());
}
}
return rotated;
}
# 1 楼答案
如上所述,for循环对图片旋转没有任何作用。我假设你的“getWidth()”调用应该得到“rotated”的宽度。如果是这样的话,那么你需要把它旋转。getWidth()来访问该属性。在剩下的代码中也使用同样的方法
rotated.getHeight(); rotated.getPixel (x, y)
等 另外,在for循环中使用两个int值作为增量,它们总是相等的,可以只使用一个# 2 楼答案
# 3 楼答案
在更一般的情况下,要旋转到任何角度,必须使用javax。imageio*