# Problem with Recangle Array

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-22-2012, 02:57 PM
Cemahe
Problem with Recangle Array
:(party): Hello guys, i am new to this forum :(party):,:(wait): i came here to get and hopefully help. I got an error in my code :
Code:

``` // Rectangle array  Rectangle containerBoxes[] = new Rectangle[299];    public void setContainerBoxes() {             int x,y;             x = 0;             y = -32;                         for(int i=0; i<299; i++) {                     if(x == 608) {                             x = 0;                             y += 32;                     }                     if(x + 32< 640) {                             x += 32;                     }                     containerBoxes[i] = new Rectangle(x,y,32,32);             }     }         public void drawContainerBoxes(Graphics g) {             for(int i=0; i<299; i++) {                     g.fillRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);                     g.setColor(Color.WHITE);                                        g.drawRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);             }     }```
Basicly all i want it to do i create 300 rectangles with everyone a x and y coordinates different from the other and the draw it on my JFrame window i have set up.
Any one want to help me? Thanks in advanced! Cemahe
• 03-22-2012, 04:42 PM
Norm
Re: Problem with Recangle Array
Quote:

I got an error in my code :
Please explain. Copy and paste here the full text of the error message.

What is the problem with the posted code?
• 03-22-2012, 04:45 PM
KevinWorkman
Re: Problem with Recangle Array
What error do you have?
• 03-23-2012, 02:01 AM
Cemahe
Re: Problem with Recangle Array
Here is my full program code :
Code:

``` import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class Main extends JFrame {         //Double buffering     Image dbImage;     Graphics dbg;             boolean gameStarted = false;     boolean startHover;     boolean quitHover;         //Menu Buttons     Rectangle startButton = new Rectangle(250, 200, 100, 25);     Rectangle quitButton = new Rectangle(250, 250, 100, 25);     Rectangle containerBoxes[] = new Rectangle[299];     //Variables for screen size     int     GWIDTH = 640,     GHEIGHT = 480;     //Dimension of GWIDTH*GHEIGHT     Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);         //Create constructor to spawn window     public Main(){             setContainerBoxes();         this.setTitle("Game Creator");         this.setSize(screenSize);         this.setResizable(false);         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         this.setVisible(true);         this.setBackground(Color.DARK_GRAY);         this.addKeyListener(new KeyHandler());         this.addMouseListener(new MouseHandler());         this.addMouseMotionListener(new MouseHandler());     }         public void startGame() { gameStarted = true; }         public static void main(String[] args){         Main m = new Main();     }         @Override     public void paint(Graphics g){         dbImage = createImage(getWidth(), getHeight());         dbg = dbImage.getGraphics();         draw(dbg);         g.drawImage(dbImage, 0, 0, this);     }     public void draw(Graphics g){                     if(!gameStarted) {                     //Menu                 g.setFont(new Font("Alien Encounters", Font.BOLD, 26));                 g.setColor(Color.WHITE);                 g.drawString("Game Creator", 200, 150);                 if(!startHover) {                         g.setColor(Color.CYAN);                 } else {                         g.setColor(Color.PINK);                 }                 g.fillRect(startButton.x, startButton.y, startButton.width, startButton.height);                 g.setFont(new Font("Arial", Font.BOLD, 12));                 g.setColor(Color.GRAY);                 g.drawString("Start Game", startButton.x+20, startButton.y+17);                 if(!quitHover) {                         g.setColor(Color.CYAN);                 } else {                         g.setColor(Color.PINK);                 }                 g.fillRect(quitButton.x, quitButton.y, quitButton.width, quitButton.height);                 g.setColor(Color.GRAY);                 g.drawString("Quit Game", quitButton.x+20, quitButton.y+17);             } else {                     // Game Drawing                     drawContainerBoxes(g);             }         repaint();     }         ////////EVENT LISTENER CLASSES/////////     public class KeyHandler extends KeyAdapter {         @Override         public void keyPressed(KeyEvent e){         }         @Override         public void keyReleased(KeyEvent e){                         }     }     public class MouseHandler extends MouseAdapter {         @Override         public void mouseMoved(MouseEvent e){                 int mx, my;             mx = e.getX();             my = e.getY();             if(mx > startButton.x && mx < startButton.x + startButton.width &&                             my > startButton.y && my < startButton.y + startButton.height) {                     startHover = true;             } else                     startHover = false;             if(mx > quitButton.x && mx < quitButton.x + quitButton.width &&                             my > quitButton.y && my < quitButton.y + quitButton.height) {                     quitHover = true;             } else                     quitHover = false;         }         @Override         public void mousePressed(MouseEvent e){             int mx, my;             mx = e.getX();             my = e.getY();             if(mx > startButton.x && mx < startButton.x + startButton.width &&                             my > startButton.y && my < startButton.y + startButton.height) {                     startGame();             }         }     }     ///////END EVENT LISTENER CLASSES/////         [I][FONT=Garamond]public void setContainerBoxes() {             int x,y;             x = 0;             y = -32;                         for(int i=0; i<299; i++) {                     if(x == 608) {                             x = 0;                             y += 32;                     }                     if(x + 32< 640) {                             x += 32;                     }                     containerBoxes[i] = new Rectangle(x,y,32,32);             }     }         public void drawContainerBoxes(Graphics g) {             for(int i=0; i<299; i++) {                     g.fillRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);                     g.setColor(Color.WHITE);                                        g.drawRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);             }     }[/i][/FONT][i][/I]     }```
So i expected an result when compiling this a result like this :
Attachment 3317

