gpt4 book ai didi

java - 当我的程序运行时,无法更改timer.scheduleAtFixedRate(TimerTask任务,长延迟,长周期)的长周期

转载 作者:行者123 更新时间:2023-12-02 11:11:55 25 4
gpt4 key购买 nike

我正在制作约翰·康威的生命游戏,但我无法弄清楚如何更改代码中的时间段或在本例中的时间长度 time.scheduleAtFixedRate(task, 0, timelength)程序正在运行。我试图在代码中使用 jslider 更改时间长度,但它根本没有改变。在代码中,一些代码创建了可以忽略的图形,而其他许多代码则用于 jtextfields 和 jbutton 等内容。

//Game of Life
package ics_sad_majorproject.gameoflifejava_1;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.SwingUtilities;

public class GameOfLife extends javax.swing.JFrame {
int intperiod = 100;
int wid = 50, hei = 50;
boolean[][] currentMove = new boolean[hei][wid], nextMove = new boolean[hei][wid];
boolean play;
public static int intpopulation = 0;
Image offScrImg;
Graphics offScrGraph;
public static int intGeneration = 0;
Timer time = new Timer();

public GameOfLife() {
initComponents();
offScrImg = createImage(jPanel2.getWidth(), jPanel2.getHeight());
offScrGraph = offScrImg.getGraphics();
TimerTask task = new TimerTask(){
public void run() {
if(play == true){
intGeneration++;
jLabel6.setText( "Generation: " + intGeneration );
for(int i = 0; i < hei; i++){
for(int j = 0; j < wid; j++){
nextMove[i][j] = decide(i,j);
}
}
for(int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
currentMove[i][j] = nextMove[i][j];
}
}
repain();
}
}
};
time.scheduleAtFixedRate(task, 0, intperiod);
repain();
}

private boolean decide(int i, int j){
int neighbors = 0;
if(j > 0){
if(currentMove[i][j-1]) neighbors++;
if(i>0) if(currentMove[i-1][j-1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j-1]) neighbors++;
}
if(j < wid - 1){
if(currentMove[i][j+1]) neighbors++;
if(i>0) if(currentMove[i-1][j+1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j+1]) neighbors++;
}
if(i>0) if(currentMove[i-1][j]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j]) neighbors++;
if(currentMove[i][j]&& neighbors < 2)intpopulation--;
if(currentMove[i][j]&& neighbors > 3)intpopulation--;
if(currentMove[i][j] && neighbors == 3)intpopulation--;
if(neighbors == 3) intpopulation++;
jLabel5.setText("Population: " + Integer.toString(intpopulation));
if(neighbors == 3) return true;
if(currentMove[i][j] && neighbors == 2)return true;
return false;
}

private void repain(){
offScrGraph.setColor(Color.BLACK);
offScrGraph.fillRect(0, 0, jPanel2.getWidth(), jPanel2.getHeight());
for (int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
if(currentMove[i][j])
{
offScrGraph.setColor(Color.getHSBColor((float) Math.random(), .8f, .8f));
int x = j * jPanel2.getWidth()/wid;
int y = i * jPanel2.getHeight()/hei;
offScrGraph.fillRect(x, y, jPanel2.getWidth()/wid, jPanel2.getHeight()/hei);
}
}
}
offScrGraph.setColor(Color.BLACK);
for (int i = 1; i < hei; i++){
int y = i * jPanel2.getHeight()/hei;
offScrGraph.drawLine(0, y, jPanel2.getWidth(), y);
}
for (int j = 1; j < wid; j++){
int x = j * jPanel2.getWidth()/wid;
offScrGraph.drawLine(x, 0, x, jPanel2.getHeight());
}
jPanel2.getGraphics().drawImage(offScrImg, 0, 0, jPanel2);
}
private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
jLabel4.setText(Integer.toString(jSlider1.getValue()) + " milisecounds");
intperiod = jSlider1.getValue();
}


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
currentMove = new boolean[hei][wid];
intGeneration = 0;
intpopulation = 0;
jLabel6.setText( "Generation: " + intGeneration );
jLabel5.setText( "population: " + intpopulation );
repain();
}
private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
currentMove[i][j] = true;
intpopulation++;
jLabel5.setText("Population: " + Integer.toString(intpopulation));
repain();
}

private void jPanel2ComponentResized(java.awt.event.ComponentEvent evt) {
offScrImg = createImage(jPanel2.getWidth(), jPanel2.getHeight());
offScrGraph = offScrImg.getGraphics();
repain();
}

private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
if(SwingUtilities.isLeftMouseButton(evt)){
currentMove[i][j] = true;
intpopulation++;
}else currentMove[i][j] = false;
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
play = !play;
if(play) jButton3.setText("Pause");
else jButton3.setText("play");
repain();
}

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
String text = jTextField4.getText();
hei = Integer.valueOf(text);
jTextField4.setText("Height: "+ Integer.toString(hei));
currentMove = new boolean[hei][wid];
nextMove = new boolean[hei][wid];
repain();
}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String text = jTextField1.getText();
wid = Integer.valueOf(text);
jTextField1.setText( "Width: "+ Integer.toString(wid));
currentMove = new boolean[hei][wid];
nextMove = new boolean[hei][wid];
repain();
}
}

最佳答案

问题是,计时器一旦实例化并启动,就不会在运行时检查参数是否已更改。这是一种封闭。

每次更改其中一个参数时,都必须使用新参数创建一个新的计时器实例。

EDIT

import java.awt.*;
import javax.swing.SwingUtilities;
import java.util.Timer;
import java.util.TimerTask;

