Jump to content
  • Advertisement
Sign in to follow this  
xDancinpoptartx

Need java Timer code help.

This topic is 2523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why wont my timer work?
import javax.swing.JPanel;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;


public class Board extends JPanel implements ActionListener {

Image BR;
Charecter Char;
Timer time;
int DELAY = 5;

public Board(){
Char = new Charecter();
ImageIcon ii = new ImageIcon(this.getClass().getResource("/Resources/BackRoads.jpg"));
BR = ii.getImage();
time = new Timer(5, this);
time.start();

}


class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
repaint();
}


}


public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(BR, 0, 0, null);

}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

}

Share this post


Link to post
Share on other sites
Advertisement
1. Wrong forum ...this belongs to "General Programming / For Beginners" at best or a Java forum on the web

2. Look at the javadoc: http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

3. "time = new Timer(5, this);" should not even compile, cfr javadoc

4. Using a thread would probably be better and simpler

5. Do you need to refresh the drawing at all?! This is necessary only if you have sprites! For static pawns, you can just update you screen once the player moved something.

6, Refreshing every 5 millisecond is a lot for a boardgame!


Nevertheless, using a timer, your board should implement "TimerTask", and use the timer that way;



public Board(){
...
timer = new Timer(true);
timer.schedule(this, 0, 50);
}

public void run() {
repaint();
}

Share this post


Link to post
Share on other sites
Actually, there are two Timer classes in the JDK. You and him are using two different classes. The code the OP isusing is correct for the Timer class he is using. With that said, he already posted this in another thread in this forum, at which I answered his question.

Share this post


Link to post
Share on other sites

public class Board extends JPanel implements ActionListener {
Image BR;
Charecter Char;
Timer time;
int DELAY = 5;

public Board(){
Char = new Charecter();
ImageIcon ii = new ImageIcon(this.getClass().getResource("/Resources/BackRoads.jpg"));
BR = ii.getImage();
time = new Timer(5, this);
time.start();

}


public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(BR, 0, 0, null);

}

public void actionPerformed(ActionEvent e) {
repaint();

}
}





You did not need the unnecessary sub class. but if you did want to use it then you should have put this in the Timer constructor:

time = new Timer(5,new TimerListener());

putting the this keyword means that nothing will be rendered due to the fact that in your actionPerformed method in the Board class was empty.

Hope it helped

Gen.

Share this post


Link to post
Share on other sites

Actually, there are two Timer classes in the JDK. You and him are using two different classes. The code the OP isusing is correct for the Timer class he is using. With that said, he already posted this in another thread in this forum, at which I answered his question.


No he is not using his Timer class correctly, and he should read the Java Doc.
He imported

import java.util.Timer;

this the one he is using.
http://docs.oracle.c...util/Timer.html
He cannot use it in that way.

You are thinking of javax.swing.Timer which he might want to use but is not using.
http://docs.oracle.c...wing/Timer.html

He will get a compile error with his code because of an Invalid Constructor call.


You did not need the unnecessary sub class. but if you did want to use it then you should have put this in the Timer constructor:

time = new Timer(5,new TimerListener());

putting the this keyword means that nothing will be rendered due to the fact that in your actionPerformed method in the Board class was empty.

Hope it helped

Gen.


You should actually avoid using 'this' in constructors because it is one of the conditions that can cause Java to leak because it's not fully initialized so it is best to get out of that bad habit. A subclass is a good way to avoid it, but a Factory is better but well beyond what he's doing. However, you're right he used the subclass incorrectly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!