Results 1 to 8 of 8
Thread: Trying to make a grid
- 07-28-2012, 04:30 PM #1
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
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?
- 07-28-2012, 05:11 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-28-2012, 05:17 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Trying to make a grid
Check if 'rijSprong' and 'kolomSprong' have sensible values.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-28-2012, 05:51 PM #4
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
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.
- 07-28-2012, 07:48 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 49
- Rep Power
- 0
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
- 07-28-2012, 08:00 PM #6
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
Re: Trying to make a grid
solved, thank you!
- 07-28-2012, 08:18 PM #7
Member
- Join Date
- Jun 2012
- Posts
- 49
- Rep Power
- 0
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
- 07-28-2012, 08:51 PM #8
Member
- Join Date
- Dec 2011
- Location
- Utrecht
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
how to make grid number ?
By nikiiz in forum New To JavaReplies: 6Last Post: 05-28-2012, 05:02 PM -
Open Grid Scheduler/Grid Engine
By java software in forum Java SoftwareReplies: 0Last Post: 03-27-2012, 07:00 PM -
How do I make a grid of buttons work within a border layout??
By jazzgnat in forum New To JavaReplies: 3Last Post: 03-23-2012, 07:43 PM -
can i make a program to make keyboard and mouse idle or not responding for 10 second
By 3ammary in forum Advanced JavaReplies: 4Last Post: 07-23-2011, 08:08 PM -
creating a make grid method
By Matt198525 in forum New To JavaReplies: 13Last Post: 10-29-2010, 02:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks