Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree2Likes

Thread: Problem with Recangle Array

  1. #1
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Question 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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!

  4. #4
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Problem with Recangle Array

    Here is my full program code :
    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]
        
    }
    So i expected an result when compiling this a result like this :
    Problem with Recangle Array-expected.png

    But it gave me this :
    Problem with Recangle Array-snapshot1.png

    How do i get the result i want? thx Cemaje

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

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

  6. #6
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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.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 :
    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);
        	}
        }
        
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Problem with Recangle Array

    the result is not what i want :
    Please explain.
    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.

  10. #10
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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. #11
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Problem with Recangle Array

    the screen is 640 by 480

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

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

  13. #13
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Problem with Recangle Array

    Here is what the game looks like :
    Problem with Recangle Array-javagame.png
    But i cant or dont know how to make it fill the entire screen of those little squares thx Cemahe

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

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

  15. #15
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

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

  17. #17
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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. #18
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default 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. #19
    Cemahe is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default 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. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. problem with array
    By adrian110288 in forum New To Java
    Replies: 2
    Last Post: 02-09-2012, 04:15 PM
  2. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  3. array problem
    By jabo in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 10:54 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:13 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
  •