我需要创建一个我的类SimpleRGB的新实例,它是为了将图片的像素更改为一种颜色而创建的。我知道我没有正确创建实例,但无法弄清楚如何做到这一点。
/**
* Get the NEW image containing only the red color. The red values of this
* new image should be exactly the same as red value of this image. The
* green and blue values of this new image should be 0s.
*
* @return the NEW image (SimpleRGB) containing only the red color of this
* image.
*/
public SimpleRGB getRedImage() {
SimpleRGB redImage = new SimpleRGB(aRed);
return redImage;
}
这是完整的课程
public class SimpleRGB {
private int aWidth;
private int aHeight;
private int[][] aRed;
private int[][] aBlue;
private int[][] aGreen;
public SimpleRGB(int aWidth, int aHeight) {
aRed = new int[aWidth][aHeight];
aBlue = new int[aWidth][aHeight];
aGreen = new int[aWidth][aHeight];
}
/**
* Gets the width of this image.
*
* @return the width of this image.
*/
public int getWidth() {
return aWidth;
}
/**
* Gets the height of this image.
*
* @return the height of this image.
*/
public int getHeight() {
return aHeight;
}
/**
* Sets the red value at coordinate (x,y) to aRed.
*
* @param x the x coordinate of this image.
* @param y the y coordinate of this image.
* @param aRed the red value (0 - 255)
*/
public void setRed(int x, int y, int aRed) {
this.aRed[x][y] = aRed;
}
/**
* Sets the green value at coordinate (x,y) to aGreen.
*
* @param x the x coordinate of this image.
* @param y the y coordinate of this image.
* @param aGreen the green value (0 - 255)
*/
public void setGreen(int x, int y, int aGreen) {
this.aGreen[x][y] = aGreen;
}
/**
* Sets the blue value at coordinate (x,y) to aBlue.
*
* @param x the x coordinate of this image.
* @param y the y coordinate of this image.
* @param aBlue the blue value (0 - 255)
*/
public void setBlue(int x, int y, int aBlue) {
this.aBlue[x][y] = aBlue;
}
/**
* Gets the red value at coordinate (x,y).
*
* @param x the x coordinate of this image.
* @param y the y coordinate of this image.
* @return the value of red at coordinate (x,y).
*/
public int getRed(int x, int y) {
return aRed[x][y];
}
/**
* Gets the green value at coordinate (x,y).
*
* @param x the x coordinate of this image.
* @param y the y coordinate of this image.
* @return the value of green at coordinate (x,y).
*/
public int getGreen(int x, int y) {
return aGreen[x][y];
}
/**
* Gets the blue value at coordinate (x,y).
*
* @param x the x coordinate of this image.
* @param y the y coordinate of this image.
* @return the value of blue at coordinate (x,y).
*/
public int getBlue(int x, int y) {
return aBlue[x][y];
}
/**
* Get the NEW image containing only the red color. The red values of this
* new image should be exactly the same as red value of this image. The
* green and blue values of this new image should be 0s.
*
* @return the NEW image (SimpleRGB) containing only the red color of this
* image.
*/
public SimpleRGB getRedImage() {
SimpleRGB redImage = new SimpleRGB(aWidth,aHeight);
return redImage;
}
/**
* Get the NEW image containing only the green color. The green values of
* this new image should be exactly the same as green value of this image.
* The red and blue values of this new image should be 0s.
*
* @return the NEW image (SimpleRGB) containing only the green color of this
* image.
*/
public SimpleRGB getGreenImage() {
SimpleRGB greenImage = new SimpleRGB(aWidth,aHeight);
return greenImage;
}
/**
* Get the NEW image containing only the blue color. The blue values of this
* new image should be exactly the same as blue value of this image. The red
* and green values of this new image should be 0s.
*
* @return the NEW image (SimpleRGB) containing only the blue color of this
* image.
*/
public SimpleRGB getBlueImage() {
SimpleRGB blueImage = new SimpleRGB(aWidth,aHeight);
return blueImage;
}
/**
* Get the NEW image representing the greyscale of this image. The grey
* colors are colors that the red, green and blue value are exactly the
* same. To convert an RGB image into a greyscale image, use the following
* formula to calculate the new value. (0.21 * red) + (0.72 * green) + (0.07
* * blue) For example, suppose the (R,G,B) value of this image at
* coordinate (10,20) are (10,100,200), since (0.21 * 10) + (0.72 * 100) +
* (0.07 * 200) = 88 the (R,G,B) value of the new greyscale image at (10,20)
* should be (88,88,88).
*
* @return the NEW image representing the greyscale of this image.
*/
public SimpleRGB getGreyImage() {
SimpleRGB greyImage = new SimpleRGB(aWidth,aHeight);
return greyImage;
}
}
答案 0 :(得分:2)
我愿意:
public SimpleRGB getRedImage() {
SimpleRGB result = new SimpleRGB(aWidth,aHeight);
for (int x = 0; x < aWidth; x++) {
for (int y = 0; y < aHeight; y++) {
result.setRed(x, y, this.getRed(x, y));
}
}
return result;
}
这将创建一个新的SimpleRGB图像,其所有颜色值都设置为0(默认情况下,int数组中的所有值都初始化为0)。然后它设置这个新的SimpleRGB的红色值,以匹配当前RGB中每个点的当前RGB红色值。
答案 1 :(得分:1)
public SimpleRGB getRedImage() {
SimpleRGB redImage = new SimpleRGB(aWidth, aHeight);
for (int x = 0; x < aWidth; ++x) {
System.arraycopy(aRed[x], 0, redImage.aRed[x], 0, aHeight);
}
return redImage;
}
这会立即直接使用私有字段,而低级方法System.arraycopy可以快速复制。