Jump to content

  • Log In with Google      Sign In   
  • Create Account

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

#1 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

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);
  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 Net Gnome   Members   -  Reputation: 773

Like
0Likes
Like

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   -  Reputation: 98

Like
0Likes
Like

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);
	    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    time = new Timer();
	    time.schedule(new myTimer(), DELAY);
    }
    class myTimer extends TimerTask {
	    @Override
	    public void run() {
		    System.out.println("Delayed Hello World");
		    System.out.println("I will show you my Frame");
		    mainFrame.setVisible(true);
	    }
    }
}


#4 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 19 January 2012 - 02:10 PM

Medv, he is using the swing timer, not the utility timer.
Here is the docs for it:
http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html

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.



PARTNERS