How to write true color random (java) -
this code:
import java.awt.*; import java.util.random; import javax.swing.* public class graphspanetest { public static void main(string[] args) { myframe window = new myframe(); } }
class myframe extends jframe { public myframe() { mypanel panel = new mypanel(); container cont = getcontentpane(); cont.add(panel); setbounds(100,100,500,500); setvisible(true); } }
class mypanel extends jpanel { public void paintcomponent(graphics gr) { super.paintcomponent(gr); random ab = new random(); colors={color.blue, color.red, color.orange, color.pink}; //here error int colorsthis = ab.nextint(colors[colorthis]); //here error gr.setcolor(color.red); //i try it, does't work //and here error int a=1; while (a<10) { random b = new random(); gr.fillrect(b.nextint(900+1),b.nextint(900+1), b.nextint(50+1), b.nextint(50+1)); a++; } } }
i tried create code has square has random color @ random coordinate random bounds. have error. please help. know english bad.
this generate random colour rather predefined colour in array:
public color randomcolor() { random r = new random(); float red, green, blue; red = r.nextfloat(); green = r.nextfloat(); blue = r.nextfloat(); return new color(red, green, blue); }
Comments
Post a Comment