我想在不同的类中使用我的计数器,但它的初始值为零

时间:2015-01-18 07:04:11

标签: java timer count counter

我正在尝试创建一个程序,该程序产生5分钟的随机数量的节拍,然后在5分钟后在屏幕上显示最终节拍数。以下是我的两个与当前问题相关的课程。我已经尝试了很多东西,但最终的节拍数仍然显示为零。我是Java的新手,所以我会尽可能多地欣赏它。提前谢谢!

另一方面,我还想让用户在计时器结束后在屏幕上输入他们的猜测。有人能指出我在正确的轨道上吗?我无法在网上找到任何关于如何做到这一点的内容。我不想要一个对话框(除非我可以改变它看起来的样子?)。

package com.home.timer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.Timer;
import javax.swing.JPanel;

public class MyScheduler extends JPanel
{

public static final long serialVersionUID = 1L;

public static MyFrame d = new MyFrame();
public static MyTimer t = new MyTimer();

public Font large = new Font ("Calibri", Font.BOLD, 30);
public Font small = new Font ("Calibri", Font.PLAIN, 18);
public Rectangle beginButton = new Rectangle (310, 290, 650, 300);
public Rectangle beginNow = new Rectangle (594, 472, 80,40);
public Rectangle beginNow2 = new Rectangle (594,492,80,40); //new variable because beginNow isn't at right location for some reason
public static String appState = "Begin"; //need Begin for background

Timer timer = new Timer();
Random time = new Random();
int number = 2000+time.nextInt(3000); //setting period to random

MyTimer myTask = new MyTimer (timer);
MySound myBeat = new MySound (timer);

int firstStart = 1000; //timer will start after x ms
int period = number; //task will repeat after this period

public MyScheduler (MyFrame d)
{
d.f.addMouseListener(new MouseListener()
{
    public void mouseClicked(java.awt.event.MouseEvent arg0) //click button to begin task
    {
        Point p = arg0.getPoint();
        if (beginNow2.contains(p)){
            appState = "Start"; //Start = no dialogue box
            timer.schedule(myTask, firstStart, period); //begin count
            timer.schedule(myBeat,firstStart, period); //begin beats
        }
    }
    public void mouseEntered(java.awt.event.MouseEvent arg0)
    {   
    }
    public void mouseExited(java.awt.event.MouseEvent arg0)
    {   
    }
    public void mousePressed(java.awt.event.MouseEvent arg0)
    {   
    }
    public void mouseReleased(java.awt.event.MouseEvent arg0)
    {   
    }
});
}

public static void atEnd() //if time is over, then appState changes to Result
{
    while(true){
    if (System.currentTimeMillis() > t.end)//this is condition when you want to stop task
    {
        appState = "Result";
    }
    }
}

public void paintComponent (Graphics g)
{
    super.paintComponent(g);

    g.drawImage(MyFrame.i.BG, 0, 0, MyScheduler.d.width, MyScheduler.d.height, null); //background picture

    if (toBegin()) //Begin = dialogue box
    {
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(310,290,650,300);
        g.setColor(Color.WHITE);
        g.drawRect(beginButton.x, beginButton.y, beginButton.width, beginButton.height);
        g.setFont(large);
        g.drawString("Begin", beginButton.x +290, beginButton.y+210);
        g.drawRect(beginNow.x, beginNow.y, beginNow.width, beginNow.height);
    }

    if (appState == "Result") //shows count at end
    {
        g.setFont(large);
        g.setColor (Color.RED);
        g.drawString(t.count+" beats", 200, 200);
    }

    repaint();
}

public void startApp() //startApp = Start = toStart
{
    appState = "Start";
}
public void beginApp() //beginApp = Begin = toBegin
{
    appState = "Begin";
}
public void resultApp() //resultApp = Result = toResult
{
    appState = "Result";
}

public boolean toBegin()
{
    return (appState.equalsIgnoreCase("Begin")? true:false);
}
public boolean toStart()
{
    return (appState.equalsIgnoreCase("Start")? true:false);
}
public boolean toResult()
{
    return (appState.equalsIgnoreCase("Result")? true:false);
}

    public static void main(String [] args)
    {
        d.display();
        atEnd();
    }
}

这是我的计时器课,我从中得到了计数。定时器设置为10秒。

package com.home.timer;

import java.util.Timer;
import java.util.TimerTask;

public class MyTimer extends TimerTask
{
    Timer timer;
    int count = 0;
    public MyTimer(){   
    }

    public MyTimer (Timer timer)
    {
        this.timer = timer;
    }

    public void toDo()
    {
        System.out.println (" Count: " + (count++));
    }

    long start = System.currentTimeMillis();
    long end = start + 10*1000;

    @Override
    public void run()
    {
        toDo();
        if (System.currentTimeMillis() > end)//this is condition when you want to stop task
        {
            timer.cancel();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您有MyTimer课程的两个实例。一个是静态的 - t - 另一个不是 - myTaskTimer实际使用的实例是myTask,但您使用其他实例打印计数 - g.drawString(t.count+" beats", 200, 200);。因此,打印的计数为0并不奇怪。

尝试g.drawString(myTask.count+" beats", 200, 200);