Java Reference
In-Depth Information
Color and Java graphics
InJavagraphicsthevideodisplaycolorisanattributeofthegraphicsob-
ject.Onceacolorisset,itisusedforalldrawingoperationsthatfollow.To
drawinmultiplecolorsyoumustselecttheminsuccession.TheColorclass
Java'sAWTencapsulatescolors.Severalmethodsareavailablefordefining
colors.Oneofthemisbasedonthepre-definedcolornames,shownin Ta-
ble27-1 .
Table27-1
JavaStandardColors
black
green
red
blue
lightGraywhite
cyan
magenta
yellow
darkGrayorange
gray
pink
RGB color designation
Basedonthephysicsoflight,acolorcanalsobedefinedbyitsred,green,
andbluecomponents.ThismethodiscalledtheRGBcolorformat.Con-
structorsoftheColorclassallowsacustomcolortobecreatedbyspecify-
ingitsred,green,andbluecomponents.InoneconstructortheRGBvalues
arespecifiedasfloatingpointnumbersintherange0.0to1.0.Anothercon-
structorallowsspecifyingtheRGBvaluewithintegers.Athirdconstructor
reflectsthearchitectureofthemostcommonvideocardswhichuseabyte
valuetoencodeeachoftheRGBcomponents.Usingthisthirdconstructor
youcancreateobjectsoftheColorclass,asfollows:
Color aRed = new Color(200, 0, 0);
Color lightBlue = new Color(0, 0, 210);
Color pink = new Color(200, 50, 50);
Color blackest = new Color(0, 0, 0);
Color whitest = new Color(255, 255, 255);
Onceacolorhasbeendefined,itsnamecanbeusedtoinstallitasan
attributeoftheJavagraphicsobjectbymeansofthesetColor()method.
ThemethodtakesaColorobjectasaparameter,whichcanbeacolorcre-
atedusingoneofthepre-definedcolornamesin Table27-1 , oritcanalso
be a named color using one of the other constructors. For example:
g.setColor(aRed);
g.setColor(Color.orange);
You can also create a customized color and install it as an attribute of
the graphics object in the same statement:
g.setColor(new Color(0, 128, 128));
 
Search WWH ::




Custom Search