Trouble with a simple animation

Discussion in 'Programmer's Corner' started by krow, Sep 23, 2011.

  1. krow

    Thread Starter Member

    May 25, 2010
    49
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. import java.awt.*;
    4. import javax.swing.*;
    5.  
    6. public class NewClass extends JComponent implements Runnable {
    7.  
    8.     int i = 1;
    9.  
    10.     public void paint(Graphics g) {
    11.  
    12.         i++;
    13.         g.drawLine(i,50 ,50,50);
    14.         repaint();
    15.     }
    16.  
    17.     public void run() {
    18.  
    19.         for (int i = 0; i < 10; i++) {
    20.             System.out.println(i);
    21.             try {
    22.                 Thread.sleep(1000);
    23.             } catch (InterruptedException ex) {
    24.             }
    25.  
    26.         }
    27.  
    28.     }
    29. }
    30.  
    31. class classs {
    32.  
    33.     public static void main(String args[]) {
    34.  
    35.         NewClass p = new NewClass();
    36.  
    37.         JFrame frame = new JFrame();
    38.         JPanel panel = new JPanel();
    39.         frame.setSize(200,200);
    40.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    41.         frame.add(panel);
    42.         frame.setVisible(true);
    43.  
    44.  
    45.  
    46.     }
    47. }
    48.  
     
Loading...