Trouble with a simple animation

Thread Starter

krow

Joined May 25, 2010
49
I'm trying to write a simple animation but I'm having serious trouble trying to understand the concepts, the animation itself does not matter (it's just a simple line) , I just want to see something "moving" on the screen.

The problem is in adding the line to the panel I'm also having trouble in the main method, I'd appreciate it if someone could help me, thanks.

Rich (BB code):
import java.awt.*;
import javax.swing.*;
 
public class NewClass extends JComponent implements Runnable {
 
    int i = 1;
 
    public void paint(Graphics g) {
 
        i++;
        g.drawLine(i,50 ,50,50);
        repaint();
    }
 
    public void run() {
 
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
 
        }
 
    }
}
 
class classs {
 
    public static void main(String args[]) {
 
        NewClass p = new NewClass();
 
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.setSize(200,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setVisible(true);
 
 
 
    }
}
 
Top