【发布时间】:2012-11-16 16:06:37
【问题描述】:
我是 Java 编码新手,需要一些建议。
我正在编写一个小程序,该程序根据用户控制台输入的指定在图形面板中绘制形状。 (使用扫描仪类)
例如,用户可以键入 move 100 100 将图形“笔”移动到 x,y 点,
或者可以输入line 100 200 在两个坐标之间画一条线,
或者可以输入'circle 50'来绘制一个半径为50px的圆
我的下一个目标是包含命令“load example.txt”以加载一个文本文件(example.txt),其中包含一些命令,然后将在图形面板上执行。
有人告诉我最好的方法是使用
processCommandLine(String commandLine);
我已经浏览互联网很长时间了,现在正在寻找一些有用的信息,但到目前为止,我只能找到如何从文本文件中读取,很多是这样的:
Scanner reader = new Scanner(new FileInputStream("example.txt");
我知道我需要使用 Scanner 类来读取文件内容,然后(我认为使用 processCommandLine 方法)在图形面板上执行它们
到目前为止我的代码:(我已经调用了所有必要的类和方法,保存在一个单独的文件中)
import java.util.Scanner;
public class Assign1 {
public final static void main(String[] args) {
System.out.println("Let's draw something on the screen!");
GraphicsScreen graphics = new GraphicsScreen();
Scanner input = new Scanner(System.in); // used to read the keyboard
String next; // stores the next line input
String[] one;
do {
System.out.print("Enter a command (\"stop\") to finish : ");
System.out.print("Type 'help' for a list of commands ");
next = input.nextLine();
one = next.split(" ");
String command = one[0];
if (next.contains("help")) {
System.out
.println("Type 'move' followed by an X and Y co-ordinate to move the graphical pointer.");
System.out
.println("Type 'circle' followed by a radius value to output a circle.");
System.out
.println("Type 'line' followed by an X and Y co-ordinate to draw a line.");
System.out.println("Type 'clear' to reset the graphical canvas.");
}
else if (next.contains("move")) {
int x = 0;
int y = 0;
x = Integer.parseInt(one[1]);
y = Integer.parseInt(one[2]);
graphics.moveTo(x, y);
}
else if (command.equalsIgnoreCase("circle")) {
int radius = 0;
radius = Integer.parseInt(one[1]);
graphics.circle(radius);
}
else if (command.equalsIgnoreCase("line")) {
int x = 0;
int y = 0;
x = Integer.parseInt(one[1]);
y = Integer.parseInt(one[2]);
graphics.lineTo(x, y);
}
else if (next.contains("clear")) {
graphics.clear();
}
else {
System.out.println("error message");
}
} while (next.equalsIgnoreCase("stop") == false);
System.out
.println("You have decided to stop entering commands. Program terminated!");
graphics.close();
}
}
我需要包含的文本文件,例如:
移动 100 100 50圈 第 100 200 行当我调用“load example.txt”操作时,该文本文件将被应用程序读取,并通过在图形画布上绘制指定的形状来执行其中的命令。
任何帮助或指导将不胜感激,我从事 Java 编程不到一年,并且一直在努力改进,因此欢迎任何建设性的批评。
【问题讨论】:
-
有什么东西不能用吗?你有具体的问题吗?很难准确理解您的要求。
-
我正在尝试创建命令“load”,该命令将从文本文件中加载命令,例如,如果用户键入“load example.txt”,则文本文件中的命令将被读取并在安慰。这些命令是 graphics.draw 命令,可将用户输入的指定形状呈现到图形画布上。
标签: java command-line java.util.scanner java-io file-io