Question

I'm trying to have a timer trigger a TimerTask I made called UpdateTask every second. However, eclipse states that I have a syntax error on the line:

"timer . schedule ( new UpdateTask(), 10);"

I have emboldened the specific tokens on which eclipse is declaring a syntax error. I don't understand what I'm missing here.

import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;


public class graphpanel extends variables
{
Timer timer = new Timer();
timer.schedule(new UpdateTask(), 10);

int ypoint;
int barheight;

int height = getHeight();
int width = getWidth();
int bars = (int)getLife() - (int)getAge();
int xpoint = 0;
int barwidth = 20;

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

    for (int i = 0; i < bars; i++)
    {
        barheight = (int) getTime(i)/100;
        ypoint = height/2 - barheight;
        g.drawRect(xpoint, ypoint, barwidth, barheight);
    }
}

class UpdateTask extends TimerTask
{
    public void run()
    {
        bars = (int)getLife() - (int)getAge();
        repaint();
    }
}

}

Was it helpful?

Solution

This line

timer.schedule(new UpdateTask(), 10);

is not a declaration and thus needs to be within a method, constructor, static initializer, or instance initializer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top