关于我的班级以及为什么它没有检测到 java.awt.Color?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18725157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:30:39  来源:igfitidea点击:

About my class and why it's not detecting java.awt.Color?

javaawtdraw

提问by allenlistar

I've got a issue with the program I'm writing - this is an APCS A class so there is an APCS.lib - it includes the DrawingTool Class that is used in the program. I'm having issues with the import java.awt.Color statement:

我正在编写的程序有问题——这是一个 APCS A 类,所以有一个 APCS.lib——它包括程序中使用的 DrawingTool 类。我在导入 java.awt.Color 语句时遇到问题:

There is a driver that is executed for the entire program but my question is in the line import java.awt.Color; and line 33 - pencil.SetColor(Color, red); . Not sure my drjava is not detecting that java.awt.color import and still giving me a "cannot find symbol/variable error." The program draws a house.

有一个为整个程序执行的驱动程序,但我的问题是在 import java.awt.Color; 行中。第 33 行 - pen.SetColor(Color, red); . 不确定我的 drjava 没有检测到 java.awt.color 导入并且仍然给我一个“找不到符号/变量错误”。该程序绘制了一个房子。

//Name£∫ Allen Li
//Date: Monday, September 9th, 2013
//Purpose: Using apcslib to draw a house on with a piece of paper and pencil. 

import apcslib.*;
import java.awt.Color; 
public class DrawHouse{

  private DrawingTool pencil;
    private SketchPad paper;

 /**
  *  Basic constructor for DrawHouse.  
  *  Instantiates paper and pencil to basic
  *  values.
  */
 public DrawHouse(){
  paper = new SketchPad(300, 400);
     pencil = new DrawingTool(paper);
 }

   /**
     *  The draw method for the DrawHouse class.  
     *  This method will run all of the
     *  commands necessary to draw the house.
     *
     */

   public void draw(){

     // draw the main house
     pencil.setWidth(3); 
     pencil.setColor(Color, red); 
     pencil.down(); 
     pencil.move(-100,0); 
     pencil.move(-100,100); 
     pencil.move(100,100); 
     pencil.move(100,0); 
     pencil.move(0,0); 

     //door

     pencil.up(); 
     pencil.move(-20,0);
     pencil.down(); 
     pencil.move(-20,50); 
     pencil.move(20,50); 
     pencil.move(20,0); 

     //roof
     pencil.up(); 
     pencil.move(-100,100); 
     pencil.down(); 
     pencil.move(0,150); 
     pencil.move(100,100); 


     //window left
     pencil.up(); 
     pencil.move(-70, 60); 
     pencil.down(); 
     pencil.move(-40, 60); 
     pencil.move(-40, 90); 
     pencil.move(-70, 90); 
     pencil.move(-70, 60); 

     //window right
     pencil.up(); 
     pencil.move(70,60); 
     pencil.down(); 
     pencil.move(40,60); 
     pencil.move(40,90); 
     pencil.move(70,90); 
     pencil.move(70,60); 
     pencil.up();





   }
}

采纳答案by Xynariz

To elaborate on the given answers:

详细说明给定的答案:

The setColor method requires a single Colorobject as its parameter. You cannot pass it Color, as that is a class, and you cannot pass it red, as that doesn't mean anything to the compiler (it tries to find a variable named red).

setColor 方法需要一个Color对象作为其参数。你不能传递它Color,因为它是一个类,你不能传递它red,因为这对编译器没有任何意义(它试图找到一个名为 的变量red)。

The trick is to access the static variable Color.redor Color.RED(which is a Colorobject), and pass it into the method, as the other answers have done. As mentioned in another answer, the javadocs for Colormay help you here.

诀窍是访问静态变量Color.redor Color.RED(它是一个Color对象),并将其传递给方法,就像其他答案所做的那样。正如另一个答案中提到的,javadocs forColor可能会在这里帮助你。

回答by Marc Hauptmann

java.awt.Color has a constant red. It should be

java.awt.Color 有一个常量red。它应该是

pencil.setColor(Color.red);

in line 33. Have a look at its the javadoc for Color.

在第 33 行。看看它的javadoc forColor.

回答by JNYRanger

You have a syntax error where you have pencil.setColor(Color, red);

你有一个语法错误,你有 pencil.setColor(Color, red);

This line should be: pencil.setColor(Color.RED);

这一行应该是: pencil.setColor(Color.RED);

By the way welcome to SO!

顺便说一下,欢迎来到 SO!