2014-10-09 3 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

+0

картинка не появится, когда я запустил программу, и так как я не вижу изображение, я не знаю, работает ли рандомизация цвета или нет. –

+0

вы знаете, как рисовать в java? вам нужен контейнер (frame) toplevel и нарисуйте его на своей графике ... знаете ли вы это и делаете ли вы это в своем коде? если ** да **, пожалуйста, отправьте этот код ... если ** нет ** plaes study http://docs.oracle.com/javase/tutorial/2d/images/ –

ответ

0

Я действительно не уверен, если это то, что вы пытаетесь ... получения идеальной

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; 
} 
Смежные вопросы