5 Replies - 59266 Views - Last Post: 28 April 2010 - 09:36 PM Rate Topic: -----

#1 skim  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Create a Random Color generator?

Posted 18 November 2008 - 09:11 PM

Hey,
I'm new to the forum as a member, but I have browsed through a few times looking over different tips. Anyway, I am currently doing a homework assignment and am not sure how to make a random color generator. I thought about creating a random number generator and making it psuedo random by assigning a color based on the integer the generator chooses. I was wondering if that is the easiest way or someone has other suggestions. I have hit a wall and can't seem to think of anything else. Any ideas would be greatly appreciated

	/**
	 * Create a box with a user defined set of balls that bounce around the inside and off the walls.
	 */
	public void boxBounce(int numberOfBalls)
	{
		int ground = 400; //Ground position
		int wall = 0; //wall position
		myCanvas.setVisible(true);
		
		//Draw the box
		myCanvas.getSize();
		Rectangle box = new Rectangle(20, 20, ((int)myCanvas.getSize().getWidth() - 40), ((int)myCanvas.getSize().getHeight() - 40));
		myCanvas.draw(box);
		
		//Create the balls and add them to an ArrayList
		ArrayList<BallBoxBounce> balls = new ArrayList();
		Random numGen = new Random();
		Random colorGen = new Random();
		
		for (int count = 0; count < numberOfBalls; count++)
		{
			if (numGen / 2 == true)
			{
				return even;
			}
			else
			{
				if (numGen / 2 == false)
				{
					return odd;
				}
				else
				{
					if (numGen / 3 == true)
					{
						return divByThree;
					}
					else (numGen == 1)
					{
						return isOne;
					}
				 }	  
			 }
			 return 0;
				
			BallBoxBounce ball = new BallBoxBounce(numGen.nextInt(350), numGen.nextInt(80), numGen.nextInt(50), Color.colorGen, ground, myCanvas);
		}
	}
}



EDIT - fixed the dumb bracket that wasn't enclosing the code tags.

This post has been edited by skim: 18 November 2008 - 09:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Create a Random Color generator?

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Create a Random Color generator?

Posted 18 November 2008 - 09:29 PM

Thanks for trying to post correctly!

This post has been edited by n8wxs: 18 November 2008 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

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

Re: Create a Random Color generator?

Posted 18 November 2008 - 10:01 PM


		Random numGen = new Random();
....
....
   Color getRandomColor() {
	  return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
   }


Was This Post Helpful? 3

#5 skim  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 18-November 08

Re: Create a Random Color generator?

Posted 18 November 2008 - 10:04 PM

Wow..
Thank you I have done a few things recently and the answer is always so obvious and I wonder how I miss it.
Was This Post Helpful? 0
  • +
  • -

#6 Ariunbold  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-November 09

Re: Create a Random Color generator?

Posted 28 April 2010 - 06:59 AM

I wanted to generate random color, first I thought to make an array of colors but wanted to see if there was better alternative...
after googling, i found this... thanks for the answer!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

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

Re: Create a Random Color generator?

Posted 28 April 2010 - 09:36 PM

View PostAriunbold, on 28 April 2010 - 07:59 AM, said:

I wanted to generate random color, first I thought to make an array of colors but wanted to see if there was better alternative...
after googling, i found this... thanks for the answer!

Why make things complicated for nothing ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1