java - Creating a random circle that can be clicked on like a button but is a circle -
this question exact duplicate of:
so here predicament cannot seem find anywhere on internet. question simple. creating game app android. game generate random circles on screen user can click on once user click on 1 of these random circles action occur. function of button, different because whole circle able clicked. post random number generator generates circles. not main class separate class extends view. have attempted below in ontouchevent method, errors in logcat have posted below long main class. please feel if close finding solution problem. if tell me doing wrong in class helpful everyone
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(getwidth()-radius/2) + radius/2f); int randomheight = (random.nextint((int) (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) { switch (event.getaction()) { case motionevent.action_down : randomwidth = (int) event.getx(); randomheight = (int) event.gety(); invalidate(); break; } return true; }
tell me if can done or if im going have change code thank here main class if helps other code have here please disregard comments trying learn on own , horribly failed each time
public class main extends activity { drawingview v; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen); linearlayout layout1 = new linearlayout (this); framelayout game = new framelayout(this); drawingview v = new drawingview (this); textview mytext = new textview(this); //int w = getresources().getinteger(drawingview.redcolor); //button redcircle = (button) findviewbyid(w); //redcircle.setwidth(300); //redcircle.settext("start game"); layout1.addview(mytext); // layout1.addview(redcircle); //redcircle.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); //game.addview(mytext); game.addview(v); game.addview(layout1); setcontentview(game); //redcircle.setonclicklistener((onclicklistener) this); } public void onclick(view v) { intent intent = new intent(this, main.class); startactivity(intent); // re-starts activity game-view. add this.finish(); remove stack } @override public boolean oncreateoptionsmenu(menu menu){ getmenuinflater().inflate(r.menu.main, menu); return true; } }
i these errors in log cat
03-31 01:48:52.594: e/androidruntime(2395): fatal exception: main 03-31 01:48:52.594: e/androidruntime(2395): process: com.tripps.thesimplegame, pid: 2395 03-31 01:48:52.594: e/androidruntime(2395): java.lang.runtimeexception: unable start activity componentinfo{com.tripps.thesimplegame/com.tripps.thesimplegame.main}: java.lang.illegalargumentexception: n <= 0: -115 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activitythread.access$800(activitythread.java:144) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activitythread$h.handlemessage(activitythread.java:1278) 03-31 01:48:52.594: e/androidruntime(2395): @ android.os.handler.dispatchmessage(handler.java:102) 03-31 01:48:52.594: e/androidruntime(2395): @ android.os.looper.loop(looper.java:135) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activitythread.main(activitythread.java:5221) 03-31 01:48:52.594: e/androidruntime(2395): @ java.lang.reflect.method.invoke(native method) 03-31 01:48:52.594: e/androidruntime(2395): @ java.lang.reflect.method.invoke(method.java:372) 03-31 01:48:52.594: e/androidruntime(2395): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 03-31 01:48:52.594: e/androidruntime(2395): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 03-31 01:48:52.594: e/androidruntime(2395): caused by: java.lang.illegalargumentexception: n <= 0: -115 03-31 01:48:52.594: e/androidruntime(2395): @ java.util.random.nextint(random.java:182) 03-31 01:48:52.594: e/androidruntime(2395): @ com.tripps.thesimplegame.drawingview.<init>(drawingview.java:37) 03-31 01:48:52.594: e/androidruntime(2395): @ com.tripps.thesimplegame.main.oncreate(main.java:30) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activity.performcreate(activity.java:5933) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 03-31 01:48:52.594: e/androidruntime(2395): @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) 03-31 01:48:52.594: e/androidruntime(2395): ... 10 more
for these lines, using negative number in random.nextint(), have use positive numbers considering using math.abs()
instead of
int randomwidth =(int) (random.nextint(getwidth()-radius/2) + radius/2f); int randomheight = (random.nextint((int) (getheight()-radius/2 + radius/2f)));
use math.abs() not giving random.nextint() negative number
int randomwidth =(int) (random.nextint(math.abs(getwidth()-radius/2)) + radius/2f); int randomheight = (random.nextint((int) math.abs((getheight()-radius/2 + radius/2f))));
Comments
Post a Comment