Color |
Tutorial Home | ||
Intro | All of our objects up until now have been white on a black background. This is boring. Any object (except groups and axes) can have their color changed. In addition the background of the window can be changed from white. | |
RGB |
The colors visible by humans are composed of red, green and blue (RGB) because those are the color sensors in the human eye. All colors in the Phun system are defined by either a named color that was previously defined or by three numbers, one for each of red, green and blue.
Each component is defined as a floating point number between 0.0 (no light) and 1.0 (maximum light). The following are examples:
|
|
Named Colors |
The Color class also provides several named colors that can be used directly rather than creating a new custom color.
For a complete list of color constants see the Color class javadocs. |
|
Background color |
A Win2D object has a background color. Its default is white, however, this can be changed using the win.setBackground() methods. One of these methods takes a Color and the other takes three double values for red, green and blue. The following code shows how they are used. public static void main (String []args) { Win3D win = new Win3D("Tutorial",10,20,400,400); win.add(new Axes(3)); win.setLookFrom(-3,4,7); win.setLookAt(0,1,0); win.setBackground(Color.LIGHT_GRAY); win.add(new SphereObj()); } The image below is what is produced. |
|
Colored Objects |
Any object can have its color changed using the obj.setColor(...) method. As with setBackground(...) there is a form with a Color object and a form that takes the RGB values for the desired color. The following code shows a sphere receiving a named color for yellow and the floor receiving a medium brown color constructed from the RGB primaries. public static void main(String[] args) { Win2D win = new Win2D("Tutorial",10,20,400,300); win.setBackground(Color.LIGHT_GRAY); Rect myRect = new Rect(2,2,3,5); win.add(myRect); myRect.setFillColor(Color.BLUE); myRect.setLineColor(null); // Setting the line color to null eliminates the border Oval myOval = new Oval(0,0,8,10); win.add(myOval); myOval.setFillColor(null); // Setting the fill color to null eliminates the fill myOval.setLineColor(new Color(0.2,0.6,0.6)); myOval.setLineWidth(0.4); win.add(new Axes()); } The resulting image is shown below: |