私はJavaのコーディングに不慣れで、アドバイスが必要です。
ユーザーコンソールの入力から指定されたとおりにグラフィックパネルに図形を描画する小さなプログラムをコーディングしています。(スキャナークラスを使用)
たとえば、ユーザーはmove 100 100
グラフィックスの「ペン」をx、yポイントに移動するために入力するかline 100 200
、2つの座標間に線を引くために入力するか、半径50pxの円を描くために「circle50」と入力することができます。
次の目標は、コマンド'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();
}
}
たとえば、テキストファイルを含める必要があります。
移動100100 サークル50 100行目200
そして、操作'load example.txt'を呼び出すと、このテキストファイルがアプリケーションによって読み取られ、その中のコマンドがグラフィックキャンバスに指定された形状を描画することによって実行されます。
どんな助けやガイダンスも大歓迎です。私はJavaプログラミングを1年未満行っており、常に改善しようとしているので、建設的な批判は大歓迎です。