我目前正在尝试制作一条跟随鼠标的彩虹轨迹。我使用 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
这可能会使这更清楚一点吗?