Home E-Books Special Effects and Game Development in Java The basics  

Special Effects and Game Development in Java(TM) - The basics for all the graphics programming in Java 

by Anibal Wainstein

2.1 The basics for all the graphics programming in Java 

I think that the most popular area within the applet programming is without doubt the graphics programming. In these sections we will look at the most important functions that you must know in order to be a full fledged graphics programmer.

2.1.1 What is a pixel? 

A computer screen consist of something that you call pixels. A pixel is a color value that consist of a red, green and blue mix of colors (RGB). In your TV screen you can see how this works, since each dot consist of these three colors. By having different shades of these colors you can get different color. For instance red with green gives yellow, red with blue gives magenta, and so on. For every pixel in the computer screen you can set what mix it should use.

Depending on your graphics card configuration, you can have different types of resolutions. Two of the most common are 800x600 and 1024x768 pixels (pixels per width and pixels per height). You can also have different color resolutions, but this is nothing you have to worry about, since Java takes care of this automatically for you. In section 2.0.3 we had specified that we wanted an applet 100 times 100 pixels large. This area we call the applet screen and should generally not be larger than the computer screen's resolution.

2.1.2 How to mix colors in Java (The Color class) 

With the Color class in Java you can create any color type by specifying different shades of red, green and blue. Color has different constructors but we shall mostly work with the following:

public Color(int red, int green, int blue);

In the constructor we can specify a value between 0 and 255 for each color. Look at the following example: 

//gives black because the value of the three colors 
//is 0  
Color black = new Color( 0, 0, 0); 
//gives blue because all the values are 0 except
//the blue component  
Color blue = new Color( 0, 0, 255); 
//gives white because all the values are 255  
Color white = new Color( 255, 255, 255);               

The color class has default colors that you can use. These exist as Color variables inside the Color class. Since these variables are static variables, you do not need to create a Color object to be able to get to them. We will review static variables later in this course when we get real use of them. The following colors exist:

Color black = Color.black; 
Color blue = Color.blue; 
Color cyan = Color.cyan; 
Color darkgray = Color.darkGray; 
Color gray = Color.gray; 
Color green = Color.green ; 
Color lightgray = Color.lightGray; 
Color magenta = Color.magenta; 
Color orange = Color.orange; 
Color pink = Color.pink ; 
Color red = Color.red; 
Color white= Color white; 
Color yellow = Color.yellow; 

Next Page >>