2015-11-05 76 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.如果我們已經達到了顏色,我們會選擇另一種新的目標顏色。

*未經測試,但我認爲你可以計算出其餘