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 780101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:01:31+00:00 2026-05-14T20:01:31+00:00

First of all here are some code snippets: public void startThread() { this.animationThread =

  • 0

First of all here are some code snippets:

public void startThread() {
    this.animationThread = new Thread(this);
    this.animationThread.start();
    try {
        this.animationThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Override
public void run() {     
    pirateMainAnimation.animate();
}

public void animate() {
    for (int j = 0; j < 9; j++) {
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            break;
        }
        PirateAnimationPanel.getInstance().setCurrent(j);
        PirateAnimationPanel.getInstance().repaint();
    }
}

I’m trying to animate some images. The thing is that I want the main thread to wait for the animation thread to finish and then to continue. I searched around, read a little bit and decided to use the join() method. It perfectly waits for the thread to finish but I doesn’t animate correctly. The repaint() method gets called 2 times instead of nine. I think maybe the problem is because I used singletons. Here is the singleton implementation.

import java.awt.Graphics;
import java.awt.MediaTracker;

import javax.swing.JPanel;

import uk.ac.aber.dcs.piratehangman.animation.PirateMainAnimation;
import uk.ac.aber.dcs.piratehangman.utilityclasses.AnimationThread;

@SuppressWarnings("serial")
public class PirateAnimationPanel extends JPanel {
    private int current;
    private MediaTracker mTracker;
    private PirateMainAnimation pirateMainAnimation;
    private AnimationThread animationThread;

    private PirateAnimationPanel() {
        this.current = 0;
        this.pirateMainAnimation = new PirateMainAnimation();
        mTracker = new MediaTracker(this);
        this.animationThread = new AnimationThread();
        setMediaTracker();
        repaint();
    }

    private void setMediaTracker() {
        for (int i = 0; i < 9; i++) {
            mTracker.addImage(
                    this.pirateMainAnimation.getImagesForAnimation()[i],
                    this.pirateMainAnimation.getImagesForAnimationID()[i]);
            try {
                mTracker.waitForID(this.pirateMainAnimation
                        .getImagesForAnimationID()[i]);
            } catch (InterruptedException e) {
                System.out.println("Error loading image: " + i);
            }
        }
    }

    public void playAnimation() {
        this.animationThread.startThread();
    }

    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        System.out.println("called");
        g.drawImage(this.pirateMainAnimation.getImagesForAnimation()[current],
                0, 0, this);
    }

    private static class PirateAnimationPanelHolder {
        private static final PirateAnimationPanel pirateAnimationPanel =
            new PirateAnimationPanel();
    };

    public static PirateAnimationPanel getInstance() {
        return PirateAnimationPanelHolder.pirateAnimationPanel;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public int getCurrent() {
        return current;
    }
}
  • 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-05-14T20:01:32+00:00Added an answer on May 14, 2026 at 8:01 pm

    I think you mean that the paintComponent() methods only gets called twice. Also I think you should be able to remove the call to super.paintComponents() if you fill the component to the background color.

    The repaint() method only marks the component as dirty and requests a re-render on the next paint.

    I would have expected the Swing thread to be able to repaint within the 250ms but I’m not sure what other work is being done/rendered. You might want to put a call to MediaTracker.waitForAll() before the animation.

    While the static singleton is not adding much I don’t think it is causing a problem (in this case).

    Update:
    So the problem is that the join() is on the Swing event Thread which is blocking the repainting of the component. I suggested a call like the following to show the “new game dialog after the last animation:

    SwingUtilities.invokeLater(new Runnable() {
        public void run() { showDialog(); }
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I did something very similar to this (except with PHP/MySQL).… May 15, 2026 at 9:04 am
  • Editorial Team
    Editorial Team added an answer If you wish to log custom events, you may consider… May 15, 2026 at 9:04 am
  • Editorial Team
    Editorial Team added an answer readLine() is a rather particular about what constitutes a line… May 15, 2026 at 9:04 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.