【发布时间】:2017-08-10 02:06:02
【问题描述】:
我正在尝试为 JPanel 设置动画渐变背景。效果有效,但我想在重新开始时获得平滑的过渡。下面是我目前的实现。当 xvalue2 达到限制设置时,我交换颜色并重新开始。
public class GradientAnimation {
static class GradientPanel extends JPanel {
private static final long serialVersionUID = -4185583782901846967L;
private Timer timer;
private float Xend;
private final float MAXVALUE = 800f;
private Color color1 = new Color(128,62,153,255);
private Color color2 = new Color(192,201,200,255);
GradientPanel() {
Xend = 0f;
setOpaque(true);
ActionListener action = new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
if (Xend < MAXVALUE) Xend+=2f;
else{
Color aux = color1;
color1 = color2;
color2 = aux;
Xend = 0f;
}
revalidate();
repaint();
}
};
timer = new Timer(5, action);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
final BufferedImage image = new BufferedImage(
getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
g2d = image.createGraphics();
GradientPaint prim = new GradientPaint(0f, 0f, color1,
Xend, 0f, color2);
g2d.setPaint(prim);
g2d.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image, 0, 0, null);
}
}
private static void createAndShowUI() {
try {
JFrame frame = new JFrame("Gradient Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
GradientPanel imagePanel = new GradientPanel();
frame.add(imagePanel);
frame.setSize(400, 400);
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
再次,我想隐藏我交换颜色以获得完美的渐变动画循环的那一刻。请让我知道代码是否正确以及如何提高质量。
【问题讨论】:
-
请不要发布关于填充的废话,因为这违背了限制的目的,并且对我们这些会帮助你的志愿者不公平。相反,请告诉您问题的详细信息,代码的作用,它应该做什么。
-
是的,我明白了。但是与代码相比,我的解释太短了,我想发布一个工作代码而不是 sn-p 以便可以对其进行测试。对不起。
-
所以补充说明,不废话。帮助我们了解您编写的代码,描述它。同样,您对我们的帮助只会对您有所帮助。
-
改写一下,我这样做是因为我无法发布带有上述解释的问题,因为我认为我提供了足够的详细信息和可以尝试的工作代码。