1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package org.alwin;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class TestSwing extends JFrame{
private static final int ARRAY_SIZE = 20;
JButton[][] p = new JButton[ARRAY_SIZE][ARRAY_SIZE];
boolean[][] b = new boolean[ARRAY_SIZE][ARRAY_SIZE];
Random rand = new Random();
public TestSwing() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
GridLayout layout = new GridLayout(ARRAY_SIZE,ARRAY_SIZE);
contentPane.setLayout(layout);
for( int i = 0; i < ARRAY_SIZE; i++ ) {
for(int j = 0; j < ARRAY_SIZE; j++ ) {
p[i][j] = new JButton();
contentPane.add(p[i][j]);
b[i][j]=rand.nextBoolean();
}
}
JButton pane = p[0][0];
pane.setText("Clic me");
pane.setBackground(Color.blue);
pane.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
startAnim2();
}
}
);
setSize(400,400);
setVisible(true);
}
private void startAnim2() {
Runnable r = new Runnable() {
public void run() {
startAnim();
}
};
new Thread(r).start();
}
private void startAnim() {
for( int timer = 0; timer < 100 ; timer++) {
for( int i = 0; i < ARRAY_SIZE; i++ ) {
for(int j = 0; j < ARRAY_SIZE; j++ ) {
JButton _p = p[i][j];
boolean _b = b[i][j];
_p.setBackground( _b ? Color.BLACK : Color.WHITE);
b[i][j] = rand.nextBoolean();
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
}
}
public static void main(String[] args) {
new TestSwing();
}
} |