Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:51:44+00:00 2026-06-17T09:51:44+00:00

I’m attempting to create a simple animation in Java that displays a blue ball

  • 0

I’m attempting to create a simple animation in Java that displays a blue ball moving horizontally part way across a 500 x 500 window. The ball is supposed to move at a rate of 1px/30ms. The problem is, is that the window is only being painted when the while loop exits and not during every iteration of the while loop as I intended. This results in the blue ball being painted in its final location. Could you please tell me what I’m doing wrong here? I’ve also tried executing this code on the EDT with the paintComponent() method and got the same result. In addition, as suggested by other posts, I got the same result when using paintImmediately(0, 0, getWidth(), getHeight()) instead of repaint() while using the EDT and paintComponent() method. I’m trying to do all of this without using timers.

import javax.swing.*;
import java.awt.*;

class AnimationFrame extends JPanel {

    int ovalX = 50;
    long animDuration = 5000;
    long currentTime = System.nanoTime() / 1000000;
    long startTime = currentTime;
    long elapsedTime = currentTime - startTime;

    public AnimationFrame() {
        setPreferredSize(new Dimension(500, 500));
        runAnimation();
    }

    public void runAnimation() {
        while (elapsedTime < animDuration) {
            currentTime = System.nanoTime() / 1000000;
            elapsedTime = currentTime - startTime;
            System.out.println(elapsedTime);
            ovalX = ovalX + 1;
            try {
                Thread.sleep(30);
            }
            catch (Exception e) {
            }
            repaint();
        }
    }

    public void paint(Graphics g) {
        Rectangle clip = g.getClipBounds();
        g.setColor(Color.BLACK);
        g.fillRect(clip.x, clip.y, clip.width, clip.height);
        g.setColor(Color.BLUE);
        g.fillOval(ovalX, 250, 70, 70);
    }

    public static void main(String[] args) {
        createAndShowGUI();
    }

    public static void createAndShowGUI() {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(new AnimationFrame());
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:51:46+00:00Added an answer on June 17, 2026 at 9:51 am

    I looked at your code and noticed that you are calling the method that runs the animation from within the constructor of the “AnimationFrame” which you are adding to your “mainFrame”.

    The problems with doing it that way is that you are trying to animate before the object has finished being constructed, which must be completed before it can be added to the mainFrame, which is not yet made to be visible on the screen.

    I made the follow changes to your code and I see a blue ball move across the frame now.

    import javax.swing.*;
    import java.awt.*;
    
    class AnimationFrame extends JPanel {
    
        int ovalX = 50;
        long animDuration = 5000;
        long currentTime = System.nanoTime() / 1000000;
        long startTime = currentTime;
        long elapsedTime = currentTime - startTime;
    
        public AnimationFrame() {
            setPreferredSize(new Dimension(500, 500));
            //i removed the call to runAnimation from here
    
        }
    
        public void runAnimation() {
            while (elapsedTime < animDuration) {
                currentTime = System.nanoTime() / 1000000;
                elapsedTime = currentTime - startTime;
                System.out.println(elapsedTime);
                ovalX = ovalX + 1;
                try {
                    Thread.sleep(30);
                }
                catch (Exception e) {
                }
                repaint();
            }
        }
    
        @Override
        public void paint(Graphics g) {
            Rectangle clip = g.getClipBounds();
            g.setColor(Color.BLACK);
            g.fillRect(clip.x, clip.y, clip.width, clip.height);
            g.setColor(Color.BLUE);
            g.fillOval(ovalX, 250, 70, 70);
        }
    
        public static void main(String[] args) {
            createAndShowGUI();
        }
    
        public static void createAndShowGUI() {
            JFrame mainFrame = new JFrame();
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            AnimationFrame animationPanel = new AnimationFrame();
            mainFrame.add(animationPanel);
            mainFrame.pack();
            mainFrame.setVisible(true);
            //I made the call to runAnimation here now
            //after the containing frame is visible.
            animationPanel.runAnimation();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.