> lol sorry. How do I implement 2 timers at once? I
> would like one timer to execute ever 30 seconds and
> within that time frame one that executes every 10
> seconds. How do i do that?
Just schedule two different tasks on one timer, or instantiate two different timers and schedule the tasks. A Timer is single threaded so you have to decide if that is good enough or not.
Kaj
I believe you can do either this:
Timer t1 = new Timer(10000, new SomeActionListener());
Timer t2 = new Timer(30000, new SomeOtherActionListener());
t1.start();
t2.start();
Or this:
Timer t = new Timer(10000, new ActionListener() {
private int count;
public void actionPerformed(ActionEvent e) {
doSomething();
if ((count++ % 3) == 0) {
doSomethingElse();
}
}
});
t.start();
Although I haven't tried it. Why not try writing either or both as a simple test case and see how it goes?
alright i got the timer working, but now im having problems with the graphics. I want it to alternate between drawing a rectangle and drawing an image. however it is only drawing the rectangle and not the image. i put print statements so i know its going into the method. when i comment out the rectangle, it draws the image. (for test purposes i set the rectangle and image to different locations)
I also tried making 2 different graphics objects on the same buffered image but its still not working.
g = (Graphics2D)image.getGraphics();
g2 = (Graphics2D)image.getGraphics();
timer = new Timer(3000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
g.setColor(new Color(image.getRGB(eye1X +(eye1Width/2), eye1Y + eye1Height + 3)));
g.fillRect(eye1X, eye1Y, eye1Width, eye1Height);
repaint();
System.out.println("3");
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4");
g2.drawImage(eye1, 0, 0, null);
repaint();
timer.restart();
}
});
timer.start();
When you post code, please wrap it in [code][/code] tags. If you don't the code you post is hard to read.
Re: your code, it's hard to tell what's going on since the code isn't formatted right (no code tags) and you only quoted selected snippets. Does your paint() method do drawImage(image, someInt, someInt)?
Also why do you have a Thread.sleep in your actionPerformed? The whole point of using timers is that you don't have to use Thread.sleep. I think that the timer may actually run in the GUI update thread; if it does, then sleeping like that could screw up graphics display and GUI responsiveness.