Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need java Timer code help.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 17 January 2012 - 08:50 AM

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

 

}



}



Sponsor:

#2 jonbonazza   Members   -  Reputation: 121

Like
1Likes
Like

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.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS