0
我試圖得到一個橢圓形每次大小達到50或100的時間來改變漸變的顏色:如何在JFrame不斷重新繪製時設置形狀的漸變?
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是直徑大小,來了窗口的大小)
我爲我想用於圓的漸變設置了2個隨機顏色,但是我希望只有在圓的大小達到100或50(它不斷增長並縮小到這些尺寸)時它纔會更改,但由於我重新繪製了所有內容每次運行時都是白色的,除非它的大小恰好是50或100,否則你永遠不會看到它。我怎樣才能使它始終保持那種顏色,直到它必須改變?
幫助呢? http://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/ex/ColorGradient.java – ControlAltDel