我需要一個用Java編寫的顏色漸變示例。 我的要求是我有兩個矩形。一個充滿了紅色,另一個充滿了白色。當我點擊任何按鈕時,我想讓那個紅色開始褪色並移動綠色。一旦達到綠色位置,則另一個矩形自動從黃色藍色開始。有人能幫我解決嗎?如果有任何使用Javaswing或SWT編寫的例子會很好。謝謝。褪色樣本
褪色樣本
回答
結帳Trident動畫庫。它允許您輕鬆地在班級中插入屬性。另請查看來自Kirill Grouchnikov(Trident的作者)的原創教程these。
編輯:動畫例如W/O三叉戟
下面的示例是基於AnimatedGraphics例如在Filthy Rich Clients:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class AnimatedGraphics extends JComponent implements ActionListener {
Color startColor = Color.RED; // where we start
Color endColor = Color.GREEN; // where we end
Color currentColor = startColor;
int animationDuration = 2000; // each animation will take 2 seconds
long animStartTime; // start time for each animation
public AnimatedGraphics() {
Timer timer = new Timer(30, this);
// initial delay while window gets set up
timer.setInitialDelay(1000);
animStartTime = 1000 + System.nanoTime()/1000000;
timer.start();
}
public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(currentColor);
g.fillOval(0, 0, getWidth(), getHeight());
}
public void actionPerformed(ActionEvent ae) {
// calculate elapsed fraction of animation
long currentTime = System.nanoTime()/1000000;
long totalTime = currentTime - animStartTime;
if (totalTime > animationDuration) {
animStartTime = currentTime;
}
float fraction = (float)totalTime/animationDuration;
fraction = Math.min(1.0f, fraction);
// interpolate between start and end colors with current fraction
int red = (int)(fraction * endColor.getRed() +
(1 - fraction) * startColor.getRed());
int green = (int)(fraction * endColor.getGreen() +
(1 - fraction) * startColor.getGreen());
int blue = (int)(fraction * endColor.getBlue() +
(1 - fraction) * startColor.getBlue());
// set our new color appropriately
currentColor = new Color(red, green, blue);
// force a repaint to display our oval with its new color
repaint();
}
private static void createAndShowGUI() {
JFrame f = new JFrame("Animated Graphics");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200, 200);
f.add(new AnimatedGraphics());
f.setVisible(true);
}
public static void main(String args[]) {
Runnable doCreateAndShowGUI = new Runnable() {
public void run() {
createAndShowGUI();
}
};
SwingUtilities.invokeLater(doCreateAndShowGUI);
}
}
好的。謝謝。但我不知道我們是否可以將這個庫添加到我們的項目中。 Anywyas謝謝。 – user414967
@ user414967我包含了一個動畫顏色轉換不帶三叉戟的示例。 – tenorsax
非常感謝爵士!!!非常有幫助! – user414967
- 1. 基本褪色幻燈片
- 2. IE 8和文本褪色
- 3. 褪色和褪色標籤不定式用單獨的`文本'
- 4. 黑色背景上的褪色文本
- 5. ObjectAnimator不褪色
- 6. 與jquery褪色?
- 7. Fancybox 3.0褪色
- 8. 與jquery褪色
- 9. 褪色背景
- 10. 如何褪色
- 11. 褪色的ghostscript
- 12. 計算褪色
- 13. jQuery:褪色
- 14. 邊境褪色
- 15. 圖像褪色
- 16. ArcMap顏色顯得褪色
- 17. Fullpage.js與背景色褪色
- 18. Python - RGB LED顏色褪色
- 19. 背景顏色不褪色
- 20. Javascript顏色褪色延遲
- 21. 文本出現並褪色時移動
- 22. 褪色Java幻燈片腳本
- 23. PDF ZOOMING正在褪色的文本
- 24. java腳本圖像變化和褪色
- 25. javascript/jquery:尋找圖像褪色腳本
- 26. 褪色文本背景的CSS
- 27. 要褪色的隨機文本
- 28. 即在文本中褪色的問題
- 29. Flexslider IE8標題文本不褪色
- 30. Cycle2褪色標題
我曾嘗試一個褪色不satisy我的要求。它通過使用alpha將紅色減少爲白色。 – user414967