Do you like to let the program do some mischief and make others feel uncomfortable? If you answer "yes", then this month's tip must be for your appetite. With J2SE 1.4, your Java program can now change the video mode and gain absolute control over the screen. You don't have to let other people play with the computer, you can almost have the entire control. Thank you for the new Full-screen Exclusive mode (FEM) API, which provides us with this unparalleled and powerful feature.
Even if you say "no" and don't want to annoy others, you'll find that the FEM API provides a lot of help. The FEM API provides full control of the display by writing the memory directly, which is ideal for game development, although there are many other applications. For example, some programs can only look better with a specific size screen and work better. Read on to discover your inner-control myths.
Change how you display
Let's start with the Java.awt.DislayMode class that studies the FEM API, which wraps the screen size and refresh rate for a particular display. Supported in a way that is specific to system hardware support.
To find out how a particular system is supported, check the graphicsenvironment. In this environment, you can get the default screen device Graphicdevice, which can be displayed as shown in Listing 1:
Listing 1. Find how to display
GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDevice =
graphicsEnvironment.getDefaultScreenDevice();
DisplayMode displayModes[] =
graphicsDevice.getDisplayModes();
You can also use the GetDisplayMode () method to get the current display mode, as shown in Listing 2:
Listing 2. Get the current display mode
DisplayMode originalDisplayMode =
graphicsDevice.getDisplayMode();
The change is proving relatively easy, but you must first use the GraphicsDevice isdisplaychangesupported () method to ask if the graphics device involved supports the change.
Once you know this, to change the way, use the Setdisplaymode () method to pass in the new way. Display mode changes typically occur in a try/finally block so that the finally block resets the code to its original form. Although this procedure is not absolutely necessary, it ensures a secure way to complete the program. Listing 3 shows the typical patterns used to change the way the display is displayed:
Listing 3. How to change
GraphicsDevice graphicsDevice = ...
DisplayMode originalDisplayMode = graphicsDevice.getDisplayMode();
DisplayMode newDisplayMode = ...
try {
if (graphicsDevice.isDisplayChangeSupported()) {
graphicsDevice.setDisplayMode(newDisplayMode);
}
} finally {
graphicsDevice.setDisplayMode(originalDisplayMode);
}