Timers

To use Timers, I must implement ActionListener.But I'm trying to activate my timer object by moving my mouse.Is it possible for java to do it? If yes, can show me some codes on how to do it please ?Thanks : )
[237 byte] By [RainbowEnergiesa] at [2007-9-28 7:32:04]
# 1

This or something like it may or may or not be ... useful-to-you/ what-you're-looking-for ? You say 'moving the mouse', that's a bit vague ...

import java.util.Random;

import javax.swing.*;

import java.awt.event.*;

public class junk extends JFrame implements ActionListener {

JButton a,b;

JLabel l;

java.text.DecimalFormat d = new java.text.DecimalFormat("#0.##");

double r,s;

String result ="";

public junk(){

super("Timer Buttons");

JPanel p = new JPanel();

p.setSize(200,200);

p.setOpaque(false);

p.setLayout(new java.awt.FlowLayout() );

a = new JButton("first");

a.addActionListener(this);

p.add(a);

b = new JButton("last");

b.addActionListener(this);

p.add(b);

l = new JLabel(result);

p.add(l);

getContentPane().add(p);

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==a) r = System.currentTimeMillis();

if(e.getSource()==b) {

s = System.currentTimeMillis();

s -=r;

s /=1000;

l.setText("That took "+d.format(s)+" seconds");

}

}

public static void main(String[] args) {

junk frame = new junk();

frame.setSize(200,200);

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.setVisible(true);

}

}

Sum-shusSuea at 2007-7-9 18:44:54 > top of Java-index,Other Topics,Java Game Development...
# 2
what do timers have to do with the mouse. please explain, I am currious too.....
Exception13a at 2007-7-9 18:44:55 > top of Java-index,Other Topics,Java Game Development...
# 3
I mean when I click on a position with my mouse, the animation will start (using timer object)Thanks for your help : )
RainbowEnergiesa at 2007-7-9 18:44:55 > top of Java-index,Other Topics,Java Game Development...
# 4
> I mean when I click on a position with my mouse, the> animation will start (using timer object)> You mean a Thread?
FoxyLadya at 2007-7-9 18:44:55 > top of Java-index,Other Topics,Java Game Development...
# 5

> I mean when I click on a position with my mouse, the

> animation will start (using timer object)

You will need a MouseListener for that (or MouseAdapter if you are lazy, but adapters are evil !) and override the mouseClicked method. This mouseClicked method should then start your Timer. Question remains: does the animation needs to be canceled and restarted every time you click ? If yes, then stop the timer and start of a new one. If no, just ignore the clicks until the animation is finished.

It would be a lot easier for me to answer if you gave more information ...

jpwinne

jpw35a at 2007-7-9 18:44:55 > top of Java-index,Other Topics,Java Game Development...