Use different colors in same graphics (Java) -
i'm relatively new java.
i need exercise draws different random shapes, different positions , colors. want every shape have different color. part of code (which has 2 different shapes, oval , rectangle). 2 shapes have same random color:
protected void paintcomponent(graphics g) { int r = (int)(math.random()*256); int g = (int)(math.random()*256); int b = (int)(math.random()*256); color color = new color(r, g, b); // picks random color int ovala = (int)(math.random()*400); int ovalb = (int)(math.random()*300); int ovalc = (int)(math.random()*(400 - ovala)); int ovald = (int)(math.random()*(300 - ovalb)); int recta = (int)(math.random()*300); int rectb = (int)(math.random()*400); int rectc = (int)(math.random()*(400 - recta)); int rectd = (int)(math.random()*(300 - rectb)); super.paintcomponent(g); g.setcolor(color); g.filloval(ovala, ovalb, ovalc, ovald); g.fillrect(recta, rectb, rectc, rectd); }
protected void paintcomponent(graphics g) { int r = (int)(math.random()*256); int g = (int)(math.random()*256); int b = (int)(math.random()*256); color color = new color(r, g, b); // picks random color g.setcolor(color); int ovala = (int)(math.random()*400); int ovalb = (int)(math.random()*300); int ovalc = (int)(math.random()*(400 - ovala)); int ovald = (int)(math.random()*(300 - ovalb)); g.filloval(ovala, ovalb, ovalc, ovald); r = (int)(math.random()*256); g = (int)(math.random()*256); b = (int)(math.random()*256); color = new color(r, g, b); // picks random color int recta = (int)(math.random()*300); int rectb = (int)(math.random()*400); int rectc = (int)(math.random()*(400 - recta)); int rectd = (int)(math.random()*(300 - rectb)); super.paintcomponent(g); g.setcolor(color); g.fillrect(recta, rectb, rectc, rectd); }
now, here's explanantion behind it, when had code way did it, did not new random set of numbers color, ints used stayed same when did inquire color. used math.random alter values 1 time again before making color out of them
java
No comments:
Post a Comment