java - Trying to get the x, y coordinate of each random circle that is drawn on the screen -
hi im making game create random circles on screen. circles created randomly have value red or green. question able not determine when user clicks on 1 of circles determine circle have clicked on (red or green). here code below. main problem trying find x , y of circle drawn
public class drawingview extends view { public drawingview(context context) { super(context); // todo auto-generated constructor stub } rectf rectf = new rectf(0, 0, 200, 0); private static final int w = 100; public static int lastcolor = color.black; private final random random = new random(); private final paint paint = new paint(); private final int radius = 230; private final handler handler = new handler(); public static int redcolor = color.red; public static int greencolor = color.green; int randomwidth =(int) (random.nextint(math.abs(getwidth()-radius/2)) + radius/2f); int randomheight = (random.nextint((int) math.abs((getheight()-radius/2 + radius/2f)))); private final runnable updatecircle = new runnable() { @override public void run() { lastcolor = random.nextint(2) == 1 ? redcolor : greencolor; paint.setcolor(lastcolor); invalidate(); handler.postdelayed(this, 1000); } }; @override protected void onattachedtowindow() { super.onattachedtowindow(); handler.post(updatecircle); } @override protected void ondetachedfromwindow() { super.ondetachedfromwindow(); handler.removecallbacks(updatecircle); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); // other stuff here canvas.drawcircle(randomwidth, randomheight + radius/2f, radius, paint); } @override public boolean ontouchevent (motionevent event) { double r = math.sqrt(((randomwidth^2)+(randomheight^2))); int maxx = (int) (((randomwidth)*(randomwidth)) + r); int minx = (int) ((((randomwidth)*(randomwidth))) - r); int maxy = (int) (((randomheight)*(randomheight)) + r); int miny = (int) ((((randomheight)*(randomheight))) - r); public int xcoordinateofredcolor(){ if(redcolor == lastcolor){ if(randomwidth > maxx && < minx){ event.getx(); } }; } public int ycoordinateofredcolor(){ if(redcolor == lastcolor){ if(randomheight > maxy && < miny){ event.getx(); } }; } if(redcolor == lastcolor){ if(randomheight > maxy && < miny){ event.gety(); } }; if(greencolor == lastcolor){ }; if(greencolor == lastcolor){ }; switch (event.getaction()) { case motionevent.action_down : randomwidth = (int) event.getx(); randomheight = (int) event.gety(); invalidate(); break; case motionevent.action_pointer_up : break; } return true; } }
i dont know if im close or far away.
there many ways solve kind of problem. can drive nuts trying list them all. 1 of powerful assign each shape unique color when created. use color find shape. in other words...
when click on this:
sample color this:
keep hashmap these unique colors shapes object reference. have shapes paint unique color on hidden bitmap never put on screen. when user clicks, color @ x y on hidden bitmap. run color thru hashmap , have reference shape clicked on. don't need worry overlaps or using different shapes circles. gives not x , y of shape know shape.
of course work have make own class shapes. made unique colors distinct illustration purposes. in reality have int increment. either incremented constructor or better yet have factory method inject it.
this powerful enough works when rendering 3d. keep visible , hidden bitmaps in sync. turns out easy since reuse same rendering code. loop each shape , ask paint itself, it's unique color, on bitmap hand it. works long there no additional effects change color (shading, lighting, ray tracing). make sure turn off.
by way, don't think this:
if(randomheight > maxy && < miny){
does think does. when want test between 2 values use:
if(miny <= randomheight && randomheight <= maxy){
because reminds me of mathematical inequalities 3 ≤ x ≤ 15.
Comments
Post a Comment