Mulriple timers question

hey i need help on a program as soon as possible. i need an event to happen every 20 seconds and within the 5 seconds i want an event. (im changing pixels back and forth). i need help with the implementing two timers at once.(i know i have to uses thee timer class) thanks for the help
[292 byte] By [nbjacoba] at [2007-10-3 11:27:39]
# 1
Eh? What's your question?I'm pretty sure you can have as many timers as you like.
paulcwa at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...
# 2
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?
nbjacoba at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...
# 3
So you want something to happen every 10 seconds? Why didn't you say so?
jwentinga at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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

kajbja at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...
# 5

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?

paulcwa at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...
# 6

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

nbjacoba at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

paulcwa at 2007-7-15 13:53:47 > top of Java-index,Java Essentials,Java Programming...