如何缓慢地将对象的颜色从一种改变为另一种?
我正在尝试实现一种方案,其中对象的颜色从一种颜色缓慢变为另一种颜色。
我的初始颜色为targetColor,最终颜色为updateColor。changingSpeed变量设置为5。
我必须使用的机制是
- 使用
getRed()
,getGreen()
,getBlue()
获得了红,绿,蓝三基色 - 通过targetColor–color = [dr dg db]计算目标颜色的差异
- 通过将向量的范数除以[dr dg db] T来规范化[dr dg db] T(请注意div除以零)
- 通过更改Speed乘以以控制更改颜色的速度
- 将颜色更新为color + [dr’dg’db’]
到目前为止,我已经能够进行以下代码:
dr=targetColor.getRed()-updateColor.getRed();
dg=targetColor.getGreen()-updateColor.getGreen();
db=targetColor.getBlue()-updateColor.getBlue();
double nrml= Math.sqrt((dr*dr)+(dg*dg)+(db*db));
dr=dr/nrml;
dg=dg/nrml;
db=db/nrml;
如何执行第四步和第五步?可以请任何人通过代码示例指定如何执行此操作吗?另外,请检查以上代码是否正确。
-
这是一个示例,当您在组件之间移动时,背景会逐渐变暗:
import java.awt.*; import java.awt.event.*; import java.util.Hashtable; import java.util.ArrayList; import javax.swing.*; public class Fader { // background color when component has focus private Color fadeColor; // steps to fade from original background to fade background private int steps; // apply transition colors at this time interval private int interval; // store transition colors from orginal background to fade background private Hashtable backgroundColors = new Hashtable(); /* * Fade from a background color to the specified color using * the default of 10 steps at a 50 millisecond interval. * * @param fadeColor the temporary background color */ public Fader(Color fadeColor) { this(fadeColor, 10, 50); } /* * Fade from a background color to the specified color in the * specified number of steps at the default 5 millisecond interval. * * @param fadeColor the temporary background color * @param steps the number of steps to fade in the color */ public Fader(Color fadeColor, int steps) { this(fadeColor, steps, 50); } /* * Fade from a background color to the specified color in the * specified number of steps at the specified time interval. * * @param fadeColor the temporary background color * @param steps the number of steps to fade in the color * @param intevral the interval to apply color fading */ public Fader(Color fadeColor, int steps, int interval) { this.fadeColor = fadeColor; this.steps = steps; this.interval = interval; } /* * Add a component to this fader. * * The fade color will be applied when the component gains focus. * The background color will be restored when the component loses focus. * * @param component apply fading to this component */ public Fader add(JComponent component) { // Get colors to be used for fading ArrayList colors = getColors( component.getBackground() ); // FaderTimer will apply colors to the component new FaderTimer( colors, component, interval ); return this; } /* ** Get the colors used to fade this background */ private ArrayList getColors(Color background) { // Check if the color ArrayList already exists Object o = backgroundColors.get( background ); if (o != null) { return (ArrayList)o; } // Doesn't exist, create fader colors for this background ArrayList colors = new ArrayList( steps + 1 ); colors.add( background ); int rDelta = ( background.getRed() - fadeColor.getRed() ) / steps; int gDelta = ( background.getGreen() - fadeColor.getGreen() ) / steps; int bDelta = ( background.getBlue() - fadeColor.getBlue() ) / steps; for (int i = 1; i < steps; i++) { int rValue = background.getRed() - (i * rDelta); int gValue = background.getGreen() - (i * gDelta); int bValue = background.getBlue() - (i * bDelta); colors.add( new Color(rValue, gValue, bValue) ); } colors.add( fadeColor ); backgroundColors.put(background, colors); return colors; } class FaderTimer implements FocusListener, ActionListener { private ArrayList colors; private JComponent component; private Timer timer; private int alpha; private int increment; FaderTimer(ArrayList colors, JComponent component, int interval) { this.colors = colors; this.component = component; component.addFocusListener( this ); timer = new Timer(interval, this); } public void focusGained(FocusEvent e) { alpha = 0; increment = 1; timer.start(); } public void focusLost(FocusEvent e) { alpha = steps; increment = -1; timer.start(); } public void actionPerformed(ActionEvent e) { alpha += increment; component.setBackground( (Color)colors.get(alpha) ); if (alpha == steps || alpha == 0) timer.stop(); } } public static void main(String[] args) { // Create test components JComponent textField1 = new JTextField(10); textField1.setBackground( Color.YELLOW ); JComponent textField3 = new JTextField(10); JComponent textField4 = new JTextField(10); JComponent button = new JButton("Start"); JComponent checkBox = new JCheckBox("Check Box"); JFrame frame = new JFrame("Fading Background"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.getContentPane().add(textField1, BorderLayout.NORTH ); frame.getContentPane().add(button, BorderLayout.SOUTH ); frame.getContentPane().add(textField3, BorderLayout.WEST ); frame.getContentPane().add(textField4, BorderLayout.EAST ); frame.getContentPane().add(checkBox); // Gradual Fading (using defaults) // Fader fader = new Fader( new Color(155, 255, 155) ); Fader fader = new Fader( new Color(155, 255, 155), 10, 50 ); fader.add( textField1 ); fader.add( textField3 ); fader.add( checkBox ); // Instant Fading fader = new Fader( new Color(255, 155, 155), 1, 1 ); fader.add( textField4 ); fader.add( button ); frame.pack(); frame.setVisible( true ); } }
它使用计时器以指定的间隔更新背景。然后,它仅根据所需的步数在两种颜色之间进行插值。
-
将值从一种形式传递到另一种形式
2021-02-02 关注 0 浏览533 1答案
-
双速电机定子绕组从一种接法改变为另一种接法时,电源相序( ),以保证电机的旋转方向不变
2022-05-22 关注 0 浏览16 1答案
-
超声波从一种介质进入另一种不同介质而改变传播方向的现象叫做( )。
2022-05-08 关注 0 浏览11 1答案
-
我应该如何将DDL更改从一种环境迁移到另一种环境?
2021-05-10 关注 0 浏览69 1答案
-
在Java中从一种基础转换为另一种基础
2021-01-31 关注 0 浏览92 1答案
-
如何用BufferedImage中的另一种颜色替换颜色
2021-01-30 关注 0 浏览107 1答案
-
如何在詹金斯中将现有工作从一种观点转移到另一种观点?
2021-01-31 关注 0 浏览118 1答案
-
一种药物改变另一种药物的分布、代谢、排泄,属于()
2022-05-15 关注 0 浏览11 1答案
-
( )是由一种队形变为另一种队形的队列动作。
2022-05-08 关注 0 浏览47 1答案
-
从一种控制器方法重定向到另一种控制器方法
2021-02-02 关注 0 浏览116 1答案