java - Trying to get the x, y coordinate of each random circle that is drawn in android -


this question has answer here:

i making game create random circles on screen. circles created randomly have value red or green.

my question able not determine when user clicks on 1 of circles determine circle have clicked on (red or green). main problem trying find x , y of circle drawn.

here code:

    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;       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         // code below works when here           float randomwidth =(int) (random.nextint(math.abs(getwidth()-radius/2)) + radius/2f);         float randomheight = (random.nextint((int)math.abs((getheight()-radius/2 + radius/2f))));         canvas.drawcircle(randomwidth, randomheight + radius/2f, radius, paint);     }      @override     public boolean ontouch(view v, motionevent event) {    int x = event.getx();    int y = event.gety();    if(isinsidecircle(x, y)){       //do things here    }    return true; }  private boolean isinsidecircle(int x, int y){   if (((x - randomwidth)^2 + (y - randomheight)^2) < radius^2)     return true;   return false;     }    } 

code below not work

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;     //its right below here code compiles perfectly, gets errors in logcat     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);     }      public boolean ontouch(view v, motionevent event) {    int x = (int) event.getx();    int y = (int) event.gety();    if(isinsidecircle(x, y)){       //do things here    }    return true; }  private boolean isinsidecircle(int x, int y){   if (((x - randomwidth)^2 + (y - randomheight)^2) < radius^2)     return true;   return false;     }    } 

here logcat

04-01 03:41:48.856: e/androidruntime(4010): fatal exception: main 04-01 03:41:48.856: e/androidruntime(4010): process: com.tripps.thesimplegame, pid: 4010 04-01 03:41:48.856: e/androidruntime(4010): java.lang.runtimeexception: unable start activity componentinfo{com.tripps.thesimplegame/com.tripps.thesimplegame.main}: java.lang.illegalargumentexception: n <= 0: 0 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activitythread.access$800(activitythread.java:144) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activitythread$h.handlemessage(activitythread.java:1278) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.os.handler.dispatchmessage(handler.java:102) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.os.looper.loop(looper.java:135) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activitythread.main(activitythread.java:5221) 04-01 03:41:48.856: e/androidruntime(4010):     @ java.lang.reflect.method.invoke(native method) 04-01 03:41:48.856: e/androidruntime(4010):     @ java.lang.reflect.method.invoke(method.java:372) 04-01 03:41:48.856: e/androidruntime(4010):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 04-01 03:41:48.856: e/androidruntime(4010):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 04-01 03:41:48.856: e/androidruntime(4010): caused by: java.lang.illegalargumentexception: n <= 0: 0 04-01 03:41:48.856: e/androidruntime(4010):     @ java.util.random.nextint(random.java:182) 04-01 03:41:48.856: e/androidruntime(4010):     @ com.tripps.thesimplegame.drawingview.<init>(drawingview.java:38) 04-01 03:41:48.856: e/androidruntime(4010):     @ com.tripps.thesimplegame.main.oncreate(main.java:30) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activity.performcreate(activity.java:5933) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 04-01 03:41:48.856: e/androidruntime(4010):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) 04-01 03:41:48.856: e/androidruntime(4010):     ... 10 more 

what calling randomwidth , randomheight x , y cordinates of circle.
methods sign:

canvas.drawcircle(x, y, radius, paint); 

to check if user clicked inside circle can this:

public boolean ontouch(view v, motionevent event) {    int x = (int)event.getx();    int y = (int)event.gety();    if(isinsidecircle(x, y)){       //do things here    }    return true; }  private boolean isinsidecircle(int x, int y){   if (((x - center_x)^2 + (y - center_y)^2) < radius^2)     return true;   return false;     } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -