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
}
}
1 reply to this topic
#1 Members - Reputation: 118
Posted 17 January 2012 - 08:50 AM
Why wont my Timer work?
Sponsor:
#2 Members - Reputation: 117
Posted 17 January 2012 - 03:27 PM
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.
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.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US
Bonafide Software, L.L.C.
Fairmont, WV 26554 US






