用于重置JFrame视图的按钮

时间:2015-01-24 04:46:37

标签: java swing

我必须编写一个程序,生成20个随机半径长度的随机圆。如果这些圆中的任何一个与另一个圆相交,则圆必须为蓝色,如果它不相交,则颜色为红色。我还必须在JFrame上放置一个按钮。如果按下此按钮,则需要清除JFrame,并按照相同的颜色规则生成一组20个圆圈。我是Java Swing的新手,我真的被卡住了。除按钮外我一切正常。我无法生成一组新的圆圈。任何帮助将不胜感激。谢谢。

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

3 个答案:

答案 0 :(得分:2)

建议:

  • 为您的班级提供一种创建随机圈子并调用repaint()
  • 的方法
  • 此方法应创建圆圈并将其添加到ArrayList中。
  • 考虑使用Ellipse2D来表示您的圈子,因此您拥有ArrayList<Ellipse2D>
  • 在类构造函数中调用此方法。
  • 在按钮的ActionListener中再次调用它。
  • 从不在paintComponent方法中添加按钮或更改类的状态。此方法用于绘制圆圈并仅绘制它们,仅此而已。您每次调用paintComponent方法时都会创建按钮,因此可能会不必要地创建许多JButtons
  • 并且不必要地放慢你的时间关键绘画方法。
  • 而是在构造函数中添加按钮。
  • 请务必在paintComponent方法中调用super.paintComponent(g)作为第一次调用。这将在需要时清除旧圈子。
  • 同样在paintComponent中,遍历圆圈的ArrayList,绘制每个圆圈。

答案 1 :(得分:0)

在本网站上搜索一下后,我发现了我需要的东西。一切似乎现在都有效。感谢。

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

答案 2 :(得分:0)

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

也许你可以尝试一下......