Results 1 to 20 of 31
Thread: Problem with Recangle Array
- 03-22-2012, 01:57 PM #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 :
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.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); } }
Any one want to help me? Thanks in advanced! Cemahe
- 03-22-2012, 03:42 PM #2
Re: Problem with Recangle Array
Please explain. Copy and paste here the full text of the error message.I got an error in my code :
What is the problem with the posted code?If you don't understand my response, don't ignore it, ask a question.
- 03-22-2012, 03:45 PM #3
Re: Problem with Recangle Array
What error do you have?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-23-2012, 01:01 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Re: Problem with Recangle Array
Here is my full program code :
So i expected an result when compiling this a result like this :Java 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] }

But it gave me this :

How do i get the result i want? thx Cemaje
- 03-23-2012, 01:13 AM #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.If you don't understand my response, don't ignore it, ask a question.
- 03-23-2012, 12:10 PM #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
- 03-23-2012, 12:17 PM #7
Re: Problem with Recangle Array
Please explain. What does the code do now? What do you want changed?another bug appeared
Post the full text of any error messages here.If you don't understand my response, don't ignore it, ask a question.
- 03-23-2012, 01:16 PM #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 :
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.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); } } }
Java 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, 01:18 PM #9
Re: Problem with Recangle Array
Please explain.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.If you don't understand my response, don't ignore it, ask a question.
- 03-23-2012, 01:50 PM #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 :)
- 03-23-2012, 01:51 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Re: Problem with Recangle Array
the screen is 640 by 480
- 03-23-2012, 03:13 PM #12
Re: Problem with Recangle Array
What does the program do now? How is it different from you want it to do?
If you don't understand my response, don't ignore it, ask a question.
- 03-24-2012, 12:58 AM #13
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
- 03-24-2012, 01:05 AM #14
Re: Problem with Recangle Array
What do you want to change about the image that you posted in post#13?
If you don't understand my response, don't ignore it, ask a question.
- 03-24-2012, 01:14 AM #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
- 03-24-2012, 01:19 AM #16
Re: Problem with Recangle Array
What happens when you do what I suggested in post #5?
If you don't understand my response, don't ignore it, ask a question.
- 03-24-2012, 09:13 AM #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.
- 03-24-2012, 09:31 AM #18
Member
- Join Date
- Mar 2012
- Posts
- 41
- Rep Power
- 0
Re: Problem with Recangle Array
Whats this??? .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.Java Code:containerBoxes[i].y
ALSO
g. is the individual Graphics object obtained from that particular Rectangle.
so it should start for each paint of a rectangle as
thenJava Code:Graphics g = containerBoxes[i].getGraphics();
Java Code:g.setColor(Color.WHITE); g.drawRect(x,y,width,height); g.fillRect(x,y,width,height);
- 03-24-2012, 11:15 AM #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
- 03-24-2012, 12:41 PM #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:
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)Java Code://g.drawString(containerBoxes[i].x + " " + containerBoxes[i].y,containerBoxes[i].y,containerBoxes[i].x);
This will show you the x,y values used to draw the boxes.If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
problem with array
By adrian110288 in forum New To JavaReplies: 2Last Post: 02-09-2012, 03:15 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
array problem
By jabo in forum New To JavaReplies: 2Last Post: 03-31-2010, 09:54 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks