Results 1 to 3 of 3
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Array elements have the same co-ordinates

    Hey im following a simple game tutorial online to increase my java knowledge.

    i have this "shop" which has 8 buttons, which is an array on 8 rectangle objects.

    The issue i have is that when i loop through the array creating the new elements, adding a specified spacing to the x co ordinate of each rectangle, the final outcome is 8 rectangles with the same X co ordinates, so it only "draws" 1 rectangle


    Screen is a Class, which is just a 700x600 JFrame
    Java Code:
    public class Shop {
    
    	public static int shopWidth = 8;
    
    	public static int buttonSize = 46;
    
    	public static int spacing = 5;
    
    	public Rectangle [] towers = new Rectangle [shopWidth];
    
    
    	public Shop(){
    
    		define();
    	}
    
    
    	public void define() {
    		for(int i = 0; i < towers.length; i ++){
    			towers[i] = new Rectangle((Screen.myWidth/2) - (((shopWidth*buttonSize + spacing)/2)), 30, buttonSize + spacing, buttonSize + spacing); 
    			System.out.println("Square " + i + " x " + towers[i].getX() + " y " + towers[i].getY()); //DEBUGGING LINE
    		}
    	}
    
    	public void draw(Graphics g){
    
    		for(int i = 0; i < towers.length; i ++){
    
    			g.fillRect(towers[i].x, towers[i].y, towers[i].width, towers[i].height);
    
    		}
    	}

    OUTPUT FROM DEBUGGING LINE

    Square 0 x 161.0 y 30.0
    Square 1 x 161.0 y 30.0
    Square 2 x 161.0 y 30.0
    Square 3 x 161.0 y 30.0
    Square 4 x 161.0 y 30.0
    Square 5 x 161.0 y 30.0
    Square 6 x 161.0 y 30.0
    Square 7 x 161.0 y 30.0

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Array elements have the same co-ordinates

    ok quick update, ive managed to fix the initial problem by adding this:

    Java Code:
    public void define() {
    		for(int i = 0; i < towers.length; i ++){
    			towers[i] = new Rectangle((Screen.myWidth/2) - ((i*buttonSize + spacing)/2), 30, buttonSize + spacing, buttonSize + spacing); 
    
    			System.out.println("Square " + i + " x " + towers[i].getX() + " y " + towers[i].getY());
    		}
    	}
    now prints:

    Square 0 x 345.0 y 30.0
    Square 1 x 322.0 y 30.0
    Square 2 x 299.0 y 30.0
    Square 3 x 276.0 y 30.0
    Square 4 x 253.0 y 30.0
    Square 5 x 230.0 y 30.0
    Square 6 x 207.0 y 30.0
    Square 7 x 184.0 y 30.0


    New problem, it doesn't actually add the spacing of 5 between each element shop space any ideas?

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Array elements have the same co-ordinates

    fixed, needed to changed the code to this.

    towers[i] = new Rectangle((Screen.myWidth/2) - ((shopWidth*buttonSize)/2) + ((buttonSize + spacing) * i ), 30, buttonSize + spacing, buttonSize + spacing);

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2011, 12:38 AM
  2. keep the first N elements of an array only
    By aneuryzma in forum New To Java
    Replies: 13
    Last Post: 03-27-2011, 04:03 PM
  3. JPanel Co-ordinates
    By Bongeh in forum New To Java
    Replies: 6
    Last Post: 04-01-2010, 08:06 PM
  4. drawing in JPanel ,beyond the co-ordinates
    By anandnarayan891 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2009, 03:42 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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