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

  • Home
  • SEARCH
  • 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 8729107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:46:30+00:00 2026-06-13T08:46:30+00:00

In this simple code example for an animation of a bouncing ball: import javax.swing.JApplet;

  • 0

In this simple code example for an animation of a bouncing ball:

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

public class GraphicsMovement extends JApplet
{
public static void pause()
{
    try {
        Thread.sleep(10);
        } catch(InterruptedException e) {
          }
}

public static void main(String args[])
{
    JApplet example = new GraphicsMovement();
    JFrame frame = new JFrame("Movement");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(example);
    frame.setSize(new Dimension(500,300));       //Sets the dimensions of panel to appear when run
    frame.setVisible(true);
}

  public void paint (Graphics page)
  {
 int width = getWidth();    // width = the width of the panel which appears when run
 int height = getHeight();  // height = the height of the panel which appears when run.

//Changes background color to a blueish color
page.setColor(new Color (140,214,225));
page.fillRect(0,0,width,height);
for(int i = 0; i <= 5; i++)
{
    for (int j = 0; j <= 100; j++)
    {
        page.setColor(Color.YELLOW);
        page.fillOval(100,55 + j,100,100);  //draws a yellow oval
        pause();
        page.setColor(new Color (140,214,225));
        page.fillOval(100,55 + j,100,100);  //draws a blueish oval over the yellow oval
    }
    for (int k = 100; k >= 0; k--)
    {
        page.setColor(Color.YELLOW);
        page.fillOval(100,55 + k,100,100);  //draws a yellow oval
        pause();
        if (k != 0)
        {
            page.setColor(new Color (140,214,225));  //draws a blueish oval over the yellow oval
            page.fillOval(100,55 + k,100,100);
        }
    }
}
 }
 }

The animation is drawn fine and runs on a Windows machine (using JCreator), but will not run on Mac OS X compiled with either IntelliJ or Eclipse. Tried on two different OS X machines, and both will draw the ball and background (after a long wait) but will not proceed with the animation.

Is there some sort of platform-specific code in here that I am missing?
Thanks!

  • 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-13T08:46:31+00:00Added an answer on June 13, 2026 at 8:46 am

    Never do anything in any paint method that would either block or might trigger a repaint request.

    You should always call super.paintXxx, these methods do a lot of work in the background, usually a lot better then you can.

    You shouldn’t need to (very rare cases) extend from top level containers, like JApplet or JFrame. You are better of creating a custom container (such as JPanel) and add your components to it (or perform your custom painting). Apart from the double buffering support, you also gain flexibility in deployment choices.

    Don’t kid yourself, you don’t control the paint process, it’s up to the repaint manager to make those decisions, you can however, “encourage” it to update. This causes the most pain when people start playing with animation.

    You should modify the “state” of the animation outside of the influence of the Event Dispatching Thread (EDT) or in your case, out side the paint context.

    You problem is simple enough that a simple javax.swing.Timer will solve it. More complex animations might require a “animation” thread.

    public class GraphicsMovement extends JApplet {
    
        @Override
        public void init() {
            setLayout(new BorderLayout());
            add(new AnimatedPane());
        }
    
        @Override
        public void start() {
        }
    
        public class AnimatedPane extends JPanel {
    
            private Timer timer;
            private boolean colorSwitch = false;
    
            private int yOffset = 0;
            private int direction = 1;
    
            public AnimatedPane() {
                timer = new Timer(10, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
    //                    colorSwitch = !colorSwitch;
                        yOffset += direction;
                        if (yOffset > 100) {
                            direction = -1;
                            yOffset = 100;
                        } else if (yOffset < 0){
                            direction = 1;
                            yOffset = 0;
                        }
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
    
                setBackground(new Color(140, 214, 225));
            }
    
            @Override
            protected void paintComponent(Graphics page) {
                super.paintComponent(page);
                int width = getWidth();    // width = the width of the panel which appears when run
                int height = getHeight();  // height = the height of the panel which appears when run.
    
                if (colorSwitch) {
                    page.setColor(new Color(140, 214, 225));
                } else {
                    page.setColor(Color.YELLOW);
                }
                page.fillOval(100, 55 + yOffset, 100, 100);  //draws a yellow oval
            }
        }
    }
    

    I’m concerned with the “delay” of 10 milliseconds, it’s enough to want me to face a fit 😛

    You might find reading through…

    • Concurrency
    • Concurrency in Swing
    • How to Use Swing Timers
    • Performing Custom Painting
    • Painting in AWT and Swing

    Of some interest

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Best explained with code I think, this is just a simple example: public class
In this very simple code example, messages get lost every once in a while.
I just tossed this super simple code example into a Flash CS4 IDE frame
I'm using this simple node.js code example but I don't understand why FireBug doesn't
If I have a simple cucumber feature and scenario , like this (example code
take this simple code: class A{ public: virtual void foo() = 0; void x(){
I have a simple animation (only for Safari in this example): h1 { -webkit-animation:
Language: asp This is sample of my code: str = www.example.com/gotobuy.aspx?id=1234 key_word = .obuy.
I have this simple code (for making things shorted, the important bits are probably
I have this simple code, using Joda-time. Works fine, but I have a problem.

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.