Results 1 to 8 of 8
  1. #1
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    22
    Rep Power
    0

    Default Trying to make a grid

    Hello,
    I'm trying to make a grid with 5's in it. But it only shows the lines and only one 5 in the upper left corner. "speelveld" is a 3 by 3 2-dimensional array with 5's in all places.This is my code:


    Java Code:
    public void display(Graphics g){
        
        for (int rij=0; rij<speelveld[0].length; rij++)
        for (int kolom=0; kolom<speelveld[0].length; kolom++)
        
        g.drawRect(startX+(kolomSprong * kolom),(startY-rijSprong) + (rijSprong * rij),kolomSprong,rijSprong);
        g.drawString(Integer.toString(speelveld[rij][kolom]),(startX +(kolomSprong/2)+ (kolomSprong * kolom)),startY-(rijSprong/2) + (rijSprong *rij));

    if I put the g.drawString line in first, then it shows all the 5's and only one rectangle in the upper left corner.
    Anyone any idea why I dont get both the rectangles and the fives?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Trying to make a grid

    Near impossible to say. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. A single snippet out of context doesn't show what might be wrong.

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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trying to make a grid

    Check if 'rijSprong' and 'kolomSprong' have sensible values.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    22
    Rep Power
    0

    Default Re: Trying to make a grid

    In reaction to Darryl I made my program as small as possible and I can post it as a whole. The problem/question is the same, if I put g.drawRect first it makes the lines but not the 5's. If I put g.drawString first it puts in the 5's but not the lines. Here is the code:


    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Boterkaas2 extends Applet {
      
      private Veld boter;
      
      
      public void init() {
        boter = new Veld(3,3);
      }
      
      public void paint(Graphics g) {
        boter.display(g);
      }
      
      
      class Veld  {
        
        
        private int[][] speelveld;
        int startX = 80;
        int startY = 80;
        int rijSprong = 30;
        int kolomSprong = 30;
        public int rij;
        public int kolom;
        
        
        
        public Veld(int rijen, int kolommen) {
          speelveld = new int [rijen][kolommen];
          for (int rij=0; rij<rijen;rij++)
          for (int kolom=0; kolom<kolommen;kolom++)
          speelveld[rij][kolom]=5;
          
          
        }
        
        public void display(Graphics g){
          
          for (int rij=0; rij<speelveld[0].length; rij++)
          for (int kolom=0; kolom<speelveld[0].length; kolom++)
          
         g.drawString(Integer.toString(speelveld[rij][kolom]),
          (startX + (kolomSprong * kolom)),startY + (rijSprong *rij));
          g.drawRect(startX+(kolomSprong * kolom),
          (startY-rijSprong) + (rijSprong * rij),kolomSprong,rijSprong);    }
      }
     }
    Last edited by Maarten; 07-28-2012 at 05:53 PM.

  5. #5
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Trying to make a grid

    you have to put the drawString and the drawRect inside the for loops. and then you should just have to fix the positions

  6. #6
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    22
    Rep Power
    0

    Default Re: Trying to make a grid

    solved, thank you!

  7. #7
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Trying to make a grid

    Your welcome. Remember that i you don't use the "{" and the "}" it will ONLY loop the next line and nothing more than that

  8. #8
    Maarten is offline Member
    Join Date
    Dec 2011
    Location
    Utrecht
    Posts
    22
    Rep Power
    0

    Default Re: Trying to make a grid

    yes its actually something I rememberd after your post, wont forget it again now, cheers

Similar Threads

  1. how to make grid number ?
    By nikiiz in forum New To Java
    Replies: 6
    Last Post: 05-28-2012, 05:02 PM
  2. Open Grid Scheduler/Grid Engine
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-27-2012, 07:00 PM
  3. Replies: 3
    Last Post: 03-23-2012, 07:43 PM
  4. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  5. creating a make grid method
    By Matt198525 in forum New To Java
    Replies: 13
    Last Post: 10-29-2010, 02:47 AM

Posting Permissions

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