Connecting Tech Pros Worldwide Forums | Help | Site Map

Java Array HELP

Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#1: Jul 23 '09
I Have a question regarding arrays and filling a 4x4 grid..here is my function.
**this is the grid that is created....
////////////////////////////////////////////////////////////
//color grid

colorPanel= new JPanel();
colorPanel.setLayout(new GridLayout(4,8));
colorPanel.setPreferredSize(new Dimension (350,350));

colorPanel.setMaximumSize(new Dimension(350,350));
colorPanel.setBorder(new LineBorder(Color.black,2));
int idx=0;
for (int i =0; i<4; i++)
for (int j=0; j<4;j++)
colorPanel.add(gridCell[idx++],i,j);
//fillColorArray();

fillColorArray();
setGridColors();
mainPanel.add(colorPanel);
colorPanel.setBorder(BorderFactory.createTitledBor der("Color Grid"));
mainPanel.add(Box.createRigidArea(new Dimension(1000,0)));

//////////////////////////////////////////////////////////


*** this is the fill color array
private void fillColorArray(){

RED = (new Color(255,0,0));
YELLOW=(new Color(255,255,0));
ORANGE=(new Color(255,165,0));
GREEN=(new Color(0,100,0));
BLUE=(new Color(0,0,255));
PURPLE=(new Color(160,32,240));
BROWN=(new Color(165,42,42));
BLACK=(new Color(0,0,0));
PINK=(new Color(255,192,203));
GRAY=(new Color(190,190,190));
NAVY=(new Color(0,0,128));
SILVER=(new Color(139,134,130));
MAROON=(new Color(176,48,96));
WHITE=(new Color(255,255,255));
AQUA=(new Color(102,205,170));
FUCHISA=(new Color(238,130,238));

for(int i=0; i<16; i++) {

colorArray[i]=MAROON;
colorArray[i]=RED;
colorArray[i]=YELLOW;
colorArray[i]=GREEN;
colorArray[i]=BLUE;
....
...
..


}
}


* when i compile and run , the program just gives me one hudge color grid instead of a grid with 16 different colors..if anyone has any ideas what im doing wrong with this array I would really appreciate it. Thanks

Member
 
Join Date: Aug 2008
Posts: 36
#2: Jul 23 '09

re: Java Array HELP


Quote:

Originally Posted by yeshello54 View Post

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<16; i++) {
  2.  
  3.            colorArray[i]=MAROON;
  4.    colorArray[i]=RED;
  5. colorArray[i]=YELLOW;
  6. colorArray[i]=GREEN;
  7. colorArray[i]=BLUE;
  8. ....
  9. ...
  10. ..
  11.  
  12.  
  13.             }

You are doing the loop wrong. Every colorArray is filled with the latest color of that loop. You can do it in loop but probably would be easier and better just to add them right away to array.

Also, use code tags.
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#3: Jul 23 '09

re: Java Array HELP


how would i go about adding them to the array without using a loop? and can you explain code tags a little bit more?Thanks.
Member
 
Join Date: Aug 2008
Posts: 36
#4: Jul 23 '09

re: Java Array HELP


Quote:

Originally Posted by yeshello54 View Post

how would i go about adding them to the array without using a loop? and can you explain code tags a little bit more?Thanks.

You can wrap the code with # when making message. Makes it easier to read. As for filling array, it's simple really:

Expand|Select|Wrap|Line Numbers
  1. RED = (new Color(255,0,0));
  2. colorArray[2]=RED;
Or:

Expand|Select|Wrap|Line Numbers
  1. colorArray[2]=(new Color(255,0,0));
Member
 
Join Date: Mar 2009
Location: San Diego , CA
Posts: 45
#5: Jul 24 '09

re: Java Array HELP


Ahh thank you..Ya i totally just drew a blank when it comes to arrays..ha.thanks though.
Reply

Tags
array, grid, java, loop, swing