9 Replies - 5461 Views - Last Post: 06 August 2010 - 09:01 PM Rate Topic: -----

#1 umair shaikh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-August 10

Display Random Color in Swing

Posted 06 August 2010 - 10:23 AM

pls help me display random color in swing
Is This A Good Question/Topic? 0
  • +

Replies To: Display Random Color in Swing

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Display Random Color in Swing

Posted 06 August 2010 - 10:42 AM

You'll have to post some code in order to demonstrate that you've actually tried this and are looking for guidance.

My guess:

Get random number.
Retrive a Color variable from a Color[] array using the random number generated.
Was This Post Helpful? 0
  • +
  • -

#3 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Display Random Color in Swing

Posted 06 August 2010 - 10:47 AM

Color constructor takes three integers (R,G,B ) so what you need is (randomValue1, randomValue2, randomValue3).

This post has been edited by Mercurial: 06 August 2010 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Display Random Color in Swing

Posted 06 August 2010 - 10:59 AM

Topic renamed to be more descriptive. Please respect rule #4 of this forum. "Java" is not a descriptive title in a forum dedicated to Java.

What have you tried? Can you show us your good faith efforts? What specific problems are you having?
Was This Post Helpful? 0
  • +
  • -

#5 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Display Random Color in Swing

Posted 06 August 2010 - 11:13 AM

One way to get a truly (in theory) random color and avoids arrays:
Random r = new Random();
Color randomColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));


Was This Post Helpful? 1
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Display Random Color in Swing

Posted 06 August 2010 - 03:15 PM

Or just do

Color c = new Color((int)(Math.random() * 0xFFFFFF));

Was This Post Helpful? 3
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Display Random Color in Swing

Posted 06 August 2010 - 03:17 PM

What does 0xFFFFFF mean?
Was This Post Helpful? 0
  • +
  • -

#8 Mercurial  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: Display Random Color in Swing

Posted 06 August 2010 - 03:30 PM

View Poststapia.gutierrez, on 06 August 2010 - 02:17 PM, said:

What does 0xFFFFFF mean?

It's a Color hex value. FF = 255, in dec. 0xFFFFFF being 255,255,255 in this case.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Display Random Color in Swing

Posted 06 August 2010 - 04:12 PM

If you see a number prefaced with 0x, that is a hexadecimal literal. A number prefaced with 0 is an octal literal. With Java 7, we get binary literals as well.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Display Random Color in Swing

Posted 06 August 2010 - 09:01 PM

View Postg00se, on 06 August 2010 - 04:15 PM, said:

Or just do

Color c = new Color((int)(Math.random() * 0xFFFFFF));

Never though of that one +1 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1