【发布时间】:2016-07-29 15:48:03
【问题描述】:
我对我正在处理的这段代码迷失了方向。我必须仅使用 java 使图片具有灰度。我有基本代码,但我不知道该放什么不会使整个屏幕变灰。我正在努力工作,但我迷路了,不知道下一步该做什么。目前的方式是,它只需要一个图像经过一个过程,然后制作一个完全相同的新版本,但我需要将新版本设为灰色版本。 访问:https://ask.extension.org/uploads/question/images/attachments/000/037/087/image_300x300%2523.jpg?1406470060我正在使用的树图。
import java.awt.*; //import the awt graphics package
class TrueColors //start of the class
{
TrueColors() //start of the main method
{
Picture pictureObj = new Picture("trunk.jpg");
pictureObj.explore();
int redValue = 0; int greenValue = 0; int blueValue = 0;
Pixel targetPixel = new Pixel(pictureObj, 0,0);
Color pixelColor = null;
for(int y=0; y < pictureObj.getHeight(); y++)
{
for(int x = 0; x < pictureObj.getWidth(); x++)
{
targetPixel = pictureObj.getPixel(x,y);
pixelColor = targetPixel.getColor();
redValue = pixelColor.getRed();
greenValue = pixelColor.getGreen();
blueValue = pixelColor.getBlue();
pixelColor = new Color(redValue, greenValue, blueValue);
targetPixel.setColor(pixelColor);
}//end of the inner for loop
}//end of the outer for loop
pictureObj.explore();
pictureObj.write("NewTrunk.jpg");
pictureObj.show();
}//end of main method
}//end of class
【问题讨论】: