我正在做一个关于在Java中编辑图片的任务。作业要求制作一个代码,将图像的颜色更改为3种不同的阴影并对其进行动画处理,使其像GIF一样。我完成了任务,但是不知何故,当阴影之间的颜色变化时,图片的细节变得松散,在第三个阴影变化上只留下了蓝色的空白。我尝试添加代码以将颜色值返回原点,然后再更改下一个,但是仍然会发生此问题。可能是我弄错了代码。有人可以帮我吗?
/* Assignment 3, Part 1 - Go Psychedelic! */
public class Assignment3Part1
{
//
public static void main(String [] args) throws InterruptedException
{
String filename;
if (args.length > 0) {
// got a filename passed into program as a parameter
// don't change this part of the code needed by TA for grading
filename = args[0];
System.out.println("Filename passed in: " + filename);
} else {
// ask user for a picture
filename = FileChooser.pickAFile();
System.out.println("User picked file: " + filename);
}
Picture pic = new Picture(filename); // Picture to modify
//
pic.show(); // Show the original picture
Thread.sleep(1000); // Pause for 1 second. You can pause for less if you like
// TODO: insert method call to tint your picture
pic.tintRed(50);
pic.repaint(); // Show the tinted picture
Thread.sleep(1000); // Pause for 1 second
// TODO: insert method call to tint your picture
pic.tintRed(-50);
pic.tintGreen(20);
pic.repaint(); // Show the tinted picture
Thread.sleep(1000); // Pause for 1 second
// TODO: insert method call to tint your picture
pic.tintGreen(0);
pic.tintBlue(10);
pic.repaint(); // Show the tinted picture
Thread.sleep(1000); // Pause for 1 second
} // End of main method
} // End of class
方法代码
public void tintRed(int percent)
{
Pixel[] pixelArray = this.getPixels();
Pixel pixel = null;
int value = 0;
int i = 0;
//loop through all the pixels in the array
while (i<pixelArray.length)
{
// get the current pixel
pixel = pixelArray[i];
// get the value
value = pixel.getRed();
// set the value to % of what it was
pixel.setRed((int)(value*percent));
// increment the index
i++;
}
}
蓝色和绿色值相同
答案 0 :(得分:0)
您正在将颜色饱和到所有颜色都是100%(空白图片)的程度。您必须更加灵活地使用阴影算法:例如25%,50%,75%