Clearing the Screen and Setting the Background Color

Now that you're all set up to use 2DCG, it's time to start drawing.

As your first step, let's look at how to clear the drawing area and set the background color.

- Table of Contents -

Clearing the Contents

To clear the contents of a graphics resource that already has something drawn on it, use the "clearGraphics2D" function.

- Function Format -
void clearGraphics2D ( int rendererID )

Arguments:

When you call this function, the renderer will clear the contents of the associated graphics resource, filling it entirely with the background color.

By default, the background color is transparent, which means the base layer of the window will show through.

Depending on your environment, this base layer may appear white, gray, or some other color. So if you plan to display the graphics resource in a window, it's recommended to set the background to an opaque color in advance.

You can set the background color using the function explained below.

Setting the Background Color

To set the background color, use the "setGraphics2DColor" function.

- Function Format -
void setGraphics2DColor (
  int rendererID, int red, int green, int blue, int alpha
)

Arguments:

What Is RGBA Format?

RGBA stands for Red, Green, Blue, and Alpha -- the three primary colors of light, plus an alpha value that represents transparency.

The alpha value controls how transparent the color is: 0 means fully transparent, and higher values move toward full opacity.

The color is produced using additive color mixing, which is the same principle as how light is blended -- not like mixing paints (which uses subtractive color mixing). For example:

Example Program

Let's try setting the background color to blue. Write and run the following code:


import Graphics;
import Graphics2D;
import GUI;

// Create a graphics resource and a renderer
int graphicsID = newGraphics( );
int rendererID = newGraphics2DRenderer( 800, 600, graphicsID );

// Create a display window
int windowID = newWindow( 0, 0, 800, 600, " Hello 2DCG ! " );
int labelID = newImageLabel( 0, 0, 800, 600, graphicsID );
mountComponent( labelID, windowID );

// Set the background color to blue
setGraphics2DColor( rendererID, 0, 0, 255, 255 );

// Clear the graphics using the background color
clearGraphics2D( rendererID );

// Paint the GUI components
paintComponent( labelID );
paintComponent( windowID );
Sample.vcssl

When you run this program, a window filled with a solid blue color will appear on the screen.

Program Output
A window filled entirely with blue will be displayed.