1

Currently, I am trying to create a program that draws a square anywhere in a 15x15 Quadrant I (Coordinate plane) grid. I am stuck on trying to get the axes displaying correctly.

This is the code I have thus far:

import java.util.Scanner;

public class Question2square {
  public static void main(String[] args) {

    // Axis variables
    int yAxismin = 0;
    int yAxismax = 15;
    int xAxismin = 0;
    int xAxismax = 15;

     //Loop through all coordinates on plane using for loops

    for(int y = yAxismin; y <= yAxismax; y++)
    {
      for(int x = xAxismin; x <= xAxismax; x++)
      {
        //Draw the axis 
        if (!Axis(x,y).equals("")) {
          System.out.print(Axis (x,y));
        }
      }
    System.out.println("");
    }
  }
  // This method draws the 15x15 axis
  public static String Axis(int x, int y)
  {
   // Each if and else if statement dictates what symbol needs to go where for the axes
  // If there is nothing to be drawn, there will simply be a blank space
    if (x == 15 && y== 0) return ">";    
    else if(x == 0 && y == 15) return "^";     
    else if (x == 0 && y == 0 )return ".";    
    else if(x == 0 && y >= 0) return "|";    
    else if(x >= 0 && y==0) return "-";    
    else return "";    
  }  


/*  
// Method to be used to draw actual square
  public static ... drawSquare(...) {
  }
*/
}

Unfortunately, instead of drawing the 'L' shaped axes I desire, it displays an 'r' shape instead. I'm trying to figure out how to display the axis properly.

I tried flipping the for loops but that didn't help. I don't see what else could be inhibiting this.

nobody
  • 19,814
  • 17
  • 56
  • 77
Kommander Kitten
  • 283
  • 2
  • 7
  • 16
  • There is a nice example here although it is not what you want :http://stackoverflow.com/questions/23183344/draw-varying-size-rectangle-with-different-orientation-using-rectangle-2d – EugenSunic Feb 03 '15 at 21:01

1 Answers1

2

Try reversing the y loop:

for(int y = yAxismax; y >= yAxismin; y--) ...

As your loop prints lines to the console from "top to bottom" and then "left to right", you want your greatest value of y to come first and your least value of x to come first. Therefore you only need to reverse the y loop to go from yAxismax to yAxismin. The result (for limits of 3 and not 15 is then:

^
|
|
.-->
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
  • Ah! That did it! I had tried switching where y started before but had neglected to change the string update from 'y++' to 'y--'. Thanks again Andy! Very impressed by the promptness and quality of response on here. Sorry for the annoyances I may be causing! – Kommander Kitten Feb 03 '15 at 21:05
  • Now to suffer through drawing the actual square. D: – Kommander Kitten Feb 03 '15 at 21:05
  • @KommanderKitten. No apologies required. You reward people that help you by upvoting good answers, and by marking the best answer to your question by clicking the check mark - this also gets you points for every answer you accept. – Andy Brown Feb 03 '15 at 21:10