Wednesday 2 March 2016

Color Commentary

To support HelloJava3’s colorful side, we have added a couple of new variables and two helpful methods. We create and initialize an array of Color objects representing the colors through which we cycle when the button is pressed. We also declare an integer variable that serves as an index into this array, specifying the position of the current color:

 int colorIndex;  
 static Color[] someColors = { Color.black, Color.red,  
 Color.green, Color.blue, Color.magenta };  
A number of things are going on here. First, let’s look at the Color objects we are putting into the array. Instances of the java.awt.Color class represent colors; they are used by all classes in the java.awt package that deal with basic color graphics. Notice that we are referencing variables such as Color.black and Color.red. These look like examples of an object’s instance variables, but Color is not an object, it’s a class. What is the meaning of this? We’ll discuss that next.

0 comments:

Post a Comment