Java 重置JFrame视图的按钮

Java 重置JFrame视图的按钮,java,swing,Java,Swing,我必须写一个程序,生成20个半径长度随机的圆。如果这些圆中的任何一个与另一个相交,则该圆必须为蓝色,如果不相交,则颜色为红色。我还必须在JFrame上放置一个按钮。如果按下此按钮,则需要清除JFrame,并根据相同的颜色规则生成一组新的20个圆圈。我对JavaSwing非常陌生,我真的被卡住了。除了按钮,我什么都能用。我无法生成一组新的圆。任何帮助都将不胜感激。多谢各位 import java.awt.Graphics; import javax.swing.JPanel; import jav

我必须写一个程序,生成20个半径长度随机的圆。如果这些圆中的任何一个与另一个相交,则该圆必须为蓝色,如果不相交,则颜色为红色。我还必须在JFrame上放置一个按钮。如果按下此按钮,则需要清除JFrame,并根据相同的颜色规则生成一组新的20个圆圈。我对JavaSwing非常陌生,我真的被卡住了。除了按钮,我什么都能用。我无法生成一组新的圆。任何帮助都将不胜感激。多谢各位

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();
             }
         }
    }
}
导入java.awt.Graphics;
导入javax.swing.JPanel;
导入java.util.Random;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类相交圆扩展JPanel
{
私有int[]xAxis=new int[20];//用于保存x轴点的数组
private int[]yAxis=new int[20];//用于保存y轴点的数组
private int[]radius=new int[20];//保存半径长度的数组
公共静态void main(字符串[]args)
{
JFrame=新的JFrame(“随机圈”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(新的相交圆());
frame.pack();
frame.setVisible(true);
}
公共交叉圈()
{
setPreferredSize(新维度(1000800));//设置窗口大小
随机=新随机();
//创建圆的坐标
对于(int i=0;i<20;i++)
{
xAxis[i]=random.nextInt(700)+100;
yAxis[i]=random.nextInt(500)+100;
半径[i]=随机。下一个(75)+10;
}
}
公共组件(图形g)
{   
//添加按钮以再次运行
JButton btnAgain=新JButton(“再次运行”);
b.立根(850,10100,30);
加上(b纳加因);
addActionListener(新按钮ClickListener());
//确定圆是否相交,创建圆,为圆着色
对于(int i=0;i<20;i++)
{   
int color=0;
对于(int h=0;h<20;h++)
{               
如果(i!=h)
{
双x1=0,x2=0,y1=0,y2=0,d=0;
x1=(X轴[i]+半径[i]);
y1=(yAxis[i]+半径[i]);
x2=(x轴[h]+半径[h]);
y2=(yAxis[h]+半径[h]);
d=(数学sqrt((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
如果(d>radius[i]+radius[h]|d<(Math.abs(radius[i]-radius[h]))
{
颜色=0;
}
其他的
{
颜色=1;
打破
}
}
}
如果(颜色==0)
{
g、 setColor(Color.RED);
g、 绘制椭圆(X轴[i],Y轴[i],半径[i]*2,半径[i]*2);
}
其他的
{
g、 setColor(Color.BLUE);
g、 绘制椭圆(X轴[i],Y轴[i],半径[i]*2,半径[i]*2);
}
}   
}
私有类按钮ClickListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
String action=e.getActionCommand();
if(action.equals(“再次运行”))
{
新的相交圆();
}
}
}
}
建议:

  • 为您的类提供一个创建随机圆并调用repaint()的方法
  • 此方法应创建圆并将其添加到ArrayList中
  • 考虑使用Ellipse2D来表示圆,这样就有了一个
    ArrayList
  • 在类构造函数中调用此方法
  • 在按钮的ActionListener中再次调用它
  • 切勿在paintComponent方法中添加按钮或更改类的状态。此方法用于绘制圆,仅绘制圆,仅此而已。每次调用paintComponent方法时,您都将按照自己的方式创建按钮,因此可能会不必要地创建许多jbutton
  • 不必要地减慢了你的时间紧迫的绘画方法
  • 而是在构造函数中添加按钮
  • 确保在paintComponent方法中调用super.paintComponent(g),作为其第一次调用。这将在需要时清除旧的圆圈
  • 同样在paintComponent中,遍历圆的数组列表,绘制每个圆

在这个网站上搜索了一番后,我找到了我需要的东西。现在一切似乎都好了。谢谢

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);
            }
        }   
    }
}
导入java.awt.Graphics;
导入javax.swing.JPanel;
导入java.util.Random;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类相交圆扩展JPanel
{
私有int[]xAxis=new int[20];//用于保存x轴点的数组
private int[]yAxis=new int[20];//用于保存y轴点的数组
private int[]radius=new int[20];//保存半径长度的数组
公共静态void main(字符串[]args)
{
JFrame=新的JFrame(“随机圈”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(新尺寸(1000800));
ActionListener再次运行=新建ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件c)
{
frame.getContentPane().add(新的相交圆());
frame.pack();
}
};
JButton btnAgain=新JButton(“再次运行”);
b.立根(850,10100,30);
btnAgain.addActionL
 private class ButtonClickListener implements ActionListener
    {
         public void actionPerformed(ActionEvent e)
         {
            repaint();
         }
    }