プログラムのダウンロード
ダウンロード開始
ソースコード
●FreeLifeクラス(FreeLife.java)
package jp.tuyano;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FreeLife extends JFrame
implements ActionListener, Runnable{
private static final long serialVersionUID = 1L;
private JButton button1,button2;
private LifePanel life1;
private Thread t;
private boolean flg,kissOfDeath;
public static void main(String[] args) {
new FreeLife().setVisible(true);
}
public FreeLife(){
this.setSize(530, 500);
setBackground(Color.pink);
flg = false;
kissOfDeath = false;
setLayout(null);
button1 = new JButton();
button1.setText("Strat");
button1.setBounds(10, 420, 70, 20);
getContentPane().add(button1);
button2 = new JButton();
button2.setText("Clear");
button2.setBounds(440, 420, 70, 20);
getContentPane().add(button2);
life1 = new LifePanel(100, 80);
life1.setBounds(10, 10, 500, 400);
life1.setBackground(Color.black);
getContentPane().add(life1);
button1.addActionListener(this);
button2.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == button1){
if(!flg){
button1.setText("Stop");
kissOfDeath = true;
t = new Thread(this);
t.start();
} else {
kissOfDeath = false;
button1.setText("Start");
}
flg = !flg;
return;
} else {
life1.initialData();
life1.repaint();
return;
}
}
public void run(){
do {
life1.checkAllLife();
life1.repaint();
try {
Thread.sleep(100L);
}
catch(Exception _ex){}
} while(kissOfDeath);
}
}
●LifePanelクラス(LifePanel.java)
package jp.tuyano;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
public class LifePanel extends JPanel {
private static final long serialVersionUID = 1L;
private boolean data[][];
private int dataWidth;
private int dataHeight;
private Image offScreen;
private Graphics offg;
public LifePanel(){
this(25, 25);
}
public LifePanel(int i, int j){
dataWidth = i;
dataHeight = j;
setPreferredSize(new Dimension(dataWidth * 5,
dataHeight * 5));
initialData();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent mouseevent){
doMouseDown(mouseevent);
}
});
}
public void initialData(){
data = new boolean[dataWidth][dataHeight];
for(int i = 0; i < dataWidth; i++){
for(int j = 0; j < dataHeight; j++)
data[i][j] = false;
}
}
public void doMouseDown(MouseEvent ev){
int i = ev.getX() / 5;
int j = ev.getY() / 5;
if(i >= dataWidth || j >= dataHeight){
return;
} else {
data[i][j] = !data[i][j];
repaint();
return;
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(offScreen == null){
offScreen = createImage(dataWidth * 5, dataHeight * 5);
offg = offScreen.getGraphics();
}
offg.setColor(Color.BLACK);
offg.fillRect(0, 0, dataWidth * 5, dataHeight * 5);
offg.setColor(Color.GREEN);
for(int i = 0; i < dataWidth; i++){
for(int j = 0; j < dataHeight; j++)
if(data[i][j])
offg.fillRect(i * 5, j * 5, 4, 4);
}
g.clearRect(0, 0, getSize().width, getSize().height);
g.drawImage(offScreen, 0, 0, this);
}
public void checkAllLife(){
boolean aflag[][] = new boolean[dataWidth][dataHeight];
for(int j = 0; j < dataWidth; j++){
for(int k = 0; k < dataHeight; k++){
int i = checkLife(j, k);
aflag[j][k] = false;
if(i == 2 && data[j][k])
aflag[j][k] = true;
if(i == 3)
aflag[j][k] = true;
if(i < 2 || i > 3)
aflag[j][k] = false;
}
}
for(int l = 0; l < dataWidth; l++){
for(int i1 = 0; i1 < dataHeight; i1++)
data[l][i1] = aflag[l][i1];
}
repaint();
}
public int checkLife(int i, int j){
int k = 0;
int l = dataWidth;
int i1 = dataHeight;
return k = (k = (k = (k = (k = (k = (k =
(k += data[(i + l + -1) % l][(j + i1 + -1) % i1] ? 1 : 0)
+ (data[(i + l) % l][(j + i1 + -1) % i1] ? 1 : 0))
+ (data[(i + l + 1) % l][(j + i1 + -1) % i1] ? 1 : 0))
+ (data[(i + l + -1) % l][(j + i1) % i1] ? 1 : 0))
+ (data[(i + l + 1) % l][(j + i1) % i1] ? 1 : 0))
+ (data[(i + l + -1) % l][(j + i1 + 1) % i1] ? 1 : 0))
+ (data[(i + l) % l][(j + i1 + 1) % i1] ? 1 : 0))
+ (data[(i + l + 1) % l][(j + i1 + 1) % i1] ? 1 : 0);
}
}
バックナンバー
第1回「画面キャプチャーを作ろう」
第2回「画面キャプチャーを作ろう(2)」