my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Array of colors in paintComponent method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Array of colors in paintComponent method." Watch "Array of colors in paintComponent method." New topic
Author

Array of colors in paintComponent method.

Jonathan Haybok
Greenhorn

Joined: Mar 12, 2009
Posts: 16
I can't seem to figure out how to fill the polygons I made with random colors. My assignment specifically says to generate 10 random colors, use an array to store the 10 colors and then use the colors to fill the ten polygons I made. Right now my array seems to just hold numbers between 0 and 255, and I don't know how I would set up the 10 RGB sets.

Lines 57 to 66 is where the specific code is.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

setColor expects a java.awt.Color object. What I might do is generate 3 random #'s 10 times. One for each: Red, Green, and Blue. I'd have a java.awt.Color[] and populate it with 10 Color objects.



Then do your page.setColor(color[index]);


GenRocket - A Test Data Generation Platform
Jonathan Haybok
Greenhorn

Joined: Mar 12, 2009
Posts: 16
I wrote this out, and get this: "AWT-EventQueue-0" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green Blue"

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Check the API. It tells you what kinds of arguments the Color constructor takes. Here's a hint, you can't mix int's and float's.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Sounds like a Swing-related topic. Moving.
Jonathan Haybok
Greenhorn

Joined: Mar 12, 2009
Posts: 16
Seems like I'm missing something here...

I tried changing them to floats, but then that was incompatible. So I tried this other format, which doesn't work.

ERROR: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of colors in paintComponent method.
 
Similar Threads
Thread.sleep question
problem with arrays and paint
Help me how to drag and move drawing shaps
Applet not running
Have I clicked on a polygon?