java - Having problems with the recursion -
my goal program have bullseye colors switch , forth. colors not switch makes new colors instead. more pressing problem when try repeat in way. screen comes when program ran blank , nothing. when there no loop bullseye comes up.
import java.awt.color; import java.awt.graphics; import java.util.random; import javax.swing.jpanel; public class bullseye extends jpanel { public void paintcomponent( graphics g ) { super.paintcomponent( g ); random rand = new random(); int top = 2; int r = rand.nextint(256); int b = rand.nextint(256); int h = rand.nextint(256); int t = rand.nextint(256); int u = rand.nextint(256); int v = rand.nextint(256); color randomcolor = new color(r, h, b); color randcolor = new color(t,u,v); //sets colors first bullseye g.setcolor(randomcolor); g.filloval( 10, 10, 200, 200 ); g.setcolor(randcolor); g.filloval( 35, 35, 150, 150 ); g.setcolor(randomcolor); g.filloval(60, 60, 100, 100); g.setcolor(randcolor); g.filloval( 85, 85, 50, 50 ); try { thread.sleep(1000); // nothing 1000 miliseconds (1 second) } catch(interruptedexception e) { e.printstacktrace(); } //sets colors second bullseye g.setcolor(randcolor); g.filloval( 10, 10, 200, 200 ); g.setcolor(randomcolor); g.filloval( 35, 35, 150, 150 ); g.setcolor(randcolor); g.filloval(60, 60, 100, 100); g.setcolor(randomcolor); g.filloval( 85, 85, 50, 50 ); //recursive call repeat , forth colors paintcomponent(g); } } import javax.swing.jframe; public class bullseyetest { public static void main( string args[] ) { bullseye panel = new bullseye(); jframe application = new jframe(); application.setdefaultcloseoperation( jframe.exit_on_close ); application.add( panel ); application.setsize( 230, 250 ); application.setvisible( true ); } }
there couple issues here contributing problem.
first, generating random numbers colors, , why different colors every time run program (without recursive call).
if want same colors every time, don't need random number generator, constants.
secondly, recursion not right way re-render ui in swing. swing provides 'repaint' method on jcomponent, , typically call repaint method action listener fired timer instead of doing recursively. also, getting non-responsive ui because telling thread sleep.
hope helps answer question. check out post more information on how implement this:
Comments
Post a Comment