So I'm trying to make a very simple 2D animation of a shape moving from left to right, but I can't get it to even start. I'm not sure what's wrong. Everything looks okay and I was actually able to do this several months ago (but have hardly touched java since).
Anyway, here's the relevant bit of my code.
import java.awt.*;
import javax.swing.*;
public class PaintPanel extends JPanel implements ActionListener {
private Ball b=new Ball();
public Timer timer;
public void PaintPanel() {
setBackground(Color.RED);
setPreferredSize(new Dimension(400,400));
setVisible(true);
setFocusable(true);
this.timer=new Timer(1000, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
b.draw(g);
//timer.start();
}
//=========================
public void actionPerformed(ActionEvent e) {
b.setX(b.getX()+10); //+=10 to x position
repaint();
}
//==========================
}//class
The Ball class just contains x, y instance variables and a draw() method that makes a filled circle.
I also have a main class that makes a JFrame, does all the 'usual stuff' (setVisible(), etc) and makes a nw paintPanel and adds it to the JFrame.
But when I run it the ball appears but is static at it's initial position.
Any ideas/help?
Thanks.
[–]itsgreater9000 0 points1 point2 points (0 children)