Creating a Display Screen
Once you've drawn something to graphics resource, it won't mean much unless you display it on the screen or save it to a file.
In this section, we'll cover how to output the rendered image to the screen.
Creating a Display Window
To make use of 2D graphics in a running program, you'll need a screen to display the graphics data you've drawn.
For this, you can use functions from the "GUI" library to create a display window.
Creating a Window
To create a window, use the "newWindow" function from the GUI library.
- Function Format -Arguments:
- x, y: Coordinates of the top-left corner of the window.
- width, height: Size of the window.
- title: The title text to be shown in the window's title bar.
When you call this function, a window will be created and its assigned component ID will be returned as an int. A component ID is a unique identifier assigned to any GUI component.
Creating an Image Label
To display the contents of graphics data within a window, youfll need to create a GUI component called an **image label**. This can be done using the "newImageLabel" function.
- Function Format -Arguments:
- x, y: Coordinates of the top-left corner of the image label.
- width, height: Size of the image label.
- grarphicsID: The ID of the graphics data you want to display.
This function returns the component ID of the image label that is created.
Repainting the Screen
To reflect changes in your GUI, you need to repaint the affected components.
If you change the contents of the graphics resource, for example, the GUI components that display it -- such as image labels or windows -- will not update automatically. You must explicitly request a repaint to update the screen.
To do this, use the paintComponent function.
- Function Format -Arguments:
- componentID: The component ID of the GUI element you want to repaint (e.g., the value returned by "newWindow" or "newImageLabel" functions).
Whenever you finish drawing 2D graphics, make sure to call this function to update the screen properly.
Example Program
import Graphics;
import Graphics2D;
import GUI;
// Create graphics resource
int graphicsID = newGraphics( );
// Create a renderer
int rendererID = newGraphics2DRenderer( 800, 600, graphicsID );
// Create a window (800x600) titled "Hello, 2DCG!"
int windowID = newWindow( 0, 0, 800, 600, " Hello , 2DCG ! " );
// Create an image label to show the graphics resource
int labelID = newImageLabel( 0, 0, 800, 600, graphicsID );
// Mount the image label onto the window
mountComponent( labelID, windowID );
// Paint the GUI components
paintComponent( labelID );
paintComponent( windowID );
Sample.vcssl
When you run this program, a window titled "Hello, 2DCG!" with a size of 800~600 will appear. The window will display the graphics content, but since nothing has been drawn yet, the screen will just show a blank area.
