Java Game start: (ii) graphics base and display text

Source: Internet
Author: User
Tags window
Graphics | Show Basic Concepts!


screen Resolution: The screen area in pixel, such as 800*600


bit Depth: Displays the number of colors. such as 8-bit 256-color, 16-bit 65536-color


refresh rate: unit Hz





a useful Object!


Window object: Represents the screen content and can be viewed as a drawing canvas, such as JFrame is a subclass of window


DisplayMode object: Used to specify screen resolution, bit depth, and refresh rate


GraphicsDevice object: Used to change the display properties, can be seen as manipulating the graphics interface, by Graphicsenvironment object to obtain





An example of putting jframe into full screen!


import java.awt.*;


import Javax.swing.JFrame;


//Prepare three objects first


JFrame jf=new JFrame ();


displaymode displaymode=new DisplayMode (800,600,16,75);


graphicsdevice device=graphicsenvironment.getlocalgraphicsenvironment (). Getdefaultscreendevice ();


//jframe to full screen


Device.setfullscreenwindow (JF);


//Change display mode


Device.setdisplaymode (DisplayMode);


//Exit Full screen


Device.setfullscreenwindow (NULL);


//Want to make the effect better, you can change the way jframe is displayed. such as:


Jf.setundecorated (TRUE);


jf.setresizable (FALSE);


//In addition, you need to add the necessary judgment and catch exceptions when changing the display mode, like this:


if (device.isdisplaychangesupported ()) {


try {


Device.setdisplaymode (DisplayMode);


}catch (IllegalArgumentException ex) {}


}


//show a HelloWorld try? Add in a jframe:


public void Paint (Graphics g) {


g.drawstring ("Hello world!", 20, 50);


}


//Text can be used Java.awt.Graphics2D class Anti-aliasing!


public void Paint (Graphics g) {


if (g instanceof graphics2d) {


graphics2d g2d= (graphics2d) g;//graphics2d is a subclass of graphics


//anti-aliasing, drawing quality, etc. can refer to the Renderinghints class document!


G2d.setrenderinghint (renderinghints.key_text_antialiasing,renderinghints.value_text_antialias_on);


}


g.drawstring ("Hello world!", 20, 50);


}


almost, the next write draw graphics!





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.