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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:42:59+00:00 2026-05-27T14:42:59+00:00

I have a thread which drops a circle in the y direction. I want

  • 0

I have a thread which drops a circle in the y direction. I want to now create several circles on screen dropping at the same time with random x positions.

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Goo 
{
protected GooPanel gooPanel;
private boolean loop = true;
protected int width , height;
private int frameTimeInMillis = 50;
private RenderingHints renderingHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING , RenderingHints.
VALUE_ANTIALIAS_ON);
@SuppressWarnings("serial")

class GooPanel extends JPanel 
{
    public void paintComponent(Graphics g) 
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHints(renderingHints);
        draw(g2d);
    }
}

public Goo() 
{
    this (800, 500);
}

public Goo(int w, int h) 
{
    width = w;
    height = h;
    JFrame frame = new JFrame ();

    frame.setSize(width , height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gooPanel = new GooPanel ();
    gooPanel.setPreferredSize(new Dimension(w, h));
    frame.getContentPane ().add(gooPanel);
    frame.pack();
    frame.setVisible(true);
}

public void go() 
{
    while (loop) 
    {
        gooPanel.repaint();
        try 
        {
            Thread.sleep(frameTimeInMillis);
        } catch (InterruptedException e) {}
    }
}

public void draw(Graphics2D g) {}

public void setFrameTime(int millis)
{
    frameTimeInMillis = millis;
}

public Component getGooPanel () 
{
    return gooPanel;
}
}

My FallingDrop class:

import java.awt.*;

public class FallingDrops extends Goo
{
    double x, y, r;
    int red, green, blue = 0;
    Color a;

FallingDrops() 
{
    x = width / 2;
    r = 10;
    y = -r;
}

FallingDrops(double x) 
{
    this.x = x;
    r = 10;
    y = -r;
}

public void draw(Graphics2D g) 
{
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, width , height);
    g.setColor(Color.WHITE);

    g.fillOval ((int) (x - r), (int) (y - r), (int) (2 * r),
            (int) (2 * r));

    y++;
    if (y - r > height)
        y = -r;

}


public static void main(String [] args) 
{
    int num = 10;
    Goo gooDrop [] = new FallingDrops[num];

    for(int i = 0; i < gooDrop.length; i++)
    {
        double x = Math.random()*800;
        gooDrop[i] = new FallingDrops(x);
        System.out.println(x);
        gooDrop[i].go();
    }

}


}

At current, the loop fails to complete when the go() method is executed; thus only painting ONE object on screen, and not several as indicated in my loop. This is a simple fix I am sure. Any ideas what I am doing wrong?

  • 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-27T14:43:00+00:00Added an answer on May 27, 2026 at 2:43 pm

    The method go() never returns. when it is called on the first object in the array, it continues working infinitely. you should either make the repainting in a separate thread that is constantly repainting. or if you want repainting only when drops are added, then remove the while in your go method

    public void go() 
    {
    
            gooPanel.repaint();
            try 
            {
                Thread.sleep(frameTimeInMillis);
            } catch (InterruptedException e) {}
    
    }
    

    this way it will returns after it had made a repaining and a pause.

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

Sidebar

Related Questions

I have a thread which performs some tasks. At the end I want to
I have a thread which must wait several objects from different threads. @Override public
I have a thread which blocks itself on some lock.Now For some condition I
I have a thread which handles all the HTTP Connections to the server and
i have this thread which run as a service: public void run() { try
I have an application where i have created a thread which must check the
I am building an application in swt, I also have a thread running which
I have the main thread from which I start a window using invokeLater .
I have a thread pool (executor) which I would like to monitor for excessive
I have created a GUI for starting a Thread which does something very simple.

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.