我想知道是否可以找到有关此问题的帮助。我被要求使用图像(“corn.jpg”),并将其完全颠倒。我知道我需要编写一个程序来从左上角和左下角切换像素,等等,但我无法在时间用完之前让我的程序正常工作。谁能提供一些提示或建议来解决这个问题?我希望能够自己写出我的代码,所以请只提供建议。请注意,我对 APImage 和 Pixel 的了解非常有限。我正在用 Java 编程。这是我设法完成的工作。
import images.APImage;
import images.Pixel;
public class Test2
{
public static void main(String [] args)
{
APImage image = new APImage("corn.jpg");
int width = image.getImageWidth();
int height = image.getImageHeight();
int middle = height / 2;
//need to switch pixels in bottom half with the pixels in the top half
//top half of image
for(int y = 0; y < middle; y++)
{
for (int x = 0; x < width; x++)
{
//bottom half of image
for (int h = height; h > middle; h++)
{
for(int w = 0; w < width; w++)
{
Pixel bottomHalf = image.getPixel(h, w);
Pixel topHalf = image.getPixel(x, y);
//set bottom half pixels to corresponding top ones?
bottomHalf.setRed(topHalf.getRed());
bottomHalf.setGreen(topHalf.getGreen());
bottomHalf.setBlue(topHalf.getBlue());
//set top half pixels to corresponding bottom ones?
topHalf.setRed(bottomHalf.getRed());
topHalf.setGreen(bottomHalf.getGreen());
topHalf.setBlue(bottomHalf.getBlue());
}
}
}
}
image.draw();
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
每当您交换变量时,如果您的语言不允许同时赋值(Java 也不允许),则需要使用临时变量。
考虑一下:
a = 1;
b = 2;
a = b; // a is now 2, just like b
b = a; // b now uselessly becomes 2 again
Run Code Online (Sandbox Code Playgroud)
相反,请执行以下操作:
t = a; // t is now 1
a = b; // a is now 2
b = t; // b is now 1
Run Code Online (Sandbox Code Playgroud)
编辑:还有@vandale 在评论中所说的:P
| 归档时间: |
|
| 查看次数: |
3677 次 |
| 最近记录: |