2015-11-05 67 views
1

我做了一个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; 
      } 
     } 
    }); 
} 

涂料成份:

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); 
} 
+0

去这里:http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java –

+0

你想每个椭圆形是渐变还是你想绘制的线看起来像一个渐变? –

+0

我希望绘制的线看起来像一个渐变的颜色 – flowinwind

回答

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.Keep轨道要线性插值到,当前的颜色,你是在和原来的颜色,当随机化函数被调用的颜色。

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

3.对于每一个刻度,如果当前颜色不是目标颜色,我们将之差是多少原来的颜色和目标颜色之间的1/5递增。 0.2

4.A不断增量的因素意味着,我们将需要5个蜱获得从一种颜色到另一种颜色,这个变量是较小的,时间越长,会得到从原始颜色的目标颜色。

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

*未经测试,但我认为你可以计算出其余