# Thread: Problem with Recangle Array

1. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Problem with Recangle Array

Hello guys, i am new to this forum , i came here to get and hopefully help. I got an error in my code :
Java 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

2. ## Re: Problem with Recangle Array

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?

3. ## Re: Problem with Recangle Array

What error do you have?

4. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: Problem with Recangle Array

Here is my full program code :
Java Code:
```
import java.awt.*;
import java.awt.event.KeyEvent;
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;

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);
}

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) {
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 :

But it gave me this :

How do i get the result i want? thx Cemaje

5. ## 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.

6. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## 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

7. ## Re: Problem with Recangle Array

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.

8. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

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

Java Code:
```import java.awt.*;
import java.awt.event.KeyEvent;
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;

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);
}

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) {
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 :
Java Code:
```import java.awt.*;
import java.awt.event.KeyEvent;
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;

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);
}

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) {
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);
}
}

}```

9. ## Re: Problem with Recangle Array

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.

10. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## 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 :)

11. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: Problem with Recangle Array

the screen is 640 by 480

12. ## Re: Problem with Recangle Array

What does the program do now? How is it different from you want it to do?

13. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## Re: Problem with Recangle Array

Here is what the game looks like :

But i cant or dont know how to make it fill the entire screen of those little squares thx Cemahe

14. ## Re: Problem with Recangle Array

What do you want to change about the image that you posted in post#13?

15. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## 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

16. ## Re: Problem with Recangle Array

What happens when you do what I suggested in post #5?

17. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

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

18. Member
Join Date
Mar 2012
Posts
41
Rep Power
0

## Re: Problem with Recangle Array

Whats this??? .y
Java 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
Java Code:
`Graphics g = containerBoxes[i].getGraphics();`
then
Java Code:
```g.setColor(Color.WHITE);
g.drawRect(x,y,width,height);
g.fillRect(x,y,width,height);```

19. Member
Join Date
Mar 2012
Posts
10
Rep Power
0

## 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

20. ## 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:
Java Code:
`//            g.fillRect(containerBoxes[i].x, containerBoxes[i].y, containerBoxes[i].width, containerBoxes[i].height);`

Uncomment this line 155:
Java 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.

Page 1 of 2 12 Last

#### Posting Permissions

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