4

我目前正在尝试制作一条跟随鼠标的彩虹轨迹。我使用 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

这可能会使这更清楚一点吗?

4

1 回答 1

3

为了获得您想要的效果,您可能必须创建一个自定义渐变绘制,它跨越一个轴的色域另一个轴的alpha透明度范围。作为一个相关示例,它KineticModel使用 a创建实例RadialGradientPaint数组。GradientImage在每个图像中,alpha 从0xff中心的 (1.0) 到0x3f外围的 (0.25) 呈放射状变化。

附录:根据您的图片,只需将图形上下文设置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));
}

You'll have to decide when to change colors based on space or time. For the latter, javax.swing.Timer is a good choice.

于 2011-09-15T03:12:23.090 回答