papernemesis
papernemesis

Reputation: 85

I want to use my counter in a different class, but it appears as its initial value of zero

I'm trying to create a program that produces a random number of beats for 5 minutes and then displays the final beat count on the screen after 5 minutes. Here are two of my classes that are related to my current problem. I've tried so many things, but the final beat count still shows up as zero. I'm super new to Java, so I would appreciate as much specificity as possible. Thanks in advance!

And on a side note, I also wanted to have the user input their guess on the screen after the timer is over. Could someone point me on the right track with that? I couldn't really find anything online as to how I would go about doing that. I don't want a dialogue box (unless I can change the way it looks?).

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();
    }
}

This is my timer class where I'm getting the count from. Timer is set to 10 seconds.

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();
        }
    }
}

Upvotes: 2

Views: 99

Answers (1)

Eran
Eran

Reputation: 394156

You have two instances of your MyTimer class. One is static - t - and the other not - myTask. The instance that is actually used by the Timer is myTask, but you use the other instance to print the count - g.drawString(t.count+" beats", 200, 200);. Therefore it's not surprising that the count being printed is 0.

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

Upvotes: 1

Related Questions