【问题标题】:How can I go and grab Graphic thats in Main from my method?我怎样才能从我的方法中获取 Main 中的图形?
【发布时间】:2014-04-04 16:30:20
【问题描述】:

我正在尝试在 main 中放置一个方法,但它不会编译,因为在方法中找不到图形“g”。

错误信息:

Lab5.java:66: error: method convert in class Lab5 cannot be applied to given types;
        convert(g);
        ^
  required: String
  found: Graphics
  reason: actual argument Graphics cannot be converted to String by method invocation conversion
Lab5.java:93: error: cannot find symbol
        A.drawString(accord, 300, 100);    }
         ^
  symbol:   method drawString(String,int,int)
  location: variable A of type String
2 errors

代码:

 public static void main(String[] args) {
    DrawingPanel2 panel = new DrawingPanel2(WIDTH, HEIGHT);
    panel.setBackground(Color.BLACK);
    Graphics g = panel.getGraphics();

    //  Sample line of code, showing how to display a picture from a JPG file
        drawPicture(g, panel, "240sx.jpg", 0, 0, WIDTH, HEIGHT);

    g.setColor(Color.WHITE);
    Scanner termIn = new Scanner(System.in);
    System.out.print("User, please give me a string to print:\n\t");
    String in = termIn.nextLine();

    //first string code
    String s1 = in;
    String civic = s1.replace("car","karr").replace("cool","crazy")
    .replace("the","teh").replace("red","blue").replace("kouki","cookie")
    .replace("nissan","datsun").replace("s14","silvia").replace("drift","slide")
    .replace("laugh","LOL").replace("240sx","sr20").replace("drive","shift");


    System.out.println(civic);
    g.drawString(civic, 60, 259);


    //second string code
    //Scanner termIn2 = new Scanner(System.in);
    //System.out.print("User, please give me a string to print:\n\t");
    //String honda = termIn2.nextLine();


    //String s2 = honda;
    //String accord = s2.replace("car","karr").replace("cool","crazy")
    //.replace("the","teh").replace("red","blue").replace("kouki","cookie")
    //.replace("nissan","datsun").replace("s14","silvia").replace("drift","slide")
    //.replace("laugh","LOL").replace("240sx","sr20").replace("drive","shift");


    //System.out.println(accord);
    //g.drawString(accord, 300, 100);
    convert(g);




    //attribution for original photographer
    g.drawString("Dax Rodriguez", 0, HEIGHT - FONTSIZE);
        }

//Methods


//replace a word within a sentence
public static String convert(String A) {
    Scanner termIn2 = new Scanner(System.in);
    System.out.print("User, please give me a string to print:\n\t");
    String honda = termIn2.nextLine();


    String s2 = honda;
    String accord = s2.replace("car","karr").replace("cool","crazy")
    .replace("the","teh").replace("red","blue").replace("kouki","cookie")
    .replace("nissan","datsun").replace("s14","silvia").replace("drift","slide")
    .replace("laugh","LOL").replace("240sx","sr20").replace("drive","shift");

    System.out.println(accord);
    A.drawString(accord, 300, 100);

}

【问题讨论】:

  • 只需将您的代码复制并粘贴到编辑窗口中,并确保其格式正确,以便我们阅读。单击编辑窗口中的小{} 图标将其设置在代码块中。另外,如果你的意思是在 main 方法中放置一个方法,你不能将一个方法放在一个方法中,这在 Java 中没有意义。
  • 不是方法中的方法,而是从 main 我调用方法,并且不知何故图形不会进入方法
  • 嗯,好的,明白了。措辞让我很困惑。发布代码后,我们将能够看到。
  • 好的,我得到了代码,请我是新手,所以我需要我能得到的所有帮助

标签: java string methods graphic


【解决方案1】:

当您说 convert(g); 时,g 的类型为 Graphics。 convert 当前采用 String 对象,而不是 Graphics 对象,这就是您收到错误的原因。

另外,convert 不会返回任何内容,因此您应该将 String 替换为 void。

改变这个:

public static String convert(String A) {

到这里:

public static void convert(Graphics A) {

应该修正你的错误。

【讨论】:

  • 非常感谢,但我还有一个问题,“返回”有什么作用?我不是很清楚。
  • 致 OP:注意像这样的简单错误。对象类型不是任意的;如果一个方法说它接受String 参数,那么除了String 或其子类型之一之外,没有其他对象类型会这样做(进一步混淆你的小警告:字符串被标记为final 并且不能通过潜在的扩展子类,但将我的陈述应用于一般对象类型,希望你能理解我的意思)。
  • @user3357247 你的转换方法是“public static String convert”,所以它应该返回一个字符串。返回带有return "this is a string" 的字符串使convert(g) 的值为"this is a string",因此System.out.println(convert(g)); 将打印出this is a string。鉴于您使用convert 的上下文,我认为您不希望它返回任何内容,因此您应该将public static String convert 更改为public static void convert。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 2018-06-05
相关资源
最近更新 更多