
April 24th, 2008, 08:00 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 1
Time spent in forums: 51 m 36 sec
Reputation Power: 0
|
|
|
Adding up color values??
Hey anyone, I have this code exercise where i have a 10 X 10 grid of Jbuttons that produces a random color when clicked. Then there is 1 row on the bottom and 1 column on the right that gives the average value of the colors from the buttons that were clicked. Basically the only part I'm confused on is how to get colors to add together. Here is what I've gotten so far. I have not found much in my book to help me  .
Code:
public class Buttons extends JFrame {
JButton[][] buttonColor = new JButton[ 10][10];
Color[][] randColor = new Color[10][10];
public Buttons() {
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(10, 10));
for(int i = 0; i < buttonColor.length; i++) {
for( int o = 0; o < buttonColor[i].length; o++) {
buttonColor[i][o] = new JButton();
buttonColor[i][o].addMouseListener(new click());
getContentPane().add(buttonColor[i][o]);
}
}
setVisible(true);
}
private class click extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
JButton pressed = (JButton)e.getSource();
pressed.setBackground(randColor());
/This part doesnt work, but is just a hypothetical on my part.
if(ButtonIsclicked) {
int average = red + green + blue;
}
}
public Color randColor() {
int red = (int)(Math.random()*256);
int blue = (int)(Math.random()* 256);
int green = (int)(Math.random()*256);
return new Color(red, green, blue);
}
public static void main(String[] args) {
new Buttons();
}
}
Thanks for any Help 
|