• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel random backgraound  RSS feed

 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i need when i click the mouse on the JPanel to random change background color.



What i should be to put on place on Color.blue to make random background color. i need only 16 color random.

thank you
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my guess would be to load 16 colors into an array and then when the user clicks the mouse have a random number between 0 and 15 generated. Choose that array item and change the color to it.

You have to make the array an array of Color Objects by the way.
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,
but give a way how to do that i know in pascal and c++ it's easy do it. please help me because it's is my part of exam!
 
Syskata Mitev
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and 16 color is a big deal for me to go on. but rgb color is better to understand for me.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look over the stuff here
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html

how you create an array of Colors is


As far as populating it with Colors I cannot figure it out since Im new at this too. The colors are static variables so we should be able to reference the color Red by Color.Red but It does not work. I will look into it further. Unless someone else know how to do this. I made it 14 instead of 16 because there are not 16 pre-defined colors.
[ November 29, 2007: Message edited by: James Hambrick ]
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link I gave before shows you how use RGB colors. Post some code declaring the array, populating it with 16 colors of your choice. Once you have that down you can then created a method that generates a random number between 0 and 15. Then you just select one out of the array. Break it down into steps like that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!