Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the random library:</p> <pre><code>import java.util.Random; </code></pre> <p>Then create a random generator:</p> <pre><code>Random rand = new Random(); </code></pre> <p>As colours are separated into red green and blue, you can create a new random colour by creating random primary colours:</p> <pre><code>// Java 'Color' class takes 3 floats, from 0 to 1. float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat(); </code></pre> <p>Then to finally create the colour, pass the primary colours into the constructor:</p> <pre><code>Color randomColor = new Color(r, g, b); </code></pre> <hr> <p>You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour.</p> <pre><code>// Will produce a random colour with more red in it (usually "pink-ish") float r = rand.nextFloat(); float g = rand.nextFloat() / 2f; float b = rand.nextFloat() / 2f; </code></pre> <p>Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element:</p> <pre><code>// Will produce only bright / light colours: float r = rand.nextFloat() / 2f + 0.5; float g = rand.nextFloat() / 2f + 0.5; float b = rand.nextFloat() / 2f + 0.5; </code></pre> <p>There are various other colour functions that can be used with the <code>Color</code> class, such as making the colour brighter:</p> <pre><code>randomColor.brighter(); </code></pre> <p>An overview of the <code>Color</code> class can be read here: <a href="http://download.oracle.com/javase/6/docs/api/java/awt/Color.html" rel="noreferrer">http://download.oracle.com/javase/6/docs/api/java/awt/Color.html</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload