Print a Rectangle on a Grid
Hey guys,
I need to write a program that prints a rectangle based on the position on the grid and the size. So if a rectangle has a top left corner of (5, 10) with a width of 4 and height of 7, then the program would display:
.....^
.20 +
.....|
.....|
.....|
.....|
.15 +
.....|
.....|
.....|
.....|
.10 +......****
.....|.......****
.....|.......****
.....|.......****
.....|.......****
..5 +.......****
.....|.......****
.....|
.....|
.....|
..0 +====+====+====+====+====+====+====+====+>
.....0.......5......10.....15......20......25..... 30......35......40
Ignore the periods. I've got the grid printed out nicely, and all the coordinates inputted from the user, but I have no idea how to apply them.
Here's the method to print the grid:
Code:
private static void printGrid(int x, int y, int w, int h)
{
printVerticalLines();
printHorizontalLines();
}
I don't want to paste all my code here, since they're not what I'm concerned about. I just need some ideas on how to print the rectangle within the printGrid method.
Hope you can help.
Re: Print a Rectangle on a Grid
Quote:
how to print the rectangle within the printGrid method.
Lines print top to bottom. Your code would need to build the output line by line starting at the top and working down.
Take a piece of graph paper and write down what needs to go on each line, then write a program to print the lines.
Re: Print a Rectangle on a Grid
Is there any reason you wouldn't just use drawRect() or fillRect?
Re: Print a Rectangle on a Grid
Probably because it's a command line program without a GUI?
db
Re: Print a Rectangle on a Grid
It's funny though - who uses those nowadays? Processing power & memory are so cheap!!
Re: Print a Rectangle on a Grid
This is a student looping exercise. The objective is for the OP to use some loops to solve a problem.
Re: Print a Rectangle on a Grid
As a former student, I just wish those examples were more related to this world. No wonder no one wants to become a programmer when they're forced to do all the boring, useless stuff like that in school.
Re: Print a Rectangle on a Grid
Animation, thats what my computing teacher gave my class as tasks.
Come on now, Anything is more interesting than command-line graphics.
Re: Print a Rectangle on a Grid
Maybe you haven't tried Visual Basic. The editor is so good, you can forget about writing GUI code. It's not buggy like all the Java GUI editors; it does exactly what its supposed to do.
Anyway, we only had to animate a picture, you don't need to write complicated java code for that either.
Re: Print a Rectangle on a Grid
Okay? I didn't say it was? Besides you're going on a tangent here.
If you really wanted to know, we were coding the logic (as newbies, writing basic loops) whilst not having to worry so much about the GUI coding.
Visual Studio, of course, has a source code editor.
Re: Print a Rectangle on a Grid
Lol got kinda off-topic here, but okay.
Quote:
Originally Posted by
Norm
Lines print top to bottom. Your code would need to build the output line by line starting at the top and working down.
Take a piece of graph paper and write down what needs to go on each line, then write a program to print the lines.
Hmm, I think I can get a sense of how to do this now. Although, in order to print rectangles on a coordinate that's not in increments of 5, I'll have to increment the height and width by 1, print "|" until I reach 5, 10, 15, 20. Work my way from the top to the bottom. So if it's in position (10, 15), I'll need to print the 20 first, and then 4 |s, print 15. The width is 10, so I'll have to make sure to print 10 spaces, and then print the desired width of the rectangle. Make a loop for that, and then just finish it off with the horizontal line.
Sorry, I'm just trying to understand this by typing out all my thoughts. I'll probably write an algorithm to organize the program better.
Thanks for the help.