drawing random circles, storing their coorindates in an array

北慕城南 提交于 2019-12-11 04:02:40

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/4197027/drawing-random-circles-storing-their-coorindates-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!