Results 1 to 11 of 11
  1. #1
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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:

    Java 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.
    Last edited by Zora; 03-13-2012 at 09:27 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Print a Rectangle on a Grid

    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.

  3. #3
    d2r123 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Print a Rectangle on a Grid

    Is there any reason you wouldn't just use drawRect() or fillRect?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Print a Rectangle on a Grid

    Probably because it's a command line program without a GUI?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Print a Rectangle on a Grid

    It's funny though - who uses those nowadays? Processing power & memory are so cheap!!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default 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.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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.

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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.

  11. #11
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Print a Rectangle on a Grid

    Lol got kinda off-topic here, but okay.

    Quote Originally Posted by Norm View Post
    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.

Similar Threads

  1. 2D grid
    By ranss41 in forum New To Java
    Replies: 0
    Last Post: 01-06-2012, 04:41 PM
  2. Rectangle class again
    By toppcon in forum New To Java
    Replies: 2
    Last Post: 05-05-2011, 03:25 AM
  3. Replies: 6
    Last Post: 11-30-2010, 11:29 PM
  4. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •