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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:27:41+00:00 2026-06-15T19:27:41+00:00

So I’m working on a small exercise in programming Java, I’ve created a couple

  • 0

So I’m working on a small exercise in programming Java, I’ve created a couple classes that simply draws a black background, and then, from the center, draws circles a radius apart from eachother in 90 degree rotations (code at bottom of post).

Now, just using a “random” seed to determine direction of travel, this always trends downward. Every time. So what I want to do now, is try and show my circles where the other circles are, and give them an inclination to bend their movement towards another circle (without existing ON another circle other than the parent). I can handle that part, but I want an efficient means of communicating location between my “nodes” or “circles”. I could build a string array with x,y and read through that list each time I am constructing my seed, but this seems like an inefficient way of doing it. I would much rather find some way where a circle can look around it and find other circles within proximity. Is there any way to do this? I don’t mind some reading and homework, I’m mostly looking for a good strategy to start looking at.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FlowerOfLife extends JFrame {
    private Circle Origin = null;



    public FlowerOfLife() {
        // Default layout manager is BorderLayout.  
        // Adding graphic component to center section will expand  
        // it to fill the available space so it does not need to  
        // provide any size hints for this parent container now.  
        GraphicPanel graphicPanel = new GraphicPanel();
        add(graphicPanel);     // default center section  
        setDefaultCloseOperation(EXIT_ON_CLOSE);  
        setSize(400,400);  
        setLocation(200,200);  
        setVisible(true);   
    }

    /** 
     * Container method draws container and passes graphics context 
     * on to components for them to draw themselves. You can draw 
     * over everything in the content pane with this method... 
     */  
    public void paint(Graphics gr)  
    {  
        Graphics2D g = (Graphics2D)gr;
        // see what happens when you remove/move this next line  
        super.paint(g);  

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                            RenderingHints.VALUE_ANTIALIAS_ON);  
        int w = getWidth();  
        int h = getHeight();
        g.setPaint(Color.white);//First there is one...
        Origin = new Circle(g, w/2, h/2, ((w+h)/2)/35);
        g.setPaint(Color.white.darker());

        Circle[] circleArray = new Circle[100];
        for(int i=0;i<circleArray.length;i++){
            circleArray[i] = new Circle(g,i>0?circleArray[i-1]:Origin);
        }


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

}

class Circle{
    public int x;
    public int y;
    public int r;
    private Circle next;
    private Circle last;
    private int    seed;

    public Circle(Graphics2D g, Circle c){ 
        c.next = this;
        this.last = c;
        this.r  = c.r; //Copy radius
        //We set these now, and modify the necessary ones.
        this.x = c.x;
        this.y = c.y;
        this.r = c.r;

        //Move the new circle into position based on seed.
        this.seed = (int) (Math.random()*4);
        if(this.seed==0){//Stay here, and grow a little...
//          this.r+=1;
        }else if(this.seed==1){//Move left, by r.
            this.x-=c.r;    
        }else if(this.seed==2){//Move up,   by r.
            this.y+=c.r;
        }else if(this.seed==3){//Move right,by r.
            this.x+=c.r;
        }else{//(this.seed==4) //Move down, by r.
            this.y-=c.r;
        }
        sleep((int)(Math.random())*9000+100);//Random(ish) life.
        //Draw the new circle
        g.draw(new Ellipse2D.Double(x,y,r*2,r*2));


    }
    public Circle(Graphics2D g,int x, int y, int r){
        /**
         *  The ellipse draws from the designated point, down and right.
         * The below permits the center to be declared in a more natural
         * way. Used for the first declaration.
        **/
        this.last = null; //Parent.
        this.x = x-r;
        this.y = y-r;
        this.r = r;
        this.seed = 0;
        g.draw(new Ellipse2D.Double(x,y,r*2,r*2));
    }
    private void sleep(int ms){
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


class GraphicPanel extends JPanel  
{  
    protected void paintComponent(Graphics g)  
    {  
        super.paintComponent(g);  
        Graphics2D g2 = (Graphics2D)g;  
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                            RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.black);
        g2.fill(new Rectangle2D.Double(0,0,getWidth(),getHeight()));
    }  
}  
  • 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-15T19:27:42+00:00Added an answer on June 15, 2026 at 7:27 pm

    Your circles can exist on a “board”, which would be a two-dimensional array. You could then give each circle a view of the board. You could also have the board determine where to put the next circle.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.