java - drawing random circles, storing their coorindates in an array -
for homework want draw circles randomly around screen. if of circles overlap, want fill in circles. starting code draws circles on screen wherever mouse pointer clicked. confused how use random values determine circles , how store values in array or arraylist. think fill in circles use statement comparing distance between centerpoints of circles. thank suggestions. here starting point trying figure out how 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 panel listen mouse events. //----------------------------------------------------------------- public dotspanel() { pointlist = new arraylist<point>(); addmouselistener (new dotslistener()); setbackground (color.black); setpreferredsize (new dimension(300, 200)); } //----------------------------------------------------------------- // draws of dots stored in list. //----------------------------------------------------------------- public void paintcomponent (graphics page) { super.paintcomponent(page); page.setcolor (color.green); (point spot : pointlist) page.filloval (spot.x-size, spot.y-size, size*2, size*2); page.drawstring ("count: " + pointlist.size(), 5, 15); } //***************************************************************** // represents listener mouse events. //***************************************************************** private class dotslistener implements mouselistener { //-------------------------------------------------------------- // adds current point list of points , redraws // panel whenever mouse button pressed. //-------------------------------------------------------------- public void mousepressed (mouseevent event) { pointlist.add(event.getpoint()); repaint(); } //-------------------------------------------------------------- // provide empty definitions 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 use
math.random()
or random class
as homework, don't want give full solution. but..
here hint.
replace addmouselistener loop, draw number of circles on screen.
inside loop, 1 of random methods 2 value x , y create point object, , add array.
to use random object, code this
random random = new random(); int x = random.nextint(200);
where 200 maximum number, size of screen.
Comments
Post a Comment