Results 1 to 11 of 11
Thread: Print a Rectangle on a Grid
- 03-13-2012, 09:20 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
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:
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.Java Code:private static void printGrid(int x, int y, int w, int h) { printVerticalLines(); printHorizontalLines(); }
Hope you can help.Last edited by Zora; 03-13-2012 at 09:27 PM.
- 03-13-2012, 10:24 PM #2
Re: Print a Rectangle on a Grid
Lines print top to bottom. Your code would need to build the output line by line starting at the top and working down.how to print the rectangle within the printGrid method.
Take a piece of graph paper and write down what needs to go on each line, then write a program to print the lines.
- 03-13-2012, 10:57 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: Print a Rectangle on a Grid
Is there any reason you wouldn't just use drawRect() or fillRect?
- 03-13-2012, 11:03 PM #4
Re: Print a Rectangle on a Grid
Probably because it's a command line program without a GUI?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: Print a Rectangle on a Grid
It's funny though - who uses those nowadays? Processing power & memory are so cheap!!
- 03-14-2012, 01:15 AM #6
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.
- 03-14-2012, 07:41 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: Print a Rectangle on a Grid
Lol got kinda off-topic here, but okay.
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.
Similar Threads
-
2D grid
By ranss41 in forum New To JavaReplies: 0Last Post: 01-06-2012, 04:41 PM -
Rectangle class again
By toppcon in forum New To JavaReplies: 2Last Post: 05-05-2011, 03:25 AM -
does rectangle contain or overlap another rectangle?
By Xycose in forum New To JavaReplies: 6Last Post: 11-30-2010, 11:29 PM -
Wrong with Rectangle res = new Rectangle(0,0,0,0);???
By jiapei100 in forum AWT / SwingReplies: 3Last Post: 09-25-2010, 03:39 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks