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
4 replies to this topic

#1 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 17 January 2012 - 08:42 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 creatures-of-gaia.com   Members   -  Reputation: 377

Like
0Likes
Like

Posted 18 January 2012 - 02:53 AM

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


#3 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 18 January 2012 - 12:44 PM

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

#4 CryoGenesis   Members   -  Reputation: 495

Like
0Likes
Like

Posted 18 January 2012 - 02:47 PM

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.

#5 medv4380   Members   -  Reputation: 98

Like
0Likes
Like

Posted 18 January 2012 - 05:36 PM

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.




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