Java AVG game development entry [1] -- drawing CG

Source: Internet
Author: User


As an adventure game, the graphic part of AVG has always been one of the core of the game. Therefore, this article will focus on Image Rendering to explain the CG generation problem of AVG. (CG, that is, computer graphics, can be called [Computer Graphics]. Here it is used as the alias for the graphic part in AVG development ).

 

 

When we were young, we might be impressed by the gorgeous effects of AVG games. But now, we all know that accomplishing those tasks is just the basic capabilities of programmers. Even if they are not professional game developers, they can easily do so.

 

As we all know, Image Rendering in Java is very easy, whether you are using ImageIO, imageicon or toolkit. getdefatooltoolkit (). if you use createimage or other methods to obtain an image (or bufferedimage), the processing method is the same, that is, using graphics.

 

Graphics is an abstract class. Therefore, image is usually required to introduce its instance.

 

In the Java AWT package, the basic usage of graphics is as follows.

 


 Public void paint (Graphics g) {<br/> // set the color <br/> G. setcolor (...); <Br/> // set the font <br/> G. setfont (...); <Br/> // draw text <br/> G. drawstring (...); <Br/> // draw a line segment <br/> G. drawline (...); <Br/> // draw a rectangle <br/> G. drawrect (...); <Br/> // fill the rectangle <br/> G. fillrect (...); <Br/> // draw an ellipse <br/> G. drawoval (...); <Br/> // fill in the ellipse <br/> G. filloval (...); <Br/> // draw a polygon <br/> G. drawpolygon (...); <Br/> // fill a polygon <br/> G. fillpolygon (...); <Br/> // display the image <br/> G. drawimage (...); <Br/> // For more information, see <br/> //... <Br/>}

 

However, for some advanced effects, graphics2d is required.

 

Graphics2d is also an abstract class inherited from graphics. It extends graphics and provides more complex control over ry, coordinate transformation, color management, and text layout. It is a basic class used to present two-dimensional shape, text and image advanced features on the Java platform.

 

Because graphics2d is a subclass of graphics, graphics can be directly converted.

 

In the Java AWT package, the basic usage of graphics2d is as follows.

 

Public void paint (Graphics g) {<br/> // obtain graphics2d instance <br/> graphics2d g2d = (graphics2d) g; <br/> // original graphics part <br/> // set the color <br/> g2d. setcolor (...); <Br/> // set the font <br/> g2d. setfont (...); <Br/> // draw text <br/> g2d. drawstring (...); <Br/> // draw a line segment <br/> g2d. drawline (...); <Br/> // draw a rectangle <br/> g2d. drawrect (...); <Br/> // fill the rectangle <br/> g2d. fillrect (...); <Br/> // draw an ellipse <br/> g2d. drawoval (...); <Br/> // fill in the ellipse <br/> g2d. filloval (...); <Br/> // draw a polygon <br/> g2d. drawpolygon (...); <Br/> // fill a polygon <br/> g2d. fillpolygon (...); <Br/> // display the image <br/> g2d drawimage (...); <Br/> // added some graphics2d functions <br/> // set the paint <br/> g2d. setpaint (...); <Br/> // set the line width <br/> g2d. setstroke (...); <Br/> // set composite (more alphacomposite) <br/> g2d. setcomposite (...); <Br/> // set the moving margin <br/> g2d. Translate (...); <Br/> // set the scale <br/> g2d. Scale (...); <Br/> // set rotation <br/> g2d. Rotate (...); <Br/> // set the cropping <br/> g2d. shear (...); <Br/> // set coordinate deformation <br/> g2d. settransform (...); <Br/> // create a specific shape instance <br/> shape = new yourshape (...); <Br/> // set the specified shape <br/> g2d. draw (SHAPE); <br/> // fill the specified shape <br/> g2d. draw (SHAPE); <br/> // set renderinghints (class used for drawing fine-tuning settings) <br/> g2d. setrenderinghint (...); <Br/> // For more information, see <br/> //... <Br/>}< br/>


 

No matter how complicated the code is to be constructed, the basic java drawing process is only image-> graphics-> paint. You only need to use a repeating repaint function, we can repeat this process countless times. I will not repeat it here because it has been involved in many other blog posts.

 

After all, the cg in the AVG game is nothing more than a mix of images and then a simple reproduction of this process.

 

Shows the specific merging relationship:

 

 

 

In my personal opinion, in the 2D AVG, layering only requires two layers of difference between foreground and background.

 

The reason is that graphics or graphics2d draw images sequentially in drawimage, and the old images will be overwritten by the new ones. Therefore, even if there are more images, it is just the process of generating the alternate background foreground, covering and switching over and over again, and finally the only CG is drawn to the screen.

 

Therefore, we can also come up with the most basic concept of AVG game development, that is, when an image is added, the background image should always be added before, and the foreground image should always be added after, the active part of the image is always used as the foreground, and the inactive part is always used as the background.

 

In the sample program in this article, the specific implementation code is as follows (Please download for details ):

 

Public void draw (final graphics g) {<br/> If (sleep <= 0) {<br/> If (CG. getbackgroundcg ()! = NULL) {<br/> If (shakenumber> 0) {<br/> graphics. drawimage (CG. getbackgroundcg (), shakenumber/2 <br/>-control. rand. nextint (shakenumber), shakenumber <br/>/2-control. rand. nextint (shakenumber), null); <br/>} else {<br/> graphics. drawimage (CG. getbackgroundcg (), 0, 0, null); <br/>}< br/> for (INT I = 0; I <CG. getcharas (). size (); I ++) {<br/> chara = (chara) CG. getcharas (). get (I); <br /> Graphics. drawimage (chara. getcharactercg (), chara. getx (), chara <br/>. gety (), null); <br/>}< br/> If (ismessage) {<br/> dialog. showdialog (dialogimage, graphics); <br/> for (INT I = 0; I <stringmaxline; I ++) {<br/> graphics. setcolor (color. black); <br/> for (Int J = 0; j <messages [I]. length (); j ++) {<br/> utility. drawstring (messages [I]. substring (J, J + 1) <br/>. tostring (), Lib. fontname, graphics, l IB. font <br/> * j + dialog. getmessage_line_x () + 2, I <br/> * (Lib. font + Lib. font_size) + Lib. font + 1 <br/> + dialog. getmessage_line_y (), 1); <br/>}< br/> If (flags [selectflag]! =-1) {<br/> graphics. setcolor (color. white); <br/> for (INT J1 = 0; J1 <messages [selectflag]. length (); J1 ++) {<br/> utility. drawstring (messages [selectflag]. substring (<br/> J1, J1 + 1 ). tostring (), Lib. fontname, <br/> graphics, Lib. font * J1 <br/> + dialog. getmessage_line_x (), <br/> selectflag * (Lib. font + Lib. font_size) <br/> + Lib. font <br/> + dialog. getmessage_line_y (), 1); <br/>}< br/> dialog. showdial OG (selectflag, Lib. font, Lib. font_size, <br/> dialogimage, graphics); <br/>}< br/> If (flags [I] =-1) {<br/> graphics. setcolor (color. white); <br/>}else {<br/> graphics. setcolor (color. gray); <br/>}< br/> for (int count = 0; count <messages [I]. length (); count ++) {<br/> utility. drawstring (messages [I]. substring (count, <br/> count + 1 ). tostring (), Lib. fontname, graphics, <br/> Lib. font * count + dialog. g Etmessage_line_x (), <br/> I * (Lib. font + Lib. font_size) + Lib. font <br/> + dialog. getmessage_line_y (), 1); <br/>}< br/>} else {<br/> sleep --; <br/> If (color! = NULL) {<br/> graphics. setcolor (color); <br/> graphics. fillrect (0, 0, Lib. width, Lib. height); <br/> utility. wait (20); <br/>}< br/> // set the background <br/> G. drawimage (screen, 0, 0, null); <br/> G. dispose (); <br/>}

 

Next time, we will begin to explain the story development and script customization of AVG.

 

The sample code interface is as follows:

 

 

 

 

 

,

 

Example: http://download.csdn.net/source/999273 (source code in jar)

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.