Zacharias Hortén
Zacharias Hortén

Reputation: 179

Update graphics for dots with Timer

Before I ask my question I apologize for any inconsistencies. I´m fairly new at this. I´m making a game that for now looks like this (the picture is not important): My game

The red dots are supposed to move to the right and they do that with a timer. This works fine. The graphics does not update though so I have to drag the side of the window back and forth to see that my dots are moving. What can I do to fix this?

My paintcomponent method in my mainclass:

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    for (int x = 0; x < SomeInts.amount; x++){
        for (int y = 0; y < SomeInts.amount; y++){
            tile[x][y].colorBody(g, x, y);              
            Tower temp;
            for (int i = 0; i < towers.size(); i++){        
                temp = towers.get(i);
                temp.colorBody(g, tile[x][y].getSize());
                temp.guard.colorBody(g, tile[x][y].getSize());                        
            }
        }
    }
}

My red dot class. Also called Guard class:

public class Guard {
int x, y, size = 10, towerx, towery;
Timer timer;
public Guard(int towerx1, int towery1){
    towerx = towerx1;
    towery = towery1;
    x = towerx + 1;
    y = towery;
    new Timer().schedule(new MyTask(), 1000, 1000);
}

public void colorBody(Graphics g, int tilesize){
    g.setColor(new Color(255, 0, 0));
    g.fillOval(x * tilesize + tilesize / 4, y * tilesize + tilesize / 4, size, size);       
}

public class MyTask extends TimerTask {
    public void run() {
        x++;
    }
}

}

Thank you for your time.

Upvotes: 4

Views: 219

Answers (1)

Roy
Roy

Reputation: 984

I'm guessing a bit here, but I think you need to call the repaint() method to see the changes you've made.

Upvotes: 5

Related Questions