, Niemeyer Pat, Knudsen Jonathan Learning Java 

[ Pobierz całość w formacie PDF ]
.The variable green, for example, is a static member in the Color class.The datatype of the variable green is Color; it is initialized like this:public final static Color green = new Color(0, 255, 0);The green variable and the other static members of Color are not changeable (after they'vebeen initialized), so they are effectively constants and can be optimized as such by the compiler.Constant (or final) static members are the closest thing to a #define construct that you'll findin Java.The alternative to using these predefined colors is to create a color manually byspecifying its red, green, and blue (RGB) components using a Color class constructor.2.3.10 Arrays Next, we turn our attention to the array.We have declared a variable called someColors, whichis an array of Color objects.In Java, arrays are first-class objects.This means that an array is,itself, a type of object that knows how to hold an indexed list of some other type of object.Anarray is indexed by integers; when you index an array, the resulting value is an object referencethat is, a reference to the object that is located in the array's specified slot.Our code uses thecolorIndex variable to index someColors.It's also possible to have an array of simpleprimitive types, such as floats, rather than objects.When we declare an array, we can initialize it by using the familiar C-like curly brace construct.Specifying a comma-separated list of elements inside of curly braces is a convenience thatinstructs the compiler to create an instance of the array with those elements and assign it to ourvariable.Alternatively, we could have just declared our someColors variable and, later, allocatedan array object for it and assigned individual elements to that array's slots.See Chapter 5 for acomplete discussion of arrays.2.3.11 Using Color MethodsSo, we now have an array of Color objects and a variable with which to index the array.Twoprivate methods do the actual work for us.The private modifier on these methods specifiesthat they can be called only by other methods in the same instance of the class.They cannot beaccessed outside of the object that contains them.We declare members to be private to hidethe detailed inner workings of a class from the outside world.This is called encapsulation and isanother tenet of object-oriented design, as well as good programming practice.Private methodsare also often created as helper functions for use solely in the class implementation.The first method, currentColor( ), is simply a convenience routine that returns the Colorobject representing the current text color.It returns the Color object in the someColors array atthe index specified by our colorIndex variable:synchronized private Color currentColor( ) {return someColors[colorIndex];}We could just as readily have used the expression someColors[colorIndex] everywhere weuse currentColor( ); however, creating methods to wrap common tasks is another way ofshielding ourselves from the details of our class.In an alternative implementation, we might haveshuffled off details of all color-related code into a separate class.We could have created a classthat takes an array of colors in its constructor and then provided two methods: one to ask for thecurrent color and one to cycle to the next color ( just some food for thought).The second method, changeColor( ), is responsible for incrementing the colorIndexvariable to point to the next Color in the array.changeColor( ) is called from ouractionPerformed( ) method whenever the button is pressed:synchronized private void changeColor( ) {if (++colorIndex == someColors.length)colorIndex = 0;setForeground(currentColor( ));repaint( );}We increment colorIndex and compare it to the length of the someColors array.All arrayobjects have a variable called length that specifies the number of elements in the array.If we have reached the end of the array, we "wrap around to the beginning" by resetting the index tozero.After changing the currently selected color, we do two things.First, we call the component'ssetForeground( ) method, which changes the color used to draw text in the application.Thenwe call repaint( ) to cause the component to be redrawn with the new color for the draggablemessage.What is the synchronized keyword that appears in front of our currentColor( ) andchangeColor( ) methods? Synchronization has to do with threads, which we'll examine in thenext section.For now, all you need know is that the synchronized keyword indicates these twomethods can never be running at the same time.They must always run one after the other.The reason is that in changeColor( ) we increment colorIndex before testing its value.Thatmeans that for some brief period of time while Java is running through our code, colorIndexcan have a value that is past the end of our array.If our currentColor( ) method happenedto run at that same moment, we would see a runtime "array out of bounds" error.There are, ofcourse, ways in which we could fudge around the problem in this case, but this simple example isrepresentative of more general synchronization issues we need to address.In the next section,you'll see that Java makes dealing with these problems easy through language-levelsynchronization support.2.4 HelloJava4: Netscape's RevengeWe have explored quite a few features of Java with the first three versions of the HelloJavaapplication.But until now, our application has been rather passive; it has waited patiently forevents to come its way and responded to the whims of the user [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • anikol.xlx.pl