2016-04-16 16 views
2

Ich muss Farben in Swing GUI nach dem Zufallsprinzip generieren, aber das Problem ist, dass ich will, dass sie nur hell sind.Wie erzeugt man zufällig helle Farbe?

+0

Ich denke, dieser Beitrag kann Ihnen helfen. http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color –

+0

Einige [Beispiele] (http://stackoverflow.com/search?tab=votes&q=user%3a230513%20Color .getHSBColor). – trashgod

Antwort

3

Verwenden Sie die statische Color-Klasse-Methode getHSBColor(...), und stellen Sie sicher, dass der dritte Parameter, der die Helligkeit darstellt, für Sie ausreichend hoch ist, vielleicht> 0.8f (aber < 1.0f).

Zum Beispiel verwendet der Code unter der oben beschriebenen Methode eine zufällige helle Farbe zu finden:

float h = random.nextFloat(); 
    float s = random.nextFloat(); 
    float b = MIN_BRIGHTNESS + ((1f - MIN_BRIGHTNESS) * random.nextFloat()); 
    Color c = Color.getHSBColor(h, s, b); 

eine Zufallsvariable namens zufällig und einen MIN_BRIGHTNESS Wert von 0,8 f:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.util.Random; 

import javax.swing.*; 

public class RandomBrightColors extends JPanel { 
    private static final int PREF_W = 500; 
    private static final int PREF_H = PREF_W; 
    private static final int RECT_W = 30; 
    private static final int RECT_H = RECT_W; 
    private static final float MIN_BRIGHTNESS = 0.8f; 
    private Random random = new Random(); 

    public RandomBrightColors() { 
     setBackground(Color.BLACK); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (int i = 0; i < 100; i++) { 
      g.setColor(createRandomBrightColor()); 
      int x = random.nextInt(getWidth() - RECT_W); 
      int y = random.nextInt(getHeight() - RECT_H); 
      g.fillRect(x, y, RECT_W, RECT_H); 
     } 
    } 

    private Color createRandomBrightColor() { 
     float h = random.nextFloat(); 
     float s = random.nextFloat(); 
     float b = MIN_BRIGHTNESS + ((1f - MIN_BRIGHTNESS) * random.nextFloat()); 
     Color c = Color.getHSBColor(h, s, b); 
     return c; 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private static void createAndShowGui() { 
     RandomBrightColors mainPanel = new RandomBrightColors(); 

     JFrame frame = new JFrame("RandomBrightColors"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      createAndShowGui(); 
     }); 
    } 
} 

bearbeiten: oder wenn die Farben vollständig gesättigt sein sollen, ändern Sie den Sättigungsparameter in 1f:

Beachten Sie auch, dass dies mit einer 3-stelligen int-RGB-Farbe erfolgen kann. Beachten Sie jedoch, dass ein Parameter nahe bei 255 aber nicht größer als 255 sein sollte, einer nahe aber nicht unter 0 sein sollte und der andere sein kann zufällig alles zwischen 0 und 255.

Verwandte Themen