绘制随机圆圈,将它们的坐标存储在数组中

发布于 2024-10-02 08:53:54 字数 2419 浏览 0 评论 0原文

对于家庭作业,我想在屏幕周围随机绘制圆圈。如果任何圆圈重叠,那么我想填充这些圆圈。我从一些代码开始,只要单击鼠标指针,就会在屏幕上绘制圆圈。我真的很困惑如何使用随机值来确定圆圈以及如何将这些值存储在数组或数组列表中。我认为要填充圆圈,我将只使用 for 语句来比较圆圈中心点之间的距离。非常感谢您的任何建议。这是我试图找出如何修改的出发点:

import java.util.ArrayList;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;

public class DotsPanel extends JPanel
{
   private final int SIZE = 6;  // radius of each dot

   private ArrayList<Point> pointList;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public DotsPanel()
   {
      pointList = new ArrayList<Point>();

      addMouseListener (new DotsListener());

      setBackground (Color.black);
      setPreferredSize (new Dimension(300, 200));
   }

   //-----------------------------------------------------------------
   //  Draws all of the dots stored in the list.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent(page);

      page.setColor (Color.green);

      for (Point spot : pointList)
         page.fillOval (spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);

      page.drawString ("Count: " + pointList.size(), 5, 15);
   }

   //*****************************************************************
   //  Represents the listener for mouse events.
   //*****************************************************************
   private class DotsListener implements MouseListener
   {
      //--------------------------------------------------------------
      //  Adds the current point to the list of points and redraws
      //  the panel whenever the mouse button is pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         pointList.add(event.getPoint());
         repaint();
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event) {}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
   }
}

For homework I want to draw circles randomly around the screen. If any of the circles overlap, then I want to fill in those circles. I am starting with some code that draws circles on the screen wherever the mouse pointer is clicked. I am really confused about how to use random values to determine the circles and also how to store those values in an array or an arraylist. I think that to fill in the circles I will just use an for statement comparing the distance between centerpoints of circles. Thank you very much for any suggestions. Here is my starting point that I am trying to figure out how to modify:

import java.util.ArrayList;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;

public class DotsPanel extends JPanel
{
   private final int SIZE = 6;  // radius of each dot

   private ArrayList<Point> pointList;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this panel to listen for mouse events.
   //-----------------------------------------------------------------
   public DotsPanel()
   {
      pointList = new ArrayList<Point>();

      addMouseListener (new DotsListener());

      setBackground (Color.black);
      setPreferredSize (new Dimension(300, 200));
   }

   //-----------------------------------------------------------------
   //  Draws all of the dots stored in the list.
   //-----------------------------------------------------------------
   public void paintComponent (Graphics page)
   {
      super.paintComponent(page);

      page.setColor (Color.green);

      for (Point spot : pointList)
         page.fillOval (spot.x-SIZE, spot.y-SIZE, SIZE*2, SIZE*2);

      page.drawString ("Count: " + pointList.size(), 5, 15);
   }

   //*****************************************************************
   //  Represents the listener for mouse events.
   //*****************************************************************
   private class DotsListener implements MouseListener
   {
      //--------------------------------------------------------------
      //  Adds the current point to the list of points and redraws
      //  the panel whenever the mouse button is pressed.
      //--------------------------------------------------------------
      public void mousePressed (MouseEvent event)
      {
         pointList.add(event.getPoint());
         repaint();
      }

      //--------------------------------------------------------------
      //  Provide empty definitions for unused event methods.
      //--------------------------------------------------------------
      public void mouseClicked (MouseEvent event) {}
      public void mouseReleased (MouseEvent event) {}
      public void mouseEntered (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-10-09 08:53:54

您想要使用

Math.random()

Random 作为这个作业

,我不想给你完整的解决方案。但是..

这里有一个提示

将 addMouseListener 替换为循环,以在屏幕上绘制圆圈数。

在循环内部,是获取 X 和 Y 的 2 个值以创建 Point 对象并将其添加到数组中的随机方法之一。

要使用 Random 对象,您的代码将如下所示,

Random random = new Random();
int x = random.nextInt(200);

其中 200 是最大数字,这将是屏幕的大小。

You want to use

Math.random()

or the Random class

As this it homework, I don't want to give you the full solution. But..

Here is a hint.

Replace the addMouseListener with a loop, to draw the number of circles on the screen.

Inside the loop, is one of the random methods to get 2 value for X and Y to create your Point object, and add it to the array.

To use the Random object, your code will look like this

Random random = new Random();
int x = random.nextInt(200);

Where 200 is the maximum number, this would be the size of your screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文