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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:12:22+00:00 2026-06-14T12:12:22+00:00

I have a program (an AWT Frame, yes I know that Swing is better

  • 0

I have a program (an AWT Frame, yes I know that Swing is better but I am restricted from using it) that has 2 moving objects. So I thought I would put each in its own thread to allow the objects to move at different speeds, etc.

One thread (gameThread) handles a bouncing ball, and the other (cannonThread) handles a cannon drawn on the screen and the projectiles fired by it. I was unsure of how to seperate the ball’s speed and the projectile’s speed which is why I thought 2 threads would work (by using thread.sleep(speedofobject)).

I don’t know how to implement them and my random guess (obviously) didn’t work. Nothing shows up on the screen and there are no errors generated at compile time. Previously, the ball would show up on the screen and move around as it should.

Here’s a snippet from where I tried to do multiple threads. If you need more information, let me know and I’ll post it.

public void start()
{
    if (gameThread == null)
    {
        gameThread = new Thread(this);
        gameThread.start();
    }
    if (cannonThread == null)
    {
        cannonThread = new Thread(this);
        cannonThread.start();
    }       
}

public void run()
    {
        //thread for the ball, collision detection and scorekeeping
        if (Thread.currentThread().equals(gameThread))
        {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            while (!kill)
            {
                if (!paused)
                {
                    target.repaint();
                }
                try
                {
                    Thread.sleep(ballSpeed);
                }
                catch(InterruptedException e){System.err.println("Interrupted.");}
            }
            stop(); 
        }

        //thread for the cannon and projectile
        if (Thread.currentThread().equals(cannonThread))
        {
            Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
            if (!paused)
            {
                if (projectileFiring)
                {
                    cannon.repaint();
                }
                try
                {
                    Thread.sleep(projectileSpeed);
                }
                catch(InterruptedException e){System.err.println("Interrupted.");}
            }
        }   
    }
  • 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-14T12:12:24+00:00Added an answer on June 14, 2026 at 12:12 pm

    Having a separate thread for each object in your world is not the way to go. One way to deal with this is to have a single separate thread that is responsible for updating the “world model” (in your case, the ball and the cannon). This world model thread would run the following animation loop:

    1. sleep for a short time
    2. update the data representing the ball and cannon based on the current time
    3. Signal that the UI needs to be updated with the current model

    Note that with this approach, you will need to synchronize access to the model data, because it will be updated by the animation loop in one thread and read by the rendering code to update the UI. You don’t want the data changing as you are reading it.

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

Sidebar

Related Questions

I have a program written in AWT, so I am using Frame (Not JFrame/Swing).
I have a program that gets a JSON from the server using getJSON and
I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it
I have a program that is not showing any errors in Eclipse, but whenever
I have a GUI program that, in this example, downloads an exe using wget
I have written a small program, while reading a book about swing, that creates
I have just started on AWT and made a simple program in it, it
I have program, that must interact with a console program before my program can
I have program that runs fast enough. I want to see the number of

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.