Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of colors in paintComponent method.

 
Jonathan Haybok
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
 
Jonathan Haybok
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48418
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a Swing-related topic. Moving.
 
Jonathan Haybok
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic