Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Having trouble with a simple animation

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble with a simple animation

    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.

     
    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);
     
     
     
        }
    }
    Last edited by knightmetal; September 23rd, 2011 at 07:53 AM. Reason: Wrong formatting


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Having trouble with a simple animation

    a) Override paintComponent, not paint b) Do not call repaint within either paint or paintComponent - this should be called during the animation (in your animation thread or Swing Timer) c) You must add the component you are painting to to the displayed component (your JFrame or the components it contains) d) You implement Runnable, but never use it (you must start a thread to do so) e) You might want to consider using a Swing Timer to do the painting. I know these are a lot of pieces of advice, but take them one step at a time. First get your item displayed (add it to the JPanel or JFrame), then work from there.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with a simple animation

    Thanks copeg, yes it's a lot to take care of but I'll do what you suggested, one step at a time.

Similar Threads

  1. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  2. Simple GUI/Animation question...
    By teslatrain in forum AWT / Java Swing
    Replies: 3
    Last Post: April 29th, 2011, 04:10 PM
  3. Project with animation.
    By ryainad in forum Paid Java Projects
    Replies: 3
    Last Post: April 10th, 2011, 02:59 PM
  4. Basic Animation
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 10:07 AM
  5. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM