Results 1 to 15 of 15
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Need help with 2D arrays

    Hi there.

    I am trying to create a 2D array that will originally have all locations in the array set to the same object. Is there any easy way to do this, other than using something repetitive like the code below? I'm sure there must be, but I can't figure it out. Please help!!!
    Java Code:
    Array[0][1]= Object;
    Array[0][2]= Object;
    Array[0][3]= Object;
    Array[0][4]= Object;
    Array[0][5]= Object;
    Array[1][0]= Object;
    Array[2][0]= Object;
    Array[3][0]= Object;
    Array[4][0]= Object;
    Array[5][0]= Object;
    Last edited by Singing Boyo; 03-20-2009 at 09:26 PM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, nested loops immediately comes to mind.

    Your array indices in your code snippet are wrong. If you did it the long way, you'd do it like so:
    Java Code:
    Array[0][0]= Object;
    Array[0][1]= Object;
    Array[0][2]= Object;
    Array[0][3]= Object;
    Array[0][4]= Object;
    Array[1][0]= Object;
    Array[1][1]= Object;
    Array[1][2]= Object;
    Array[1][3]= Object;
    Array[1][4]= Object;

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Exclamation Something Else...

    I solved my original problem(using nested loops), and ran into another one...

    if my Array is created using
    Java Code:
    int Array[][] = new int [20][20]
    Shouldn't Array[20][20] be the lower right corner of the array? When I try to set the area or use a System.out.println() statement to check what it is set to, I get the java.lang.ArrayIndexOutOfBoundsException. I'm sure there is something I don't understand, so could someone explain it to me?
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... does java find locations in arrays by the upper left corner of the area? eg. if I had a 8 by 8 chess board, the bottom right square would be ChessBoard[7][7] in java? plz tell me if I'm right
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Ok, I looked at more examples, and figured out my problem... and now I have another one.

    Is there any way to create a large number of JLabels quickly? I want a 20 by 20 Array of JLabels. Using the standard JLabel myLabel = new JLabel method to create 400 JLabels would take forever. Is there a simpler way of doing this?
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Is there any way to create a large number of JLabels quickly?
    what's so difficult about using a nested loop. just use nested loop to create 400 of them.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    400 JLabels? can nested loops actually do that? I know you can use loops to draw Graphics objects and set Arrays. I've never heard of them being used for JLabels though... could you post an example?
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Java Code:
    JLabel[][] jlbArray = new JLabel[20][20]
    for(int i =0; ...)
      for(int j=0; ...)
        jlbArray[i][j] = new JLabel();
    only problem is labeling it. i figured anyone who wants 400 labels is prob using it as demo.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by angryboy View Post
    Java Code:
    JLabel[][] jlbArray = new JLabel[20][20]
    for(int i =0; ...)
      for(int j=0; ...)
        jlbArray[i][j] = new JLabel();
    I don't need to do much with the JLabels, but I do need to modify them when something changes on an Array. If all the JLabels all have the same possible images on them, is it possible to create something relatively simple to do this?
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    be creative. pass in another array.
    Java Code:
    JLabel[][] jlbArray = new JLabel[20][20]
    for(int i =0; ...)
      for(int j=0; ...)
        jlbArray[i][j] = new JLabel(s[i][j]);
    where s is a 2d array of Strings or Icon.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Can't make anything but a blank frame appear now...

    Ok... so I am creating the arrays and I can trace them with println statements, but I can't get the JLabels to show on the screen, all I get is a blank frame. Any idea what I am doing wrong? All ImageIcon strings are declared in a separate class,(Icons) but as I said, it traces to the correct file when checked with println, so no problems there. This is my code:
    Java Code:
    package testarea;
    
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    public class JLabelArray extends JPanel{
    	private int test1, test2;
    	private ImageIconArray TestNavArray = new ImageIconArray();
    	protected JLabel[][] Array = new JLabel[20][20];
    	
    	
    	public JLabelArray(){
    		
    		setLayout(null);
    		for (test1 = 0; test1 < 20; test1++)
    			for (test2 = 0; test2 <20; test2++)
    				Array[test1][test2] = new JLabel(TestNavArray.IArray[test1][test2]);
    			System.out.println(Array[5][5]);
    			for (test1 = 0; test1 < 20; test1++)
    				for (test2 = 0; test2 <20; test2++)
    		add(Array[test1][test2]);//<<<<<new code!!!>>>>>
    		}
    	public static void main(String[]args){
    		
    		JLabelArray testLoop = new JLabelArray();
    		JPanel testPanel = new JPanel();
    		testPanel.add(testLoop);
    		JFrame testFrame = new JFrame();
    		testFrame.setContentPane(testPanel);
    		testFrame.setSize(600,600);
    		testFrame.setVisible(true);
    		
    	}
    }
    Java Code:
    package testarea;
    
    import javax.swing.ImageIcon;
    
    
    public class ImageIconArray {
    
    	ImageIcon[][]IArray= new ImageIcon[20][20];	
    
    	public ImageIconArray(){
    	
                    ImageIcon myIcon = new ImageIcon();
    
    		for (int setter =0; setter < 20; setter++)
    			for (int setter2 = 0; setter2 < 20; setter2++)	
    				IArray[setter][setter2]= myIcon;
    	
                    //All the statements below returned the correct filepath to the
                    //image I wanted
                    System.out.println(IArray[5][5]);
    		System.out.println(IArray[6][5]);
    		System.out.println(IArray[2][5]);
    		System.out.println(IArray[9][5]);
    		System.out.println(IArray[15][5]);	
    	}
    
    }
    Last edited by Singing Boyo; 03-22-2009 at 12:27 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    the code above does not compile. Too many dependencies that we are not privy to.

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    couple of things. you did not add any of the JLabel in the array to the panel. also, if you're trying to fill the array w/ the same object reference, just use Arrays.fill(Object[] a, Object val).

    and, like fubarable pointed out: "Too many dependencies", what is pics.Icons ?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #14
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    pics.Icon is a class I used to set all the Icons for the entire project. I was finding that I had a lot of classes needed the same Images, and I found it easier to create them all in one class. I edited my code so it should compile for you. I also added the array to my JPanel. I did make that addition obvious. Still comes up blank and my println statements find the right file.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  15. #15
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    ImageIcon myIcon = new ImageIcon();
    its not showing up because you don't have any images added.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2009, 12:43 AM
  2. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 10:55 PM
  3. Replies: 0
    Last Post: 01-22-2008, 12:05 PM
  4. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 01:54 PM
  5. Object locations via grid coordinates HELP.
    By deadman_uk in forum New To Java
    Replies: 4
    Last Post: 11-18-2007, 09:32 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
  •