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: JPanel motion

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

    Default JPanel motion

    Hi!

    I'm working on a simple application where is an JFrame, and 2 JPanels on it.
    I would like to write an simple motion with these JPanels. I implemented a simple version, but there is some flickering on the screen. How could I solve it?
    Here is my code:
    Jpanel c3 = new JPanel();
    Graphics g = this.getGraphics();
    c3.setLocation(30, 30);
    for(int i=0;i<100;i++)
    {
    c3.setLocation(130 + i, 30);
    this.update(g); //this is currently the JFrame.update
    try
    {
    Thread.sleep(10);
    }
    catch(InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    Please help me...


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JPanel motion

    You're going to have to define "motion". And what thread is that sleep on? It looks like it's on the EDT, which is pretty horrible. But you also don't want to do GUI updates on a thread that isn't the EDT. So I suggest you look into using a Swing Timer.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JPanel motion

    But it seems I was wasting my time trying to help you anyway, as you've already received the same advice somewhere else:
    This thread has been cross posted here:

    http://www.java-forums.org/new-java/40161-jpanel-motion.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting
    http://www.javaprogrammingforums.com...s-posting.html

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Acessor Method Problems for Projectile Motion
    By Kaiki in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 28th, 2011, 08:01 PM
  2. slow motion replays of previous games
    By WhatIsJavaAnyhow in forum Java Theory & Questions
    Replies: 10
    Last Post: December 3rd, 2010, 03:45 PM
  3. Motion Tracking Questions
    By Brt93yoda in forum Java Theory & Questions
    Replies: 6
    Last Post: August 25th, 2010, 10:19 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM