Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Java code help.

4: Adsense

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

#1 Dylan_DE   Members   


Posted 17 January 2012 - 08:20 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);
    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


#2 Net Gnome   Members   


Posted 17 January 2012 - 09:12 AM

i believe you need to assign your custom listener to your timer object. So it can notify it when the timer event occurs.

edit: sorry, i was completely wrong previously... Timer uses a TimerTask instead of an action listener (if you're using java.util.Timer). so try extending with TimerTask instead of ActionListener and assign your task via the schedule method.

#3 medv4380   Members   


Posted 17 January 2012 - 11:32 PM

You're initializing the timer incorrectly. Sun put a lot of time into making good java docs, and you should use them when you're having a problem with an object/API.
For Java 7 use http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html
For 1.4 use http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html

Timer should just be initialized with Timer() or Timer(boolean) if you want to to run as a daemon if you're using one of the older version of java. Newer ones allow you to name the thread that the timer will use but that's usually irrelevant unless you're using a profiler to monitor the app as it runs.

From what your code is doing you're trying to initialize with int (5) and with a JPanel, which isn't in the list of valid constructors which is why reading the API documentation is important. I assume you were trying to make a timer that would launch a JPanel after 5 milliseconds. To launch any frame or panel you have to ether set them to visible or add them to an existing visible panel.

You're also using "this" in the constructor and if you're using NetBeans, and I believe even Eclipse, should give you a warning/tip to not do that. The reason is that your code can leak because the Object isn't fully initialized yet. You'll see a lot of code use 'this' in the constructor, but it can cause problems so it is best to just avoid doing so entirely.

Here is some code that should be similar enough to your own that you can barrow some of the concepts from it. Using a subclass can help avoid using the 'this' keyword, and you even had a DELAY variable in your own code, but you didn't use it.
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
public class Board{
    public static void main(String args[]) {
	    Board b = new Board();
    Timer time;
    JFrame mainFrame;
    int DELAY = 5000;
    public Board() {
	    mainFrame = new JFrame("me");
	    mainFrame.setSize(320, 240);
	    time = new Timer();
	    time.schedule(new myTimer(), DELAY);
    class myTimer extends TimerTask {
	    public void run() {
		    System.out.println("Delayed Hello World");
		    System.out.println("I will show you my Frame");

#4 jonbonazza   Members   


Posted 19 January 2012 - 02:10 PM

Medv, he is using the swing timer, not the utility timer.
Here is the docs for it:

He is doing it correctly, however like i mentioned in my other post, he is assigning the ActionListener implemented by his JFrame subclass to the timer which doesn't do anything. He needs to construct an instance of his TimerListener class and pass a pointer to it to the Timer constructor instead of "this".
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.