2

私は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年未満行っており、常に改善しようとしているので、建設的な批判は大歓迎です。

4

1 に答える 1

3

したがって、これは、コードの再利用と基本的なMVC(model-view-controller)プログラミングの概念を紹介する素晴らしいプロジェクトです。基本的に、グラフィカルコンテキストがビューであり、一連のコマンドを使用してこのビューへのアクセスを制御していると考える場合、モデルは明らかにコマンド自体です。あなたが今やろうとしていることは、あなたのプログラムの他のすべてを無傷に保ちながら、代替ソースからモデルを供給することを可能にすることです。

したがって、最初のタスクは、モデル(コマンド)を取得する方法を知っているこの再利用可能な「コントローラー」コードを作成し、それを使用してビュー(グラフィックスコンテキスト)に影響を与えることです。必要なロジックはすべて揃っているので、目的の関数に移動するだけです。

public static void processCommandLine(String[] commandArgs, GraphicsScreen graphics) {
   if (commandArgs == null || commandArgs.length = 0 || commandArgs[0] == null) {
      System.out.println("Null command!");
   }

   String command = commandArgs[0];

   if (command.trim().equalsIgnoreCase("move")) {
      int x = 0;
      int y = 0;

      x = Integer.parseInt(commandArgs[1]);
      y = Integer.parseInt(commandArgs[2]);

      graphics.moveTo(x, y);
   }

   else if (command.trim().equalsIgnoreCase("circle")) {
      int radius = Integer.parseInt(one[1]);
      graphics.circle(radius);

   }

   else if (command.trim().equalsIgnoreCase("line")) {
      int x = 0;
      int y = 0;

      x = Integer.parseInt(commandArgs[1]);
      y = Integer.parseInt(comamndArgs[2]);

      graphics.lineTo(x, y);
   }

   else if (command.trim().equalsIgnoreCase("clear")) {
      graphics.clear();
   }
   else {
      System.out.println("Invalid Command!");
   }
}

これにより、コマンドとグラフィックス画面を1つの関数に渡すことができ、コマンドが「レンダリング」されます。ここで、関数にアクセスするさまざまな方法を提供します。最初のアクセス方法は、コンソールを介してユーザーにコマンドを照会し、それを実行することです。ただし、ファイルから複数のコマンドをロードして、それらすべてを一度に実行できるようにする必要もあります。したがって、ファイルから一連のコマンドを読み取るメソッドを定義します。

public static List<String> getCommands(String fileName) {
   if(fileName == null) return new ArrayList<String>(0);

   File file = new File(fileName);
   if(! (file.exists() && file.canRead()) {
      System.err.println("Cannot access file! Non-existent or read access restricted");
      return new ArrayList<String>(0);
   }

   List<String> commandLines = new ArrayList<String>(32);
   Scanner scanner = new Scanner(file);
   while(scanner.hasNextLine()) {
      commandLines.add(scanner.nextLine());
   }

   scanner.close();

   return commandLines;
}

そして今、コマンドのソースがコンソールであるかファイルであるかに応じて、コマンドをレンダリング関数にパイプする方法を変更する必要があります。

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 (command.trim().equalsIgnoreCase("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 (command.trim().equalsIgnoreCase("load")) {
         List<String> commandLines = getCommands(one[1]);
         for (String commandLine : commandLines) {
            String[] commandArgs = commandLine.split(" ");
            processCommandLine(commandArgs, graphics);
         }
      }

      else if (command.trim().equalsIgnoreCase("stop")) {
         break;
      }

      else {
         processCommandLine(one, graphics);
      }
   } while (true);

   System.out
         .println("You have decided to stop entering commands. Program terminated!");
   graphics.close();
}

これらの(マイナーな)変更により、複数の異なるソースからコマンドを取得し、それらのコマンドをビューにレンダリングする方法が得られます。

于 2012-11-28T20:30:12.763 に答える