- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作约翰·康威的生命游戏,但我无法弄清楚如何更改代码中的时间段或在本例中的时间长度 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/
我正在使用计时器以相当长的间隔(2分钟)定期运行事件。一切正常。但是,我希望在创建计时器时立即触发该事件(而不是等待2分钟)。 请注意,我不能仅通过调用方法来执行此操作,因为它需要一些时间才能运行,并
我正在检查一些可怕的遗留代码,这些代码具有 Timer 事件以及一些包含 DoEvents 调用的冗长代码。简化版如下所示: Private Sub tmrProcess_Timer()
我正在尝试创建一个 Windows 窗体应用程序,我想实现一个计时器。 public void timerStart() { DateTime now = DateTime
我正在创建一个新类,以便使 System.Timers.Timer 类更好地满足我的需要。我像这样创建我的新类...... using System.Timers; class BtElapsedEv
我最近一直在检查一些可能的计时器,并且 System.Threading.Timer和 System.Timers.Timer对我来说是必要的(因为它们支持线程池)。 我正在制作一款游戏,我计划使用所
首先我要说的是,与其说这是一个需要解决的问题,不如说这是一个问题。我现在有了解决方案,对我来说一切正常。但是我想知道为什么第一次出现问题。 这是我现在拥有的代码,它的工作方式与我预期的一样:
在本文中:http://msdn.microsoft.com/en-us/magazine/cc164015.aspx作者声明 System.Threading.Timer 不是线程安全的。 从那时起
背景: 我有一个计时器,我用它来跟踪自 serialPort DataReceived 事件被触发以来已经过了多长时间。我正在为此创建自己的解决方案而不是使用内置的超时事件,因为我正在获取连续的数据流
我正在查看 Flutter Timer代码和 cancel() 方法。当我想删除一个计时器时,我可以使用这两行: timer.cancel(); timer = null; 或者我可以这样做: 定时器
我正在尝试使用 C# 中的计时器以五秒的间隔运行一个方法。虽然这段代码似乎不起作用。运行它时我没有收到任何错误,但程序(我在控制台中运行)在 IP.timer1.Start() 之后立即关闭。 tim
我正在尝试使用 C# 中的计时器以五秒的间隔运行一个方法。虽然这段代码似乎不起作用。运行它时我没有收到任何错误,但程序(我在控制台中运行)在 IP.timer1.Start() 之后立即关闭。 tim
我有错误显示: 'Timer' is an ambiguous reference between 'System.Windows.Forms.Timer' and 'System.Threading
在我的应用程序中,我必须定期向“兄弟”应用程序发送心跳。 使用 System.Timers.Timer/Threading.Timer 或使用带有 while 循环和 Thread.Sleep 的线程
我最近遇到了编写 Windows 服务的挑战。我需要定期请求一个 URL 并检查它的可用性。为此,我决定在 OnStart 中初始化一个计时器。服务方法并在 timer_Tick 中完成所有工作事件。
看来 System.Timers.Timer 实例通过某种机制保持事件状态,但 System.Threading.Timer 实例则不然。 示例程序,具有定期System.Threading.Time
我做了一个 goog.Timer对象 ( http://closure-library.googlecode.com/svn/docs/class_goog_Timer.html ) 与 new go
当您处理“原始”.net 计时器时,您可以传入等待句柄以在 Win32 计时器被销毁后调用,并且您可以假设您的回调不会被调用。 (并且计时器将被 GC 视为“死”) 如何使用 System.Timer
我想让 deoplete 自动完成建议弹出得更快,这需要设置 g:deoplete#auto_complete_delay help 说这需要 +timers 支持。如何在配置中启用此计时器? 谢谢!
我想知道是否有合理的方法来确定 Timers.Timer 对象的负载能力?或者,更确切地说,线程功能。有人知道使用 Timer 类启动大量(数百个)线程是否有任何问题? 编辑:为了工作,我们将启动一些
我正在创建一个 WindowsService,它的 Timer 绑定(bind)到具有无限循环的 EventHandler;我希望能够在服务的 OnStart() 方法中触发此计时器一次,然后在服务的
我是一名优秀的程序员,十分优秀!