Swing 中的渐变颜色?
所以我添加了 8 个 TextFields 并想设置它们的背景颜色。我的想法是将第一个设置为红色 (255, 0, 0),最后一个设置为蓝色 (0, 0, 255),然后将 8 个(或实际上任何数字)设置为它们之间的其他渐变。我试图找出如何解决这个问题,“如果‘下一个’变量是 0,则增加该变量的数量与前一个变量的减少量相同”,
所以它在每次迭代中看起来可能是这样的:
setBackground(255, 0, 0);
setBackground(191, 63, 0);
setBackground(127, 127, 0);
...
setBackground(0, 0, 255);
现在我想尝试将这种增加和减少的方式放入 for 循环中,该循环将迭代 n 次,其中 n 是文本字段的数量(为了简单起见,现在为 8)。有人知道是否有一个聪明的解决方案吗?
再次评论:
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Apple{
public Apple(int width, int height) {
SwingUtilities.invokeLater(() -> initGUITest(width, height));
}
public void initGUITest(int width, int height) {
JFrame frame = new JFrame();
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
JPanel panel = new JPanel();
GridLayout gl = new GridLayout(10, 1);
panel.setLayout(gl);
frame.add(panel);
for(int i = 0; i < 8; i++) {
JTextField jtf = new JTextField("Track " + (i + 1));
jtf.setBackground(new Color(255, 0, 0)); //Start color
panel.add(jtf);
}
}
public static void main(String args[]) {
Apple a = new Apple(300, 300);
}
}
So I add 8 TextFields and wanna set their background colors. My idea is to set the first one to red (255, 0, 0) the last one to blue (0, 0, 255) and the 8 (or any number actually) others gradient between these. I'm trying to figure out how to solve it in terms of "If the 'next' variable is 0 increase this variable with same amount as previous variable is decreasing with"
So it could look like in each iteration:
setBackground(255, 0, 0);
setBackground(191, 63, 0);
setBackground(127, 127, 0);
...
setBackground(0, 0, 255);
Now I wanna try and fit this way of increase and decreasing into a for loop that will iterate n times where n is number of TextFields (now 8 for simplicity). Anyone know if there's a clever solution to this?
MRE:
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Apple{
public Apple(int width, int height) {
SwingUtilities.invokeLater(() -> initGUITest(width, height));
}
public void initGUITest(int width, int height) {
JFrame frame = new JFrame();
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
JPanel panel = new JPanel();
GridLayout gl = new GridLayout(10, 1);
panel.setLayout(gl);
frame.add(panel);
for(int i = 0; i < 8; i++) {
JTextField jtf = new JTextField("Track " + (i + 1));
jtf.setBackground(new Color(255, 0, 0)); //Start color
panel.add(jtf);
}
}
public static void main(String args[]) {
Apple a = new Apple(300, 300);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查值是否为零并从那里递增或递减值的效率很低。
有两种方法可以实现此
线性,
您计算一个混合值,即
(i/stepSize)
,并使用它在开始值和结束值之间进行线性插值,如下转换混合到浮动对于插值在这里工作是必要的,这是逻辑
输出:
关键帧
一个更复杂的示例涉及使用动态更改的关键帧基于您的 i 值的起点和终点
以下是关键帧,
这意味着对于
复选框 0->2 在 RED 和 RED 之间进行插值绿色
复选框 3->4 在绿色和绿色之间插入。蓝色
复选框 5->6 在蓝色和蓝色之间插入。黄色
复选框 7->8 在黄色和黄色之间插入。 MAGENTA
逻辑在于这段代码
这是完整的代码
输出:
Checking if an value is zero and incrementing or decrementing a value from there is inefficient.
There are 2 ways to go about this
Linear
you calculate an blend value which is
(i/stepSize)
and use that to linearly interpolate between the start and end value as followsconversion of blend to float is necessary for interpolation to work here is logic
Output :
KeyFrames
An more complicated example involves using key frames where you dynamically change the start and end points based on your i value
Here are the keyframes
what this means is that for
checkboxes 0->2 interpolate between RED & GREEN
checkboxes 3->4 interpolate between GREEN & BLUE
checkboxes 5->6 interpolate between BLUE & YELLOW
checkboxes 7->8 interpolate between YELLOW & MAGENTA
The logic lies in this code
Here is the full code
Output :
因此,您可以通过多种方式执行此操作,但对我个人而言,我希望使用某种“混合”算法,该算法允许您建立所需的颜色“范围”,然后基于某个值(即索引或百分比),生成这些颜色的混合颜色(在范围内)。
例如...
好的,这不是问题。只需添加另一个“停止”和该停止的颜色,例如...
将产生...
< img src="https://i.sstatic.net/rOckI.png" alt="在此处输入图像描述">
想要添加更多字段吗?不用担心,只需将
Color color = Blender.blishedColorAt(index / 7f);
更改为7f
即可成为预期字段的数量 - 1(记住,我们正在开始索引
位于0
So, any number of ways you might do this, but for me, personally, I'd look towards using some kind of "blending" algorithm which would allow you to establish the "range" of colors you want and then based on some value (ie a index or percentage), generate a color which is blend of those colors (within the range).
For example...
Okay, not an issue. Simply add another "stop" and the color for that stop, for example...
will produce...
Want to add more fields? No worries, just change
Color color = blender.blendedColorAt(index / 7f);
to so that7f
becomes the number of expected fields - 1 (remember, we're starting theindex
at0
????)这是一个可以生成一系列颜色的类,这些颜色可以按照给定的步骤数从一种颜色过渡到另一种颜色。
简单的用法是:
如果您需要多次转换,您可以这样做:
然后从蓝色转换为绿色。
生成所有过渡颜色后,您可以单独访问它们:
Here is a class that can generate a series of colors that can transition from one color to another for a given number of steps.
Simple usage would be:
If you need multiple transitions you could do:
which would then transition from BLUE to GREEN.
Once all the transition colors are generated you can access them separately: