【发布时间】: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