Two Laptop Bag*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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: 15299
    
    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 - Experts at Building Test Data
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: 15299
    
    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: 39478
    
  28
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.