java - How to set the gradient of a shape when the JFrame is constantly being repainted? -


i'm trying oval change gradient's colours every time reaches size of 50 or 100:

    class mydrawpanel extends jpanel {     public void paintcomponent(graphics g) {          graphics2d g2d = (graphics2d) g;          g2d.setcolor(color.white);         g2d.fillrect(0, 0, 300, 300);          if(dmt == 100 || dmt == 50) {             int red = (int) (math.random() * 256);             int blue = (int) (math.random() * 256);             int green = (int) (math.random() * 256);             color startcolour = new color(red, green, blue);              red = (int) (math.random() * 256);             blue = (int) (math.random() * 256);             green = (int) (math.random() * 256);             color endcolour = new color(red, green, blue);              gradientpaint gradient = new gradientpaint(300, 100, startcolour, 150, 150, endcolour);             g2d.setpaint(gradient);         }         g2d.filloval((size-dmt)/2, (size-dmt)/2 - dmt/2, dmt, dmt);     } } 

(dmt diameter, size size of window comes up)

i set 2 random colours gradients want use circle, want change when circle reaches either size 100 or 50 (it growing , shrinking these sizes), since repaint white every time runs, can never see except when size 50 or 100. how make colour, until has change?

how make colour, until has change?

somewhere must have method changes "dmt" variable. method should responsible changing properties of class. in addition dmt variable should have startcolor , endcolor variables.

then code should like:

public void setdmt(...); {     if (dmt == 50 || dmt == 100)     {         startcolor = ???         endcolor = ???     } } 

when create class need set startcolor/endcolor default value.

then in paintcomponent() method use these 2 varaibles:

    graphics2d g2d = (graphics2d) g;      g2d.setcolor(color.white);     g2d.fillrect(0, 0, 300, 300);      gradientpaint gradient = new gradientpaint(300, 100, startcolour, 150, 150, endcolour);     g2d.setpaint(gradient);      g2d.filloval((size-dmt)/2, (size-dmt)/2 - dmt/2, dmt, dmt); 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -