スレッドが循環しないという問題があり、その理由がわかりません。run メソッドを 1 回実行してから停止します。これが私のコードです。
public class LifeFrame extends JApplet{
private Scanner scan;
public FramePanel framePanel = new FramePanel();
private JPanel buttonPanel = new JPanel();
private JButton button1 = new JButton("Start");
private JButton button2 = new JButton("Stop");
private JButton button3 = new JButton("Reset");
public ImageIcon onTemp = new ImageIcon(this.getClass().getResource("squareOn.png"));
public ImageIcon offTemp = new ImageIcon(this.getClass().getResource("squareOff.png"));
public Image lifeOn = onTemp.getImage();
public Image lifeOff = offTemp.getImage();
public boolean[][] lifeCheck = new boolean[20][20];
public boolean[][] lifeHolder = new boolean[20][20];
public boolean run = false;
private Thread animator;
public void init(){
try {
scan = new Scanner(new File("life100.txt"));
} catch (FileNotFoundException e) {
System.out.println("ERROR");
}
int temp = Integer.parseInt(scan.nextLine().trim());
for(int i = 0; i < temp; i++)
lifeHolder[Integer.parseInt(scan.next().trim())-1][Integer.parseInt(scan.nextLine().trim())-1] = true;
framePanel.setSize(700,800);
button1.setSize(100,60);
button2.setSize(100,60);
button3.setSize(100,60);
ButtonHandler handler = new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
buttonPanel.setSize(300,60);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.setBackground(new Color(71,68,68));
add(framePanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
startThread();
framePanel.repaint();
}
public void startThread(){
animator = new Thread(framePanel);
animator.start();
}
public class FramePanel extends JPanel implements Runnable{
public FramePanel(){
this.setLayout(new GridLayout(20, 20));
}
public void run(){
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while(run){
cycle();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = 500 - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("Sleep Interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
public void cycle(){
int neighbors = 0;
for(int row = 0; row < lifeHolder.length; row++)
for( int col = 0; col < lifeHolder[row].length; col++)
lifeCheck[row][col] = lifeHolder[row][col];
for(int row = 0; row < lifeHolder.length; row++){
for( int col = 0; col < lifeHolder[row].length; col++){
for(int r = row - 1; r <= row + 1; r++){
for(int c = col - 1; c <= col + 1; c++){
if(r >= 0 && c >= 0 && r < lifeHolder.length && c < lifeCheck[row].length)
if(lifeHolder[r][c])
neighbors++;
}
}
if(lifeHolder[row][col]){
neighbors--;
if(neighbors < 2 || neighbors > 3)
lifeCheck[row][col] = false;
}else{
if(neighbors == 3)
lifeCheck[row][col] = true;
}
neighbors = 0;
}
}
for(int row = 0; row < lifeHolder.length; row++)
for( int col = 0; col < lifeHolder[row].length; col++)
lifeHolder[row][col] = lifeCheck[row][col];
repaint();
}
public void paint(Graphics g){
for(int r = 0; r < 20; r++)
for(int r1 = 0; r1 < 20; r1++){
if(lifeHolder[r][r1]){
g.drawImage(lifeOn, r1*20 + 3, r*20 + 29, 20, 20, framePanel);
}else
g.drawImage(lifeOff, r1*20 + 3, r*20 + 29, 20, 20, framePanel);
}
}
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource().equals(button1)){
run = true;
System.out.println(run);
}
if(event.getSource().equals(button2)){
run = false;
System.out.println(run);
}else{
//reset
}
}
}
}
どんな助けでも大歓迎です。ありがとう。