2014-10-09 124 views
0

好吧,所以我卡在一個方法,我必須返回圖像中的每個組件,但每個組件的顏色隨機化。這是我到目前爲止:隨機化圖像組件的顏色

public Picture colourComponentImage() 
{ 
    Picture picture = new Picture(fileLocation); 
    int width = picture.width(); 
    int height = picture.height(); 

    // convert to black and white 
    for (int x = 0; x < width; x++) 
    { 
     for (int y = 0; y < height; y++) 
     { 
      Color color = picture.get(x, y); 
      if (Luminance.lum(color) < threshold) 
      { 
       picture.set(x, y, Color.BLACK); 
      } 
      else 
      { 
       picture.set(x, y, Color.WHITE); 
      } 
     } 
    } 

    // union - find data structure 
    connected(height, width); 
    find(height); 
    union(height, width); 

    // Randomises the colour of each component 

    Random random = new Random(); 

    float r = random.nextFloat(); 
    float g = random.nextFloat(); 
    float b = random.nextFloat(); 

    Color randomColor = new Color(r, g, b); 
    return picture; 
} 

有人可以告訴我我要去哪裏嗎?

+2

什麼不會在你目前的工作方式? – Junuxx 2014-10-09 09:02:26

+0

當我運行該程序時,圖片不會顯示,因爲我無法看到圖片,我不知道顏色隨機化是否正常工作 – 2014-10-09 09:10:54

+0

你知道如何在java中繪圖嗎?你需要一個頂級的容器(框架),並在其圖形上繪製...你知道這一點,你是否在你的代碼中這樣做?如果**是**請提交該代碼... if ** no ** plaes study http://docs.oracle.com/javase/tutorial/2d/images/ – 2014-10-09 09:45:18

回答

0

我真的不知道,如果這是你試圖acheive ...

public Picture colourComponentImage() 
{ 
    Picture picture = new Picture(fileLocation); 
    int width = picture.width(); 
    int height = picture.height(); 

    // Create two random colours 
    Random random = new Random(); 
    Color randomColourOne = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()); 
    Color randomColourTwo = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat()); 

    // Convert to two tones of any two random colours 
    for (int x = 0; x < width; x++) 
    { 
     for (int y = 0; y < height; y++) 
     { 
      Color color = picture.get(x, y); 
      if (Luminance.lum(color) < threshold) 
      { 
       picture.set(x, y, randomColourOne); 
      } 
      else 
      { 
       picture.set(x, y, randomColourTwo); 
      } 
     } 
    } 

    // union - find data structure 
    connected(height, width); 
    find(height); 
    union(height, width); 

    return picture; 
}