java.awt.Color getRed()返回;找不到符号“;

java.awt.Color getRed()返回;找不到符号“;,java,Java,我试图做这个练习3.1.6,这是Sedgewick的《java编程入门》一书中的内容 import java.lang.Object; import java.awt.Color; import java.util.*; import java.util.Collections; public class treetseks{ public static void main(String[] args){ List<Color> list;

我试图做这个练习3.1.6,这是Sedgewick的《java编程入门》一书中的内容

import java.lang.Object;
import java.awt.Color;
import java.util.*;
import java.util.Collections;


public class treetseks{
    public static void main(String[] args){
        List<Color> list;
        list = new ArrayList<Color>();
        Picture pic= new Picture(args[0]);

        int width = pic.width();
        int height = pic.height();

        Picture picr = new Picture(width, height);
        Picture picg = new Picture(width, height);
        Picture picb = new Picture(width, height);

        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color redpixel = pic.getRed(x, y);
                Color greenpixel = pic.getGreen(x, y);
                Color bluepixel = pic.getBlue(x, y);
                list.add(p);
                picr.set(x, y, redpixel);
                picg.set(x, y, greenpixel);
                picb.set(x, y, bluepixel);
            }
        }

        System.out.println(list);
        pic.show();
        picr.show();
        picg.show();
        picb.show();
    }
}
导入java.lang.Object;
导入java.awt.Color;
导入java.util.*;
导入java.util.Collections;
公营树{
公共静态void main(字符串[]args){
名单;
列表=新的ArrayList();
Picture pic=新图片(args[0]);
int width=pic.width();
int height=pic.height();
图片picr=新图片(宽度、高度);
图片picg=新图片(宽度、高度);
图片picb=新图片(宽度、高度);
对于(int y=0;y
当我试图编译这段代码时,会收到编译错误消息“找不到符号”,并指向getRGB()方法。这让我想到我需要导入一个类或其他东西。但是我已经移植了java.awt.Color,我认为这应该足够了

班级照片来自这个网站

并且与该类位于同一文件夹中


如果我将getRed()getGreen()getBlue()方法更改为get(),则代码工作得非常完美。。我想这是因为我使用了类picture中的get()方法,而不是类Color?我说得对吗?我做错了什么?如何使用getRed()getGreen()和getBlue()方法?

您的类中没有getRed()、getGreen()或getBlue()方法。如果要使用这些函数,必须在Picture类中声明它们,否则请使用get()函数。要获得单个颜色,请执行以下操作:

Color c = new Color(pic.getRGB());
int redPixel = c.getRed();
int greenPixel = c.getGreen();
int bluePixel = c.getBlue();

picr.set(x, y, new Color(redPixel, 0, 0);
picg.set(x, y, new Color(0, greenPixel, 0));
picb.set(x, y, new Color(0, 0, bluePixel));

您引用的图片类只有方法
公共颜色get(int x,in y)因此更改零件:

            Color redpixel = pic.getRed(x, y);
            Color greenpixel = pic.getGreen(x, y);
            Color bluepixel = pic.getBlue(x, y);

因此可能是:

import java.lang.Object;
import java.awt.Color;
import java.util.*;
import java.util.Collections;


public class treetseks{
    public static void main(String[] args){
        List<Color> list;
        list = new ArrayList<Color>();
        Picture pic= new Picture(args[0]);

        int width = pic.width();
        int height = pic.height();

        Picture picr = new Picture(width, height);
        Picture picg = new Picture(width, height);
        Picture picb = new Picture(width, height);

        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color pixelColor = pic.get(x, y);
                list.add(pixelColor );
                int red = pixelColor.getRed();
                int green = pixelColor.getGreen();
                int blue = pixelColor.getBlue();
                Color redPixel = new Color(red, 0, 0);
                Color greenPixel = new Color(0, green, 0);
                Color bluePixel = new Color(0, 0, blue);

                picr.set(x, y, redPixel);
                picg.set(x, y, greenPixel);
                picb.set(x, y, bluePixel);
            }
        }

        System.out.println(list);
        pic.show();
        picr.show();
        picg.show();
        picb.show();
    }
}
导入java.lang.Object;
导入java.awt.Color;
导入java.util.*;
导入java.util.Collections;
公营树{
公共静态void main(字符串[]args){
名单;
列表=新的ArrayList();
Picture pic=新图片(args[0]);
int width=pic.width();
int height=pic.height();
图片picr=新图片(宽度、高度);
图片picg=新图片(宽度、高度);
图片picb=新图片(宽度、高度);
对于(int y=0;y
图片类不提供getRed()、getGreen()和getBlue()方法。 我能找到的唯一适合您问题的方法是get()

get()返回颜色类的实例。 颜色确实提供了getRed()、getGreen()和getBlue()

因此,在代码中,您可以执行以下操作

Color pixel = picture.get(x, y);
int red = pixel.getRed();
int green = pixel.getGreen();
int blue = pixel.getBlue();

picr.set(x, y, new Color(red, 0, 0));
picg.set(x, y, new Color(0, green, 0));
picb.set(x, y, new Color(0, 0, blue));

我相信您打算使用
getRed()
Color类的方法。那个Picture类没有这些方法

这些方法不接受参数,而是返回整数,而不是颜色对象

final Color rgb = pic.get(x, y);
int redpixel = rgb.getRed();
int greenpixel = rgb.getGreen();
int bluepixel = rgb.getBlue();
如果我将getRed()getGreen()getBlue()方法更改为get(),那么代码工作得非常完美

它不应该工作完美。你还没有弄清楚个别的颜色。当您以后使用
picr.set
方法时,您不需要只有红色。如果您需要,那么使用上面的代码,然后

picr.set(x,y, new Color(redpixel, 0, 0));

然后,其他方法:
Color getRed(intx,inty)
Color getGreen(intx,inty)
Color getBlue(intx,inty)
,您尝试使用的方法不存在

存在的是方法
Color get(int x,int y)
,然后您可以使用
Color
方法
int getRed()
int getGreen()
int getBlue()

因此,您应该将for循环的一部分更改为如下内容:

Color color = pic.get(x, y);
int redPixel = color.getRed();
int greenPixel = color.getGreen();
int bluePixel = color.getBlue();

picr.set(x, y, new Color(redPixel, 0, 0);
picg.set(x, y, new Color(0, greenPixel, 0));
picb.set(x, y, new Color(0, 0, bluePixel));
我还想指出的是,将来可以很容易地使用像
BufferedImage
这样的类(您不必重新发明轮子)。
最后,根据标准,单独颜色值的名称应分别为
redPixel
greenPixel
bluePixel
,而不是
redPixel
greenPixel
bluePixel

getRGB()
”这在代码中的什么位置?顺便说一下。这是一个练习:编写一个程序,将图像文件的名称作为命令行输入,创建并显示三个图片对象。一个只包含红色组件,一个表示绿色,一个表示蓝色。。。这些方法是在哪里定义的?链接的图片类不包含“getRed()”方法或任何其他颜色。我想你的练习就是提供那些方法。对不起。。我将getRGB改为getRed()getGreen()getBlue(),但忘了在问题中更改它。同样的问题,同样的编译错误。只是关于getRed()getGreen()getBlue()。
Color color = pic.get(x, y);
int redPixel = color.getRed();
int greenPixel = color.getGreen();
int bluePixel = color.getBlue();

picr.set(x, y, new Color(redPixel, 0, 0);
picg.set(x, y, new Color(0, greenPixel, 0));
picb.set(x, y, new Color(0, 0, bluePixel));