public class GameOfLife extends javax.swing.JFrame {

// Put this parameters global in order to be able to change it from different points in your code
Timer timer;
TimerTask timerTask;
int delay = 0;
int period = 100;


int wid = 200, hei = 100;
boolean[][] currentMove = new boolean[hei][wid], nextMove = new boolean[hei]
[wid];
boolean play;
public static int intpopulation = 0;
Image offScrImg;
Graphics offScrGraph;
public static int intGeneration = 0;

public GameOfLife() {
initComponents();
offScrImg = createImage(jPanel2.getWidth(), jPanel2.getHeight());
offScrGraph = offScrImg.getGraphics();

// start the very first timer instance
restartTimer();

repain();
}

/**
* This method first checks, whether the timer is already instatiated.
* If so, the current timer gets cancelled.
* Then the timerTask gets recreated.
*
* Finally a new timer is instantiated and gets scheduled.
*/
private void restartTimer() {
if(timer != null) {
timer.cancel();
}

resetTimerTask();

System.out.println("TimerTask: " + (timerTask != null));
System.out.println("period: " + period);
System.out.println("delay: " + delay);

timer = new Timer();
timer.scheduleAtFixedRate(timerTask, delay, period);
}

private void resetTimerTask() {
timerTask = new TimerTask(){
public void run() {
if(play == true){
intGeneration++;
for(int i = 0; i < hei; i++){
for(int j = 0; j < wid; j++){
nextMove[i][j] = decide(i,j);
}
}

for(int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
currentMove[i][j] = nextMove[i][j];
}
}
repain();
}
}
};
}

private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
jLabel4.setText(Integer.toString(jSlider1.getValue()));
period = jSlider1.getValue();

// whenever you change the period, start a new timer instance
restartTimer();
}

private boolean decide(int i, int j){
int neighbors = 0;
if(j > 0){
if(currentMove[i][j-1]) neighbors++;
if(i>0) if(currentMove[i-1][j-1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j-1]) neighbors++;
}
if(j < wid - 1){
if(currentMove[i][j+1]) neighbors++;
if(i>0) if(currentMove[i-1][j+1]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j+1]) neighbors++;
}
if(i>0) if(currentMove[i-1][j]) neighbors++;
if(i<hei-1) if(currentMove[i+1][j]) neighbors++;
if(currentMove[i][j]&& neighbors < 2)intpopulation--;
if(currentMove[i][j]&& neighbors > 3)intpopulation--;
if(currentMove[i][j] && neighbors == 3)intpopulation--;
if(neighbors == 3) intpopulation++;
jLabel5.setText("Population: " + Integer.toString(intpopulation));
if(neighbors == 3) return true;
if(currentMove[i][j] && neighbors == 2)return true;
return false;
}

private void repain(){
offScrGraph.setColor(Color.BLACK);
offScrGraph.fillRect(0, 0, jPanel2.getWidth(), jPanel2.getHeight());
for (int i = 0; i < hei; i++){
for (int j = 0; j < wid; j++){
if(currentMove[i][j])
{
offScrGraph.setColor(Color.getHSBColor((float) Math.random(), .8f, .8f));
int x = j * jPanel2.getWidth()/wid;
int y = i * jPanel2.getHeight()/hei;
offScrGraph.fillRect(x, y, jPanel2.getWidth()/wid, jPanel2.getHeight()/hei);
}
}
}
offScrGraph.setColor(Color.BLACK);
for (int i = 1; i < hei; i++){
int y = i * jPanel2.getHeight()/hei;
offScrGraph.drawLine(0, y, jPanel2.getWidth(), y);
}
for (int j = 1; j < wid; j++){
int x = j * jPanel2.getWidth()/wid;
offScrGraph.drawLine(x, 0, x, jPanel2.getHeight());
}
jPanel2.getGraphics().drawImage(offScrImg, 0, 0, jPanel2);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
currentMove = new boolean[hei][wid];
intGeneration = 0;
intpopulation = 0;
jLabel6.setText( "Generation: " + intGeneration );
jLabel5.setText( "population: " + intpopulation );
repain();
}
private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
currentMove[i][j] = true;
intpopulation++;
jLabel5.setText("Population: " + Integer.toString(intpopulation));
repain();
}

private void jPanel2ComponentResized(java.awt.event.ComponentEvent evt) {
offScrImg = createImage(jPanel2.getWidth(), jPanel2.getHeight());
offScrGraph = offScrImg.getGraphics();
repain();
}

private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {
int j = wid * evt.getX() / jPanel2.getWidth();
int i = hei * evt.getY() / jPanel2.getHeight();
if(SwingUtilities.isLeftMouseButton(evt)){
currentMove[i][j] = true;
intpopulation++;
}else currentMove[i][j] = false;
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
play = !play;
if(play) jButton3.setText("Pause");
else jButton3.setText("play");
repain();
}

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
String text = jTextField4.getText();
hei = Integer.valueOf(text);
jTextField4.setText("Height: "+ Integer.toString(hei));
currentMove = new boolean[hei][wid];
nextMove = new boolean[hei][wid];
repain();
}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String text = jTextField1.getText();
wid = Integer.valueOf(text);
jTextField1.setText( "Width: "+ Integer.toString(wid));
currentMove = new boolean[hei][wid];
nextMove = new boolean[hei][wid];
repain();
}
}

关于java - 当我的程序运行时,无法更改timer.scheduleAtFixedRate(TimerTask任务,长延迟,长周期)的长周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50558635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com