Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Need java Timer code help.

This topic is 2833 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);


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


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


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



Share this post

Link to post
Share on other sites
You subclassed ActionListener and then overrode the actionPerformed method, but you registered the JFrame subclass (which implements ActionListener, not TimerListener) as a listener for your timer.

You need to give your JFrame subclass a TimerListener as a data member, construct it in the constructor, and then pass a pointer to that object as the second argument of the Timer object constructor.

I am not sure what your design requires, but from what you showed us, you could also remove the implementation of ActionListener from the JFrame subclass as it is seemingly uneeded.

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!