user2938704
user2938704

Reputation:

Linking two classes together - Java

The class below can not find Timer in "time = new Timer", and I can not see why? am i missing something here. Thanks for any help. I had it working a while back but somehow I must of changed something and cant get it working again

    public ZombieDefense(GameScreen pointer) {
    setSize (1024,768);
    framePointer = pointer;
    time = new Timer();
    timer.start();
}


    @Override
    public void paint(Graphics g) {
        g.drawImage(background.getImage(), 0, 0, 1024, 768, null);
        survivor.draw(g);
        barricade.draw(g);
        int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
        int fontSize = (int)Math.round(22.0 * screenRes / 72.0);
        Font font = new Font("Arial", Font.PLAIN, fontSize);
        g.setFont(font);
        g.setColor(Color.white);
        g.drawString("WAVE " + wave, 450, 30);
        g.drawString("Base Health: " + barricade.health, 250, 700);
        g.drawString("Enemies remaining:  " + totalzomb, 650, 700);
        String s = "Time: " + time.counter;
        g.drawString(s,260,30);
        g.setColor(Color.red);
        g.fillRect (420,670,200,40);
        g.setColor(Color.green);
        g.fillRect (420,670,barricade.health,40);

This is the class that holds the Timer and counter

public class Stopwatch implements Runnable {

    int counter;
    Thread Timer = new Thread(this);

    public Stopwatch()
    {
    Timer.start();
    }

    @Override
    public void run() {
  counter = 0;

Upvotes: 0

Views: 96

Answers (1)

christopher
christopher

Reputation: 27346

Your issue

timer.start();

should be

time.start();

Why?

Well look at your declaration:

time = new Timer();

This states the following.

// Create a Timer object and return the reference to that object.
new Timer();

// Store that reference and call it "time"
time = new Timer();

From there on, you get to the object via the reference, and you've named that reference time, not timer.

Just a thought

You're creating a separate class called Stopwatch, but you're never using it? I think, what you mean to do is something along the lines of:

time = new Stopwatch();
// the timer is started in the StopWatch constructor, so we don't need to do it.

Upvotes: 3

Related Questions