JAVA Cartesian shape plotter

121 Views Asked by At

I am trying to create a java program that will accept inputs from the user to draw a single star (*) rectangle within the grid. I want the program to accept the (x, y) coordinate pair of the top left corner of the rectangle, along with the rectangle’s width and height. It should then draw the rectangle like this Ex. (x = 16, y = 14) and (width = 4, height = 5). I'm struggling with where to approach this as I am trying to do it without using arrays or 2d arrays for drawing the rectangle and cartesian grid.

How would I go about dividing up the cohesive methods I need to draw a 20x40 grid, and then draw a shape onto that grid without messing up the grid by drawing it?

Any tips/code to get my gears turning would be appreciated.

(no experience with special libraries either, so this has to be pretty vanilla java)

    
   Y
   ^
20 +
   |
   |
   |
   |
15 +
   |                 ****
   |                 ****
   |                 ****
   |                 ****                                   
10 +                 ****
   | 
   | 
   | 
   | 
 5 + 
   | 
   |
   |
   |
   0 +====+====+====+====+====+====+====+====+ >X
     0    5    10   15   20   25   30   35   40

1

There are 1 best solutions below

2
Tomáš Záluský On
  1. Prepare StringBuilder from constant string with empty grid of spaces (with Intellij Idea it is one copypasta of your example, or with Java 15+ you can use text blocks).
  2. Replace space subsequences by asterisk subsequences in loop.