Java - 制作渐变画笔

时间:2015-11-05 08:27:51

标签: java

我做了一个java绘图应用程序,我做了一个彩虹刷功能;但是,我想将随机颜色变成平滑的渐变。目前只是打印不同颜色的椭圆形,您可以注意到每个不同的椭圆形。有没有办法让它成为一个渐变?

Paint Project - CLICK HERE TO SEE PROGRAM

我的彩虹功能:

public void rainbow() {
    Random generator = new Random();
    int r = generator.nextInt(256);
    int g = generator.nextInt(256);
    int b = generator.nextInt(256);
    Color color = new Color(r, g, b);
    g2.setPaint(color);
}

我的鼠标听众:

public DrawArea() {

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            // save coord x,y when mouse is pressed
            oldX = e.getX();
            oldY = e.getY();
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            // coord x,y when drag mouse
            currentX = e.getX();
            currentY = e.getY();

            if (g2 != null) {
            // draw oval if g2 context not null
            g2.drawOval(oldX, oldY, 40, 40);
            g2.fillOval(oldX, oldY, 40, 40);

            // refresh draw area to repaint
            repaint();

            // store current coords x,y as olds x,y
            oldX = currentX;
            oldY = currentY;
            }
        }
    });
}

Paint Component:

public void paintComponent(Graphics g) {
    if (image == null) {
        image = createImage(getSize().width, getSize().height);
        g2 = (Graphics2D) image.getGraphics();
        clear();
    }
    // enable antialiasing
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,      RenderingHints.VALUE_ANTIALIAS_ON);
    g.drawImage(image, 0, 0, null); 
}

1 个答案:

答案 0 :(得分:0)

我认为你在每个嘀嗒声中调用你的彩虹函数,这就是为什么你会得到这样的效果。

为了产生具有渐变的错觉,r,g,b值必须变得足够慢以具有期望的效果。

您可以这样做的一种方法是将您想要的值存储到LERP(请参阅:https://en.wikipedia.org/wiki/Linear_interpolation)。

//declare a variable to store the destinated Color;
public Color colorToBeLerped = null;
public Color currentColor = null;
public Color originalColor = null;

public final float deltaFactor = 0.2;

public void rainbow() {

    if(currentColor==null||currentColor.equals(colorToBeLerped)){
        Random generator = new Random();
        int r = generator.nextInt(256);
        int g = generator.nextInt(256);
        int b = generator.nextInt(256);
        colorToBeLerped = new Color(r, g, b);
        originalColor = colorToBeLerped;
    }
    if(currentColor==null){
        currentColor=colorToBeLerped;
    }else{
    //using the Color constructor that accepts float arguments, divide 255
        currentColor= new Color((colorToBeLerped.getRed()-originalColor.getRed()*deltaFactor)/255,
                                (colorToBeLerped.getGreen()-originalColor.getGreen()*deltaFactor)/255,
                                (colorToBeLerped.getBlue()-originalColor.getBlue()*deltaFactor)/255);
    }


    g2.setPaint(currentColor);
}

阐释:

1.跟踪要渲染的颜色,调用随机函数时的当前颜色和原始颜色。

2.当我们第一次调用彩虹函数时,当前颜色将被设置为随机颜色。

3.对于每个刻度,如果当前颜色不是目标颜色,我们将增加原始颜色和目标颜色之间差异的1/5。

4.恒定delta因子0.2表示我们需要5个刻度才能从一种颜色到另一种颜色,这个变量越小,它从原始颜色到目标颜色的时间就越长。

5.如果我们达到了颜色,我们将选择另一种新的目标颜色。

*未经测试但我认为你可以找出其余的