-3

I am trying to draw multiple circles using Array list without over lapping so I am using the CheckDistance function to check the x an y of the next shape that is gonna be drawn but it shows me an error of stackoverflow

I have to save them in array to be able to retrieve each shape to do some other functionality on it.

here is my code:

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

    public Algo() 
    {
        EventQueue.invokeLater(new Runnable() 
        {
            @Override

            public void run()
            {
                try 
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } 
                catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 

                {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Airport");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(new CountriesPane());

                frame.pack();

                frame.setSize(700, 700);

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);
            }
        });
    }

    public class CountriesPane extends JPanel
    {
        private List<Country> Countries = new ArrayList<>(100);

        public CountriesPane()
        {
            for (int index = 0; index < 50; index++) 
            {
                Countries.add(new Country());
            }
        }

        protected void paintComponent(Graphics g) 
        {
            super.paintComponent(g);

            for (Country Country : Countries)
            {
                Graphics2D g2d = (Graphics2D) g.create();

                Country.paint(g2d);

                g2d.dispose();
            }
        }
    }

    protected static final Random random = new Random();

    public double x1;

    public double y1;

    public class Country
    {
        private int height = 20;

        private int width = 20;

        private double x2;

        private double y2;

        private double space = 10;

        private Ellipse2D shape;

        private String Name;

        public Country()
        {
            CheckDistance(this);

            shape = new Ellipse2D.Double(x2, y2, width, height);
        }

        public void paint(Graphics2D g2d)
        {
            g2d.setColor(Color.BLUE);

            g2d.fill(shape);
        }
    }

    public void CheckDistance (Country Co) 
    {
        Co.x2 = random.nextInt() + Co.space;

        Co.y2 = random.nextInt() + Co.space;

        double distancex = Co.x2 - x1;

        double distancey = Co.y2 - y1;

        if (distancex <= Co.width || distancey <= Co.height)
        {
            CheckDistance(Co);
        }
        else
        {
            x1 = Co.x2;

            y1 = Co.y2;
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
TheGrimBoo
  • 199
  • 1
  • 4
  • 14
  • 1
    Why did you set an initial capacity for your arraylist? ArrayLists are meant to change sizes, making an initial capacity doesn't make much sense (just use an array if you have a capacity!) – Sean May 21 '18 at 19:31
  • 2
    You'd be better to use a loop, rather than recursion, in `CheckDistance`. And if the method hangs, use your debugger to find out why. – Dawood ibn Kareem May 21 '18 at 19:40
  • 1
    `random.nextInt()` can return some large numbers. You should at least normalize those values to be within the window dimensions. – 001 May 21 '18 at 19:43
  • @Sean Setting an initial capacity for an array list can make sense as a form of optimization (although that doesn't really apply here). It can save additional garbage from the array within the array list be reallocated, and it can save space. For example storing 100 objects in an array list with default initial capacity will allocate 4 arrays (and throw away 3) of 16, 32, 64 and 128 items (iirc, didn't double check), while setting initial size to 100 will allocate 1 array, of size 100. – Mark Rotteveel May 22 '18 at 20:04
  • @Sean Correction, with Java 10 it will allocate 7 arrays (and throw away 6) of 10, 15, 22, 33, 49, 73 and 109 items. – Mark Rotteveel May 22 '18 at 20:11

1 Answers1

0

This can go endless loop(or too much loop).

   public void CheckDistance ( Country Co) 

    {

        Co.x2 = random.nextInt() + Co.space;

        Co.y2 = random.nextInt() + Co.space;

        double distancex = Co.x2 - x1;

        double distancey = Co.y2 - y1;

        if (distancex <= Co.width || distancey <= Co.height) 

        {

            CheckDistance(Co);

        }

        else 

        {

            x1 = Co.x2;

            y1 = Co.y2;

        }

Because it could forever be distancex <= Co.width || distancey <= Co.height depending on the random value.