But it gave me this :
Attachment 3318

How do i get the result i want? thx Cemaje
• 03-23-2012, 02:13 AM
Norm
Re: Problem with Recangle Array
Try taking out the call to fillRect() if you don't want solid color filling.

Try debugging the code by printing out the values of x and y for all the rectangles you are drawing to see what the values are so you can change the formula to create the values for x and y that you want used.
• 03-23-2012, 01:10 PM
Cemahe
Re: Problem with Recangle Array
Thanx Norm,
I did what you did, and i saw the problem, then i fixed and try again then another bug appeared so i am confused because i tried to fix it but couldn't, so can you help me just a little bit more please? thx Cemahe
• 03-23-2012, 01:17 PM
Norm
Re: Problem with Recangle Array
Quote:

another bug appeared
Please explain. What does the code do now? What do you want changed?
Post the full text of any error messages here.
• 03-23-2012, 02:16 PM
Cemahe
Re: Problem with Recangle Array
Well it is not an error but it is an bug, i mean the result is not what i want :

Code:

```import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class Main extends JFrame {         //Double buffering     Image dbImage;     Graphics dbg;             boolean gameStarted = false;     boolean startHover;     boolean quitHover;         //Menu Buttons     Rectangle startButton = new Rectangle(250, 200, 100, 25);     Rectangle quitButton = new Rectangle(250, 250, 100, 25);     Rectangle containerBoxes[] = new Rectangle[299];     //Variables for screen size     int     GWIDTH = 640,     GHEIGHT = 480;     //Dimension of GWIDTH*GHEIGHT     Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);         //Create constructor to spawn window     public Main(){             setContainerBoxes();         this.setTitle("Game Creator");         this.setSize(screenSize);         this.setResizable(false);         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         this.setVisible(true);         this.setBackground(Color.DARK_GRAY);         this.addKeyListener(new KeyHandler());         this.addMouseListener(new MouseHandler());         this.addMouseMotionListener(new MouseHandler());     }         public void startGame() { gameStarted = true; }         public static void main(String[] args){         Main m = new Main();     }         @Override     public void paint(Graphics g){         dbImage = createImage(getWidth(), getHeight());         dbg = dbImage.getGraphics();         draw(dbg);         g.drawImage(dbImage, 0, 0, this);     }     public void draw(Graphics g){                     if(!gameStarted) {                     //Menu                 g.setFont(new Font("Alien Encounters", Font.BOLD, 26));                 g.setColor(Color.WHITE);                 g.drawString("Game Creator", 200, 150);                 if(!startHover) {                         g.setColor(Color.CYAN);                 } else {                         g.setColor(Color.PINK);                 }                 g.fillRect(startButton.x, startButton.y, startButton.width, startButton.height);                 g.setFont(new Font("Arial", Font.BOLD, 12));                 g.setColor(Color.GRAY);                 g.drawString("Start Game", startButton.x+20, startButton.y+17);                 if(!quitHover) {                         g.setColor(Color.CYAN);                 } else {                         g.setColor(Color.PINK);                 }                 g.fillRect(quitButton.x, quitButton.y, quitButton.width, quitButton.height);                 g.setColor(Color.GRAY);                 g.drawString("Quit Game", quitButton.x+20, quitButton.y+17);             } else {                     // Game Drawing                     drawContainerBoxes(g);             }         repaint();     }         ////////EVENT LISTENER CLASSES/////////     public class KeyHandler extends KeyAdapter {         @Override         public void keyPressed(KeyEvent e){         }         @Override         public void keyReleased(KeyEvent e){                         }     }     public class MouseHandler extends MouseAdapter {         @Override         public void mouseMoved(MouseEvent e){                 int mx, my;             mx = e.getX();             my = e.getY();             if(mx > startButton.x && mx < startButton.x + startButton.width &&                             my > startButton.y && my < startButton.y + startButton.height) {                     startHover = true;             } else                     startHover = false;             if(mx > quitButton.x && mx < quitButton.x + quitButton.width &&                             my > quitButton.y && my < quitButton.y + quitButton.height) {                     quitHover = true;             } else                     quitHover = false;         }         @Override         public void mousePressed(MouseEvent e){             int mx, my;             mx = e.getX();             my = e.getY();             if(mx > startButton.x && mx < startButton.x + startButton.width &&                             my > startButton.y && my < startButton.y + startButton.height) {                     startGame();             }         }     }     ///////END EVENT LISTENER CLASSES/////         public void setContainerBoxes() {             int x,y;             x = 0;             y = 0;                         for(int i=0; i<299; i++) {                     if(y == 608) {                             y = 0;                             x += 32;                     }                     if(y + 32< 640) {                             y += 32;                     }                     containerBoxes[i] = new Rectangle(x,y,32,32);             }     }         public void drawContainerBoxes(Graphics g) {             for(int i=0; i<299; i++) {                     /*g.fillRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);                     g.setColor(Color.WHITE);                                        g.drawRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);*/                     g.drawString(containerBoxes[i].x + " " + containerBoxes[i].y, containerBoxes[i].y,containerBoxes[i].x);             }     }     }```
What i did is in setContainerBoxes() i changed the y into x and x into y because with your debug thing it made me realise something but it still doesnt work :( it just dont do what i say to do ( i think ) i dont know why. If you dont undersand copy the code below and you will understand hopefully im sorry but it is hard to explain :
Code:

```import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class Main extends JFrame {         //Double buffering     Image dbImage;     Graphics dbg;             boolean gameStarted = false;     boolean startHover;     boolean quitHover;         //Menu Buttons     Rectangle startButton = new Rectangle(250, 200, 100, 25);     Rectangle quitButton = new Rectangle(250, 250, 100, 25);     Rectangle containerBoxes[] = new Rectangle[299];     //Variables for screen size     int     GWIDTH = 640,     GHEIGHT = 480;     //Dimension of GWIDTH*GHEIGHT     Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);         //Create constructor to spawn window     public Main(){             setContainerBoxes();         this.setTitle("Game Creator");         this.setSize(screenSize);         this.setResizable(false);         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         this.setVisible(true);         this.setBackground(Color.DARK_GRAY);         this.addKeyListener(new KeyHandler());         this.addMouseListener(new MouseHandler());         this.addMouseMotionListener(new MouseHandler());     }         public void startGame() { gameStarted = true; }         public static void main(String[] args){         Main m = new Main();     }         @Override     public void paint(Graphics g){         dbImage = createImage(getWidth(), getHeight());         dbg = dbImage.getGraphics();         draw(dbg);         g.drawImage(dbImage, 0, 0, this);     }     public void draw(Graphics g){                     if(!gameStarted) {                     //Menu                 g.setFont(new Font("Alien Encounters", Font.BOLD, 26));                 g.setColor(Color.WHITE);                 g.drawString("Game Creator", 200, 150);                 if(!startHover) {                         g.setColor(Color.CYAN);                 } else {                         g.setColor(Color.PINK);                 }                 g.fillRect(startButton.x, startButton.y, startButton.width, startButton.height);                 g.setFont(new Font("Arial", Font.BOLD, 12));                 g.setColor(Color.GRAY);                 g.drawString("Start Game", startButton.x+20, startButton.y+17);                 if(!quitHover) {                         g.setColor(Color.CYAN);                 } else {                         g.setColor(Color.PINK);                 }                 g.fillRect(quitButton.x, quitButton.y, quitButton.width, quitButton.height);                 g.setColor(Color.GRAY);                 g.drawString("Quit Game", quitButton.x+20, quitButton.y+17);             } else {                     // Game Drawing                     drawContainerBoxes(g);             }         repaint();     }         ////////EVENT LISTENER CLASSES/////////     public class KeyHandler extends KeyAdapter {         @Override         public void keyPressed(KeyEvent e){         }         @Override         public void keyReleased(KeyEvent e){                         }     }     public class MouseHandler extends MouseAdapter {         @Override         public void mouseMoved(MouseEvent e){                 int mx, my;             mx = e.getX();             my = e.getY();             if(mx > startButton.x && mx < startButton.x + startButton.width &&                             my > startButton.y && my < startButton.y + startButton.height) {                     startHover = true;             } else                     startHover = false;             if(mx > quitButton.x && mx < quitButton.x + quitButton.width &&                             my > quitButton.y && my < quitButton.y + quitButton.height) {                     quitHover = true;             } else                     quitHover = false;         }         @Override         public void mousePressed(MouseEvent e){             int mx, my;             mx = e.getX();             my = e.getY();             if(mx > startButton.x && mx < startButton.x + startButton.width &&                             my > startButton.y && my < startButton.y + startButton.height) {                     startGame();             }         }     }     ///////END EVENT LISTENER CLASSES/////         public void setContainerBoxes() {             int x,y;             x = 0;             y = 0;                         for(int i=0; i<299; i++) {                     if(y == 608) {                             y = 0;                             x += 32;                     }                     if(y + 32< 640) {                             y += 32;                     }                     containerBoxes[i] = new Rectangle(x,y,32,32);             }     }         public void drawContainerBoxes(Graphics g) {             for(int i=0; i<299; i++) {                     g.fillRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);                     g.setColor(Color.WHITE);                                        g.drawRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);                     //g.drawString(containerBoxes[i].x + " " + containerBoxes[i].y, containerBoxes[i].y,containerBoxes[i].x);             }     }     }```
• 03-23-2012, 02:18 PM
Norm
Re: Problem with Recangle Array
Quote:

the result is not what i want :
For example:
The lines are too short, I want lines all the way across.
The lines are the wrong color, I want blue lines.
There are only three lines, I want 50 lines.
• 03-23-2012, 02:50 PM
Cemahe
Re: Problem with Recangle Array
Well i want to have 300 hundred rectangles to cover the whole screen, each rectangle is 32 by 32, white rectangle and black outline if possible please :)
• 03-23-2012, 02:51 PM
Cemahe
Re: Problem with Recangle Array
the screen is 640 by 480
• 03-23-2012, 04:13 PM
Norm
Re: Problem with Recangle Array
What does the program do now? How is it different from you want it to do?
• 03-24-2012, 01:58 AM
Cemahe
Re: Problem with Recangle Array
Here is what the game looks like :
Attachment 3327
But i cant or dont know how to make it fill the entire screen of those little squares thx Cemahe
• 03-24-2012, 02:05 AM
Norm
Re: Problem with Recangle Array
What do you want to change about the image that you posted in post#13?
• 03-24-2012, 02:14 AM
Cemahe
Re: Problem with Recangle Array
All i need is in the picture above, that the squares fill the complete Jframe starting from 0,0 to 640, 480. thx Cemahe
• 03-24-2012, 02:19 AM
Norm
Re: Problem with Recangle Array
What happens when you do what I suggested in post #5?
• 03-24-2012, 10:13 AM
Cemahe
Re: Problem with Recangle Array
Well it just convert the square into the x and y of the square but i dont see the new bug sorry.
• 03-24-2012, 10:31 AM
nicephotog
Re: Problem with Recangle Array
Whats this??? .y
Code:

