我正在尝试制作跟随鼠标的彩虹踪迹。我使用Linkedlist绘制鼠标的点,以便跟踪。小径本身看起来很完美,只有小路上看起来不正确的颜色。我希望他们互相褪色。有人告诉我使用线性插值,在查看它一段时间后,似乎它可以工作,我只是不知道如何实现它。
这是我到目前为止的代码:
import impsoft.bots.ColorBot;
import impsoft.scripting.ibot.interfaces.AutoPaint;
import impsoft.scripting.types.ColorScript;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Deque;
import java.util.LinkedList;
import impsoft.scripting.ibot.structs.XY;
import impsoft.scripting.types.ColorSkeltonScriptable;
import impsoft.scripting.types.parallel.scriptjobs.ScriptJob;
public class MouseTrail extends ColorScript implements AutoPaint {
public MouseTrail(ColorBot c) {
super(c);
}
public void script() throws InterruptedException {
while(true) {
mt.setSize(500);
mt.exec();
sleep(100);
}
}
public static String name = "Mouse trail test";
public static String author = "Llaver";
public static String description = "test for mouse trail";
public static double version = 1.00;
public class MouseTrail2 extends ScriptJob implements AutoPaint {
private int size;
private final ColorSkeltonScriptable cs;
private final Deque<XY> trail = new LinkedList<XY>();
private final Color[] rainbow = new Color[]{
Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.magenta
};
public MouseTrail2(ColorSkeltonScriptable cs) {
super(cs);
this.cs = cs;
}
public void setSize(int s) {
size = s;
s = 200;
}
public void runV() {
try {
while (true) {
synchronized (trail) {
if (trail.size() >= size) {
trail.pop();
}
trail.offer(cs.getCurrentMouseXY());
}
sleep(1);
}
} catch (InterruptedException e) {
}
}
@Override
public void paint(Graphics g) {
final Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
synchronized (trail) {
float perc;
int idx;
for(int i = 1 ; i < trail.size() - 1 ; i++){
XY current = ((LinkedList<XY>) trail).get(i);
XY next = ((LinkedList<XY>) trail).get(i - 1);
perc = ((float)i / trail.size()) * 100f;
idx = Math.round((perc * (float)rainbow.length) / 100f);
if(idx >= rainbow.length)idx -= 1;
g2d.setColor(rainbow[idx]);
g.drawLine(current.x, current.y, next.x, next.y);
}
}
}
}
@Override
public void paint(Graphics arg0) {
}
private MouseTrail2 mt = new MouseTrail2(this);
}
一些照片:
这就是我现在所拥有的:
http://img11.imageshack.us/img11/3031/mousetrailhavenow.png
这就是我想要的:
http://img594.imageshack.us/img594/7381/mousetrailtryingtoget.png
这可能会让这一点更清楚吗?
答案 0 :(得分:3)
为了获得你想要的效果,你可能需要创建一个自定义渐变颜色,它沿着一个轴跨越hue的色域,并且沿着另一个轴创建alpha透明度的范围。作为相关示例,此KineticModel
使用RadialGradientPaint
来创建GradientImage
个实例的数组。在每张图像中,alpha从中心的0xff
(1.0)到外围的0x3f
(0.25)径向变化。
附录:根据您的picture,只需将图形上下文的Stroke
设置为合适的宽度,将颜色设置表中的颜色设置为下一个色调(clut
),然后drawLine()
。您可以改变色调,保持饱和度和亮度不变。
float N = 360;
Queue<Color> clut = new LinkedList<Color>();
for (int i = 0; i < N; i++) {
clut.add(Color.getHSBColor(i / N, 1, 1));
}
您必须根据空间或时间决定何时更改颜色。对于后者,javax.swing.Timer
是一个不错的选择。