绘制随机圆圈,将其coorindates存储在一个数组中 [英] drawing random circles, storing their coorindates in an array

查看:154
本文介绍了绘制随机圆圈,将其coorindates存储在一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了做作业,我想在屏幕上随机绘制圆圈。如果任何一个圆圈重叠,那么我想填写这些圆圈。我开始使用一些代码,在点击鼠标指针的位置在屏幕上绘制圆圈。我对如何使用随机值来确定圆以及如何将这些值存储在数组或数组列表中感到非常困惑。我认为要填充圆圈,我只会使用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; //每个点的半径

private ArrayList< Point> pointList;

// ---------------------------------------- -------------------------
//构造函数:设置此面板以侦听鼠标事件。
// -------------------------------------------- ---------------------
public DotsPanel()
{
pointList = new ArrayList< Point>();

addMouseListener(new DotsListener());

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

// ----------------------------------- ------------------------------
//绘制列表中存储的所有点。
// -------------------------------------------- ---------------------
public void paintComponent(Graphics page)
{
super.paintComponent(page);

page.setColor(Color.green); (Point point:pointList)

page.fillOval(spot.x-SIZE,spot.y-SIZE,SIZE * 2,SIZE * 2);

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

// *********************************** ******************************
//表示鼠标事件的侦听器。
// ******************************************** *********************
private class DotsListener implements MouseListener
{
// --------- -------------------------------------------------- ---
//将当前点添加到点列表中,并重新绘制
//面板,只要按下鼠标按钮。
// -------------------------------------------- ------------------
public void mousePressed(MouseEvent event)
{
pointList.add(event.getPoint());
repaint();
}

// ----------------------------------- ---------------------------
//为未使用的事件方法提供空的定义。
// -------------------------------------------- ------------------
public void mouseClicked(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
}
}


解决方案

您想使用

 Math.random()

随机课程



由于这是作业,我不想给你完整的解决方案。但是..



以下是提示



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



,是为X和Y创建Point对象并将其添加到数组中的随机方法之一。代码看起来像这样

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

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


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

解决方案

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.

这篇关于绘制随机圆圈,将其coorindates存储在一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