`containerBoxes[i].y`
"containerBoxes" is a variable representing a "Rectangle" object type. While it has requirement of an "x" and a "y" it has to be assigned through a method call not through direct class field or member variable.

ALSO

g. is the individual Graphics object obtained from that particular Rectangle.

so it should start for each paint of a rectangle as
Code:

`Graphics g = containerBoxes[i].getGraphics();`
then
Code:

```g.setColor(Color.WHITE);            g.drawRect(x,y,width,height); g.fillRect(x,y,width,height);```
• 03-24-2012, 12:15 PM
Cemahe
Re: Problem with Recangle Array
can you give me the full code because when i try implementing it, i have an error for Graphics g = containerBoxes[i].getGraphics();
Thanks, Cemahe
• 03-24-2012, 01:41 PM
Norm
Re: Problem with Recangle Array
Graphics g = containerBoxes[i].getGraphics();
There is no reason to do that. Remove that statement.
Here is what I changed on line 152:
Code:

`//            g.fillRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);`

Uncomment this line 155:
Code:

`        //g.drawString(containerBoxes[i].x + " " + containerBoxes[i].y,containerBoxes[i].y,containerBoxes[i].x);`
and separate the String and call drawString on two lines to draw the x value on one line and the y value on the next line (add a little to the y value location so the y value is below the x value)

This will show you the x,y values used to draw the boxes.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last