1

I have to write a program that will generate 20 random circles with random radius lengths. If any of these circles intersect with another, the circle must be blue, and if it does not intersect, the color is red. I must also place a button on the JFrame. If this button is pressed, it needs to clear out the JFrame, and generate a new set of 20 circles following the same color rules. I am extremely new to Java Swing and am really stuck. I have everything working except the button. I cannot get a new set of circles to generate. Any help would be greatly appreciated. Thank You.

import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class IntersectingCircles extends JPanel
{
    private int[] xAxis = new int [20]; // array to hold x axis points
    private int[] yAxis = new int [20]; // array to hold y axis points
    private int[] radius = new int [20]; // array to hold radius length


    public static void main (String[] args)
    {
        JFrame frame = new JFrame("Random Circles");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new IntersectingCircles());

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

    public IntersectingCircles()
    {
        setPreferredSize(new Dimension(1000, 800)); // set window size

        Random random = new Random();

        // Create coordinates for circles
        for (int i = 0; i < 20; i++)
        {
            xAxis[i] = random.nextInt(700) + 100;
            yAxis[i] = random.nextInt(500) + 100;
            radius[i] = random.nextInt(75) + 10;
        }
    }

    public void paintComponent(Graphics g)
    {   
        // Add button to run again
        JButton btnAgain = new JButton("Run Again");
        btnAgain.setBounds(850, 10, 100, 30);
        add(btnAgain);
        btnAgain.addActionListener(new ButtonClickListener());

        // Determine if circles intersect, create circles, color circles
        for (int i = 0; i < 20; i++)
        {   
            int color = 0;

            for (int h = 0; h < 20; h++)
            {               
                if(i != h)
                {
                    double x1 = 0, x2 = 0, y1 = 0, y2 = 0, d = 0;

                    x1 = (xAxis[i] + radius[i]);
                    y1 = (yAxis[i] + radius[i]);
                    x2 = (xAxis[h] + radius[h]);
                    y2 = (yAxis[h] + radius[h]);

                    d = (Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1)*(y2 - y1))));

                    if (d > radius[i] + radius[h] || d < (Math.abs(radius[i] - radius[h])))
                    {
                        color = 0;
                    }

                    else
                    {
                        color = 1;
                        break;
                    }
                }
            }

            if (color == 0)
            {
                g.setColor(Color.RED);
                g.drawOval(xAxis[i], yAxis[i], radius[i] * 2, radius[i] * 2);
            }

            else
            {
                g.setColor(Color.BLUE);
                g.drawOval(xAxis[i], yAxis[i], radius[i] * 2, radius[i] * 2);
            }
        }   
    }

    private class ButtonClickListener implements ActionListener
    {
         public void actionPerformed(ActionEvent e)
         {
             String action = e.getActionCommand();  
             if(action.equals("Run Again"))
             {
                 new IntersectingCircles();
             }
         }
    }
}
adidashawn6
  • 49
  • 1
  • 6
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 3) See also [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for tips. – Andrew Thompson Jan 24 '15 at 04:50
  • 1
    @HovercraftFullOfEels 1st I'd seen that dross. Done! – Andrew Thompson Jan 24 '15 at 04:54

3 Answers3

2

Suggestions:

  • Give your class a method that creates the random circles and calls repaint()
  • This method should create the circles and add them into an ArrayList.
  • Consider using Ellipse2D to represent your circles, and so you'd have an ArrayList<Ellipse2D>.
  • Call this method in your class constructor.
  • Call it again in the button's ActionListener.
  • Never add button's or change the state of your class from within your paintComponent method. This method is for drawing the circles and drawing them only and nothing more. Your way you will be creating the button each time the paintComponent method is called, so you could be potentially needlessly creating many JButtons
  • and needlessly slowing down your time-critical painting method.
  • Instead add the button in the constructor.
  • Be sure to call super.paintComponent(g) in your paintComponent method as its first call. This will clear the old circles when need be.
  • Also in paintComponent, iterate through the ArrayList of circles, drawing each one.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

After some searching on this website, i found what i needed. Everything seems to work now. Thanks.

import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class IntersectingCircles extends JPanel
{
    private int[] xAxis = new int [20]; // array to hold x axis points
    private int[] yAxis = new int [20]; // array to hold y axis points
    private int[] radius = new int [20]; // array to hold radius length


    public static void main (String[] args)
    {
        JFrame frame = new JFrame("Random Circles");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(1000, 800));

        ActionListener runAgain = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent c)
            {
                frame.getContentPane().add(new IntersectingCircles());
                frame.pack();
            }
        };

        JButton btnAgain = new JButton("Run Again");
        btnAgain.setBounds(850, 10, 100, 30);
        btnAgain.addActionListener(runAgain);


        frame.add(btnAgain);        
        frame.getContentPane().add (new IntersectingCircles());     
        frame.pack();
        frame.setVisible(true);
    }

    public IntersectingCircles()
    {       
        Random random = new Random();

        // Create coordinates for circles
        for (int i = 0; i < 20; i++)
        {
            xAxis[i] = random.nextInt(700) + 100;
            yAxis[i] = random.nextInt(500) + 100;
            radius[i] = random.nextInt(75) + 10;
        }
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        // Determine if circles intersect, create circles, color circles
        for (int i = 0; i < 20; i++)
        {   
            int color = 0;

            for (int h = 0; h < 20; h++)
            {               
                if(i != h)
                {
                    double x1 = 0, x2 = 0, y1 = 0, y2 = 0, d = 0;

                    x1 = (xAxis[i] + radius[i]);
                    y1 = (yAxis[i] + radius[i]);
                    x2 = (xAxis[h] + radius[h]);
                    y2 = (yAxis[h] + radius[h]);

                    d = (Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1)*(y2 - y1))));

                    if (d > radius[i] + radius[h] || d < (Math.abs(radius[i] - radius[h])))
                    {
                        color = 0;
                    }

                    else
                    {
                        color = 1;
                        break;
                    }
                }
            }

            if (color == 0)
            {
                g.setColor(Color.RED);
                g.drawOval(xAxis[i], yAxis[i], radius[i] * 2, radius[i] * 2);
            }

            else
            {
                g.setColor(Color.BLUE);
                g.drawOval(xAxis[i], yAxis[i], radius[i] * 2, radius[i] * 2);
            }
        }   
    }
}
adidashawn6
  • 49
  • 1
  • 6
0
 private class ButtonClickListener implements ActionListener
    {
         public void actionPerformed(ActionEvent e)
         {
            repaint();
         }
    }

Maybe you can have a try...