【问题标题】:java.awt.Color getRed() returns "cannot find symbol" [duplicate]java.awt.Color getRed() 返回“找不到符号”[重复]
【发布时间】:2017-02-26 02:37:00
【问题描述】:

我正在尝试从 Sedgewick 的 Java 编程简介一书中做这个练习 3.1.6。

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();
    }
}

当我尝试编译这段代码时,我收到编译错误消息“找不到符号”,指向 getRGB() 方法。这给了我一个想法,我需要导入一个类或其他东西。但我已经引入了 java.awt.Color,我认为应该足够了。

班级图片来自本站

http://introcs.cs.princeton.edu/java/stdlib/Picture.java.html

并且和这个类在同一个文件夹中。

如果我将 getRed() getGreen() getBlue() 方法更改为 get(),代码可以完美运行。我猜这是因为我使用类图片中的 get() 方法而不是类 Color?我对吗?我做错了什么?如何使用 getRed() getGreen() 和 getBlue() 方法?

【问题讨论】:

  • "getRGB()" 你的代码在哪里?
  • 顺便说一句。这是练习:编写一个程序,将图像文件的名称作为命令行输入,并创建并显示三个图片对​​象。一种只包含红色成分,一种用于绿色,一种用于蓝色。
  • 检查您链接到的 Java 文件...这些方法在哪里定义?
  • 您链接的图片类不包含“getRed()”方法或任何其他颜色。我怀疑你的练习是提供这些方法。
  • 对不起.. 我将 getRGB 更改为 getRed() getGreen() getBlue() 并忘记在问题中更改它。虽然同样的问题和完全相同的编译错误。只是关于 getRed() getGreen() getBlue()。

标签: java


【解决方案1】:

您的类中没有方法 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));

【讨论】:

    【解决方案2】:

    您引用的 Picture 类只有方法 public Color get(int x, in y); 所以更改部分:

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

            Color pixelColor = pic.get(x, y);
            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);
    

    可能是这样的:

    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();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您的 Picture 类不提供 getRed()、getGreen()、getBlue() 方法。 我能找到适合您问题的唯一方法是 get()。

      get() 返回 Color 类的一个实例。 颜色确实提供 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));
      

      【讨论】:

        【解决方案4】:

        我相信您的意思是使用 Color 类的getRed() 等方法。那个 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));
        

        那么,其他人

        【讨论】:

          【解决方案5】:

          方法:Color getRed(int x, int y)Color getGreen(int x, int y)Color getBlue(int x, int y),你正在尝试使用,不存在。

          确实存在的是方法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 这样的类(您不必重新发明轮子)。 最后,根据standard naming conventions for Java,单独颜色值的名称应该是redPixelgreenPixelbluePixel,而不是redpixelgreenpixelbluepixel

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-12
            • 2019-05-31
            • 2015-06-07
            • 2020-06-13
            • 2012-09-09
            • 2011-10-29
            相关资源
            最近更新 更多