title: Java : Drawing using a two dimensional array as a grid
body: <p>I am using a two dimensional array to represent a grid using (X,Y) coordinates. The grid is 80 columns by 30 rows. I'm using redirection (java project1 &lt; project1.input) to feed my program the command lines.</p>
  
  <p>Here's what the project1.input looks like:</p>
  
  <pre><code>c
  l 10 10 20 10 *
  l 20 10 20 20 *
  l 20 20 10 20 *
  l 10 20 10 10 *
  p 15 15 X
  d
  q
  </code></pre>
  
  <p>c = clear (re initialize)</p>
  
  <p>l = draw a line from X1 Y1 to X2 Y2 using a char ( * ) </p>
  
  <p>p = plot a point at X1 Y1 using a char ( X )</p>
  
  <p>d = print the current grid</p>
  
  <p>q = quit</p>
  
  <p>Here's my entire .java, for some reason it is only able to plot a single point and not print lines. I've tried looking at my for loops to set the points, my getInput method but I can't find the problem.</p>
  
  <pre><code>import java.util.Scanner;
  import java.lang.*;
  
  public class project1
  {
      // declaring class array named grid
      public static char[][] grid = new char[80][30];
  
      // method: main
      // purpose: calls getInput and initGrid
  
      public static void main(String[] args)
      {
      initGrid();
      getInput();
      }
  
      // method: initGrid
      // purpose: initializes the grid with spaces, also acts as the clear command
      public static void initGrid()
      {
      char fill = '1';
      for(int i = 0; i &lt; grid.length; i++){
          for(int j = 0; j &lt; grid[0].length; j++){
          grid[i][j] = fill;
          }
      }
      }
  
      // method: getInput
      // purpose: gets the input and sends it to the appropriate method
      public static void getInput()
      {
      char firstCharacter, drawCharacter;
      int X1, Y1, X2, Y2;
      Scanner in = new Scanner(System.in);
      while(in.hasNextLine()){
          firstCharacter = in.next().charAt(0);
          if(firstCharacter == 'p'){
          X1 = in.nextInt();
          Y1 = in.nextInt();
          drawCharacter = in.next().charAt(0);
          in.nextLine();
          plotPoint(X1, Y1, drawCharacter);
          }
          if(firstCharacter == 'l'){
          X1 = in.nextInt();
          Y1 = in.nextInt();
          X2 = in.nextInt();
          Y2 = in.nextInt();
          drawCharacter = in.next().charAt(0);
          in.nextLine();
          drawLine(X1, Y1, X2, Y2, drawCharacter);
          }
          if(firstCharacter == 'c'){
          in.nextLine();
          initGrid();
          }
          if(firstCharacter == 'd'){
          in.nextLine();
          printGrid();
          }
          if(firstCharacter == 'q'){
          return;
          }
      }
  
      }
  
      // method: plotPoint
      // purpose: receives the p command and plots the point with given coordinates and char
      public static void plotPoint(int X, int Y, char character)
      {
      grid[X][Y] = character;
      }
  
      // method: drawLine
      // purpose: receives the l command and draws a line with given coordinates and char
      public static void drawLine(int X1, int Y1, int X2, int Y2, char character)
      {
      int stopX = X1;
      int stopY = Y1;
      int stopX2 = X2;
      int stopY2 = Y2;
      // checks for horizontal line, left to right
      if(Y1==Y2 &amp;&amp; X1&lt;X2){
          for(int i = 0; stopX == stopX2; i++){ 
          grid[X1+i][Y1] = character;
          }
      }
  
      // checking for horizontal line, right to left
      if(Y1==Y2 &amp;&amp; X1&gt;X2){
          for(int i = 0; stopX2 == stopX; i--){
          grid[X1+i][Y1] = character;
          }
      }
  
      // checking for vertical line, top to bottom
      if(X1==X2 &amp;&amp; Y1&lt;Y2){
          for(int i = 0; stopY == stopY2; i++){
          grid[X1][Y1+i] = character;
          }
      }
  
      // checking for vertical line, bottom to top
      if(X1==X2 &amp;&amp; Y1&gt;Y2){
          for(int i = 0; stopY2 == stopY; i--){
          grid[X1][Y1+i] = character;
          }
      }   
      }
  
      // method: printGrid
      // purpose: prints the grid to show the picture
      public static void printGrid()
      {
      for(int j = 0; j &lt; grid[0].length; j++){
          for(int i = 0; i &lt;  grid.length; i++){
          System.out.print(grid[i][j]);
          if(i == grid.length-1){
              System.out.println();
          }
  
          }
      }
      }
  
  }
  </code></pre>
  
  <p>Sorry about the brackets, it doesn't copy out of emacs very well.</p>