java - Generate five random Colors without making 15 randomized variables -
i'm creating beginner java programme creates 5 panels of random color , random size (each panel smaller next).
the problem:
while programme works, i've had type new variable everything. 5 panels 3 colors each (r,g,b) i've had create 15 variables. isn't there way phone call upon random within (r,g,b) rather creating many variables?
here excerpt of code deals how color randomized in each panel:
//random color maker 1 random rand = new random(); int = rand.nextint(255); int b = rand.nextint(255); int c = rand.nextint(255); int d = rand.nextint(255); int e = rand.nextint(255); int f = rand.nextint(255); int g = rand.nextint(255); int h = rand.nextint(255); int = rand.nextint(255); int j = rand.nextint(255); int k = rand.nextint(255); int l = rand.nextint(255); int m = rand.nextint(255); int n = rand.nextint(255); int o = rand.nextint(255); color color1 = new color(a, b, c); color color2 = new color(d, e, f); color color3 = new color(g, h, i); color color4 = new color(j, k, l); color color5 = new color(m, n, o); //panel 1 jpanel panel1 = new jpanel (); panel1.setbackground (color1); panel1.setpreferredsize (new dimension (rand1)); jlabel label1 = new jlabel ("1"); panel1.add(label1); //panel 2 jpanel panel2 = new jpanel (); panel2.setbackground (color2); panel2.setpreferredsize (new dimension (rand2)); jlabel label2 = new jlabel ("2"); panel2.add(label2); panel2.add(panel1);
you can introduce method
random rand = new random(); private color randomcolor() { homecoming new color(rand.nextint(255), rand.nextint(255), rand.nextint(255)); } public void yourmethod() { //panel 1 jpanel panel1 = new jpanel (); panel1.setbackground (randomcolor()); // ... }
java random colors
No comments:
Post a Comment