- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对java相当陌生,所以我认为我对此不太接近,但我似乎可以找到任何其他帮助。基本上,我正在尝试对 jPanel 的背景颜色进行动画处理,以便它的色调(我正在使用 hsb 颜色模型)发生变化。有点像这样:https://kahoot.it/#/注意颜色是如何从一种颜色 float 到另一种颜色的。这是我到目前为止的代码:
public void animate(){
for(float i=.001f;i<1f;i+=.001f){
jPanel1.setBackground(Color.getHSBColor(i, .53f, .97f));
try{
Thread.sleep(5L);
}catch(InterruptedException ex){
}
System.out.println(i);
}
}
现在我知道这可能是不对的,但是循环工作正常,唯一的问题是 jPanel 在循环完成之前不会“更新”。抱歉,我对此类事情是个大菜鸟,感谢您的回复
最佳答案
问题是您阻塞了事件调度线程,因此无法进行绘制。使用 Swing Timer而不是 sleep 。更改 HSB 颜色的运行示例:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ColorCycle {
private static class ColorPanel extends JPanel {
private final float stepSize;
private final Timer timer;
private int index;
ColorPanel(final int steps, int fps) {
stepSize = 1f / steps;
timer = new Timer(1000 / fps, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
index++;
if (index > steps) {
index = 0;
}
repaint();
}
});
}
void start() {
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.getHSBColor(index * stepSize, 1f, 1f));
g.fillRect(0, 0, getWidth(), getHeight());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Colors");
ColorPanel panel = new ColorPanel(300, 20);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
panel.start();
}
});
}
}
关于java - 使用 hsb 模型 Java 制作背景颜色动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274259/
我正在尝试均衡彩色图像。我被告知将其转换为 HSB,然后在将其转换回 RGB 之前对亮度 channel 执行均衡。 到目前为止,我已经计算了频率,但比例因子存在问题。据我了解,颜色的比例因子通常是图
在我的应用程序中,我使用快速非托管代码从多个图像中读取 RGB 像素值,然后将它们转换为 HSB 颜色。现在我想使用以下分区构建 HSB 直方图: 色调:18 个分区,从 0...360 到 20 的
我正在尝试更改笔触的不透明度,但不确定如何在 HSB 颜色模式下进行。或者,如果我切换到 RGB 模式,如何让颜色随时间变化? 这是我的代码:(任何帮助将不胜感激) float rainbow=0;
我希望通过 HSV/HSB 值对很长的颜色列表进行排序。我想按色调对它们进行排序,然后是周六,然后是明亮。我真正需要的只是一种方法来根据 HSV 的顺序判断一种颜色是“之前”还是“之后”,因为我只是要
我正在尝试控制一些 RGB LED 并从红色渐变为紫色。我正在使用 HSV 到 RGB 的转换,这样我就可以从色调 0 扫描到色调 300(超出它会移回红色)。但我注意到的问题是,它似乎在光谱的青色和
我在使用 Xcode 的集成颜色选择器(版本 6.3)时遇到了一个奇怪的问题: 在我为任何项目(在我的情况下为 UIButton)分配颜色,然后尝试输入新的色调值后,只需单击文本字段,颜色就会改变 .
我只想使用色调光谱的一部分(仅从黄绿色到红色)来指示由计算的浮点值指定的状态变化。 我发现使用整个频谱很容易: float hue; hue = (1.0 * [c floatValue]); flo
为了一个关于 UIImage 着色的项目,我折腾了几天。这个想法是,该应用程序将启动一组图像,我必须使用从网络服务检索到的值对其进行着色。如果您愿意,可以选择某种主题。 与我合作的设计师给了我一张关于
如何隐藏 JColorChooser 中除 HSB 之外的所有默认面板? 是否可以在没有 JTabbedPane 的情况下仅显示 HSB,仅显示普通面板 谢谢! 最佳答案 import javax.s
我正在为一项学校作业在 Processing 中制作音乐播放器。飞利浦Hue灯会做出一些相应的视觉效果。我想让每首歌的视觉效果都独一无二。因此,我获取了播放轨道的封面艺术(使用 LastFM API)
我在谷歌上搜索了很长时间,但找不到一种方法来做到这一点。有人有主意吗? Mac 上有一种 NSColor 方法可以实现这一点,但我看不到 iPhone 上的任何方法。我的应用程序的想法是,用户输入十六
我正在开发一个简单的图像程序,用户可以在其中更改图像的 HSB 值。但是,当我更改图像的 HSB 值并转换回 RGB 时,它似乎会丢失其透明度或 alpha 值(透明度所在的位置会变黑)。以下是我的内
我对java相当陌生,所以我认为我对此不太接近,但我似乎可以找到任何其他帮助。基本上,我正在尝试对 jPanel 的背景颜色进行动画处理,以便它的色调(我正在使用 hsb 颜色模型)发生变化。有点像这
我想在 WPF/C# 中创建一个类似于我在 Photoshop 中看到的颜色选择器。 当我移动 Hue Slider 时,渐变应该更新。我第一个想知道如何创建渐变,各个颜色停止应该出现在哪里以及它们应
如何将 HSB 颜色转换为 HSL? Photoshop 在其颜色选择器中显示 HSB 颜色。 HSL 颜色可以在 CSS 中使用。 我试过这个 JS: function hsb2hsl(h, s,
我读了文章Algorithm to Switch Between RGB and HSB Color Values Type RGBColor Red As Byte Green
我正在尝试将 HSB 颜色转换为 RGB。我这样做的方式是 System.Windows.Media.Color winColor = value; System.Drawing.Color draw
我需要准确地将 HSB 转换为 RGB,但我不确定如何解决将小数转换为整数而不四舍五入的问题。这是我在颜色选择器库中的当前功能: HSBToRGB = function (hsb) { var
我正在制作一个 Color 类,作为 C++ 中非常基本的图形 API 的一部分。所以我决定看看微软的 .NET 框架,发现他们的 Color 类有 HSB 的功能。 然后我开始了一项研究,以确定我是
我正在为家庭自动化系统中的灯光管理构建一个界面。我成功地控制了各个供应商的标准开/关和可调光灯,没有什么问题,但现在我遇到了与 RGB 灯相关的问题。 我目前使用的灯是 RGBW LED 灯带 - 具
我是一名优秀的程序员,十分优秀!