adidashawn6
adidashawn6

Reputation: 49

Button To Reset The View Of JFrame

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();
             }
         }
    }
}

Upvotes: 1

Views: 468

Answers (3)

StinsonZhao
StinsonZhao

Reputation: 39

 private class ButtonClickListener implements ActionListener
    {
         public void actionPerformed(ActionEvent e)
         {
            repaint();
         }
    }

Maybe you can have a try...

Upvotes: 0

adidashawn6
adidashawn6

Reputation: 49

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);
            }
        }   
    }
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285460

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.

Upvotes: 2

Related Questions