在java中显示问题,找不到符号错误

时间:2013-05-03 22:16:51

标签: java

继续获取这些无法找到符号错误。我知道它关于图片类和它的方法,但我知道它们在那里,但我的编译器无法识别它们。有任何想法吗?

     import java.awt.Color;
     import java.awt.Graphics;

     public class DispTest 
     { 

       private Picture display;
       private Graphics graphics;

       public DispTest()
       {
        display = new Picture(500,500);
        graphics = display.getGraphics();
        for(int x = 0; x < display.getWidth(); x++) {
           for(int y = 0; y < display.getHeight(); y++) {
             display.getPixel(x,y).setColor(new Color(123,204,246));
             if((x > 0 && x % 50 == 0) || (y > 0 && y % 50 == 0))
             display.getPixel(x,y).setColor(new Color(44,72,39));
           }
        }

        graphics.setColor(new Color(44,72,39));
          for(int x = 10; x < display.getWidth(); x += 50) 
             for(int y = 10; y < display.getHeight(); y += 50) 
                graphics.drawOval(x,y,30,30);
           display.show();
        }


        public static void main(String [] args)
        {
          DispTest dt = new DispTest();

        }
    }
发现了5个错误:

File: C:\Users\Andrew\SoftwareDevelopment\DispTest.java  [line: 22]
Error: cannot find symbol
  symbol:   method getGraphics()
  location: variable display of type Picture
File: C:\Users\Andrew\SoftwareDevelopment\DispTest.java  [line: 25]
Error: cannot find symbol
  symbol:   method getPixel(int,int)
  location: variable display of type Picture
File: C:\Users\Andrew\SoftwareDevelopment\DispTest.java  [line: 27]
Error: cannot find symbol
  symbol:   method getPixel(int,int)
  location: variable display of type Picture
File: C:\Users\Andrew\SoftwareDevelopment\DispTest.java  [line: 32]
Error: cannot find symbol
  symbol:   method getWidth()
  location: variable display of type Picture
File: C:\Users\Andrew\SoftwareDevelopment\DispTest.java  [line: 33]
Error: cannot find symbol
  symbol:   method getHeight()
  location: variable display of type Picture

2 个答案:

答案 0 :(得分:1)

Picture类不应该是Image类,并且在您的课程中添加了import java.awt.Image;吗?在这种情况下,IDE(Eclipse,Netbeans)会帮助你。

所以你可以:

// import
import java.awt.Image;
import java.awt.image.BufferedImage;

...

// declaration
private Image display;

...

// instantiation, feel free to choose your third argument from http://docs.oracle.com/javase/6/docs/api/java/awt/image/BufferedImage.html
display = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);

答案 1 :(得分:0)

删除.class个文件并重新编译代码。当您的.class文件来自旧版本的来源时会发生此错误。