您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 22

Colour

Colour is controlled by objects of Color class (note the US spelling). Colour is specified as the level of red, green and blue, where each is between 0 and 255, or between 0 and 1. Not all displays can display all possible colours, and many are limited in the number of simultaneous colours available. The display may use dithering to try to match the requested colour, or may just use the closest colour available.

The methods setColor() and setBackground() in class Graphics are used to specify the colours of any component. The arguments to these methods are objects of class colour. When creating a new Color object, there are two ways to specify which colour to use:

  • specify individual levels of red, green and blue
  • specify one of the built-in colour names

An example of the first approach might be:

g.setColor(new Color(0.2, 0.0, 0.0));

which produces a very dark red. To use a built-in colour, we write:

g.setColor(new Color(Color.red));

In order to confuse the student, the levels of red, green and blue can be specified either as numbers between 0 and 255, or between 0 and 1. In either case, 0 means ‘none of that colour', while 1 or 255 means ‘this colour fully on'.

How does the Color object know whether we are using the range 0-1, or the range 0-255?

If the numbers are floating point numbers (e.g., 1.0) then it will assume we are using the 0-1 range, and if they are integers (e.g., 1) it will assume the range is 0-255. If this were not confusing enough, there is another method for specifying colours called the ‘sRGB' standard which uses four numbers rather than three. Anyone interested should consult the on-line documentation for full details.

Although most computer displays currently in use can display a wide range of colours, many still limit the number of different colours that are available at one time. This is to limit the amount of memory used to store the screen contents. For example, if we use 1 byte to store the colour of each screen pixel, then we will have 256 different colours available (because one byte can store numbers between 0 and 255). The real colours that these 256 numbers map on to is called the palette. Most displays will use one of two methods if the program asks for a colour that is not currently part of the palette. Either the nearest available colour will be used, or the display will use dithering to synthesize the colour.

‘Dithering’ is the process of synthesising an unavailable colour by placing dots of different colours close together. For example, orange can be synthesised by showing alternating dots or red and green. Modern displays do not normally need to use dithering.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg