Merlin's Magic: Exercise Full Control over graphical display

Source: Internet
Author: User
Tags finally block

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);
   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.