Salve. Ho questo file principale:
codice:
import java.awt.*;
import javax.swing.*;
public class Biliardo extends JPanel
{
int raggio;
int millis;
boolean Dx;
Point Coords;
Timer timer;
public Biliardo(int rg, int ms)
{
raggio = rg;
millis = ms;
Dx = true;
Coords = new Point();
timer = new Timer (millis, new TimerHandler());
timer.start();
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Dimension d = getSize();
Coords.y = d.height;
g.setColor (Color.GREEN);
g.fillOval (Coords.x, Coords.y / 2 - raggio, raggio * 2, raggio * 2);
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Biliardo");
frame.setSize (200, 160);
frame.setLocationRelativeTo (null);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Biliardo panel = new Biliardo(50, 50);
panel.setBackground (Color.WHITE);
frame.add (panel);
frame.setVisible (true);
}
}
Quando faccio timer = new Timer (millis, new TimeHandler()), inizializzo il timer e come secondo parametro gli passo la classe che gestisce gli eventi, ma mi da errore...
la classe TimerHandler è questa:
codice:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class TimerHandler extends Biliardo implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
Dimension d = getSize();
if (Dx)
{
if (Coords.x >= d.width - 2 * raggio)
{
Dx = false;
}
else
{
Coords.x++;
}
}
else
{
if (Coords.x <= 0)
{
Dx = true;
}
else
{
Coords.x--;
}
}
repaint();
}
}
Cosa c'è che sbaglio?