Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

randomly assign a color from a list of colors to a drawn line

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Currently, when I draw a line onto the drawing panel it is black. However, when each line is drawn, the color is supposed to be randomly assigned from a list of 4 possible colors. (Collections.shuffle is okay to use)

I have 3 classes: BasicLinePix, Line, and Shape.

Shape is an abstract class.

The Line class extends Shape.

I think my


method in BasicLinePix is supposed to call


from the Shape class, but everything I've tried so far hasn't worked.

I included the relevant code from each class that I believe pertains to my question. Thank you.

Shape class:



Line class:





BasicLinePix class:


 
Saloon Keeper
Posts: 3407
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jim,

in your Shape class, the variable 'myColor' is only re-evaluated when you invoke the method 'setColor' of the Shape class.
As long as you do not invoke that method, myColor will remain what it is (if even initialized). So, before invoking 'getColor()', first invoke the Shape method 'setColor()'.

Note: do not confuse the method 'g.setColor' with the setColor method of the Shape class.

But why doing so complicated? Why not make the Color list a member of the Shape class (even static if you wish), and in your 'getColor()' method just return a random Color from that list. You could add a Random 'random' as field to your Shape class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!