13.4-Full Stack Java notes: Combat Aircraft Game Project |offscreenimage| Gameobject| Plane

Source: Internet
Author: User

Dual buffering technology to solve flicker problems

In the previous section, we realized the animation effect, but found that the window will not stop flashing, experience is very poor. In practical development, drawing graphics is very complex, drawing may take a few seconds or longer, and often flicker, in order to solve this problem, we usually use "double buffering technology."


1) The "Double buffering technology" drawing process is as follows:

A. Creating a buffer that is consistent with the canvas in memory

B. Drawing in Buffer

C. Copying a buffer bitmap to the current canvas

E. Freeing memory buffers


Double buffering creates an object in memory that is consistent with the screen drawing area, drawing the graphic to the object in memory, and then copying the graphics on the object to the screen at once, which can greatly speed up the drawing.

We simply put the following "double-buffered" implementation code into the Mygrameframe class, you can:

"Example 1" adds a double buffering technique

Image offscreenimage = null;

public void Update (Graphics g) {

if (offscreenimage = = null)

Offscreenimage = This.createimage (500,500);//This is the width and height of the game window

Graphics Goff = Offscreenimage.getgraphics ();

Paint (Goff);

G.drawimage (offscreenimage, 0, 0, NULL);

}


Gameobject class Design

1) Definition of Gameobject class

We found that all objects in the window (airplanes, shells, and so on) have a lot in common: "Picture object, coordinate position, running speed, width and height". In order to facilitate the development of the program, we need to design a Gameobject class, which can be used as the parent of all game objects to facilitate our programming.

"Example 2" Gameobject class

Package cn.sxt.game;

Import Java.awt.Graphics;

Import Java.awt.Image;

Import Java.awt.Rectangle;

public class Gameobject {

Image img; The object of the object corresponding to the picture

Double x, y; The coordinates of the object

int speed; The speed at which the object runs

int width,height; The width and height of the rectangular area where the object is located

/**

* How to draw this object

* @param g

*/

public void Drawmyself (Graphics g) {

G.drawimage (IMG, (int) x, (int) y, null);

}

Public gameobject (Image img, double x, double y) {

This.img = img;

this.x = x;

This.y = y;

if (img!=null) {

This.width = Img.getwidth (null);

This.height = Img.getheight (null);

}

}

Public gameobject (Image img, double x, double y, int speed, int width,

int height) {

This.img = img;

this.x = x;

This.y = y;

This.speed = speed;

This.width = width;

This.height = height;

}

Public Gameobject () {

}

/**

* Returns the corresponding rectangular area of the object for subsequent use in collision detection

* @return

*/

Public Rectangle GetRect () {

return new Rectangle ((int) x, (int) y, width, height);

}

}


2) Design Aircraft class

With Gameobject this parent class, we design aircraft class is particularly simple, the current aircraft class is not particularly complex requirements. We can use it simply by inheriting it:

"Example 3" plane class

Package cn.sxt.game;

Import Java.awt.Graphics;

Import Java.awt.Image;

public class Plane extends Gameobject {

@Override

public void Drawmyself (Graphics g) {

Super.drawmyself (g);

This.x +=3;//aircraft flying horizontally, we can also adjust the x, Y algorithm to fly according to the path we specify

}

Public Plane (Image img, double x, double y) {

Super (Img,x,y);

}

}

Through inheritance, we find that implementing new classes is a lot more fun!


3) Adjustment of Mygameframe class invocation mode

We encapsulate the plane class, and we don't need to add so many aircraft properties to the Mygameframe class, we're all encapsulated in the plane class, so it's easier to call.

Mygameframe class After "Example 4" is encapsulated

public class Mygameframe extends Frame {

Image bgimg = gameutil.getimage ("images/bg.jpg");

Image planeimg = gameutil.getimage ("Images/plane.png");

Plane Plane = new Plane (planeimg,300,300);

The Paint method works by drawing the entire window and internal content. Automatically called by the system.

@Override

public void Paint (Graphics g) {

G.drawimage (bgimg, 0, 0, NULL);

Plane.drawmyself (g); Draw the plane itself

}

The rest of the code, without any changes, is not attached, refer to the previous version yourself.

}

With object-oriented encapsulation, if we want to create multiple planes again, it becomes surprisingly simple.


"Example 5" creates multiple aircraft

public class Mygameframe extends Frame {

Image bgimg = gameutil.getimage ("images/bg.jpg");

Image planeimg = gameutil.getimage ("Images/plane.png");

Plane Plane = new Plane (planeimg,300,300);

Plane plane2 = new Plane (planeimg,300,350);

Plane plane3 = new Plane (planeimg,300,400);

The Paint method works by drawing the entire window and internal content. Automatically called by the system.

@Override

public void Paint (Graphics g) {

G.drawimage (bgimg, 0, 0, NULL);

Plane.drawmyself (g); Draw the plane itself

Plane2.drawmyself (g); Draw the plane itself

Plane3.drawmyself (g); Draw the plane itself

}

The rest of the code, which is consistent with the previous version, is not attached to the highlighted focus for space saving.

}

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/70/wKiom1mAKDqAPeTJAADzoL-Q-68878.png "title=" Figure 1.png "alt=" Wkiom1makdqapetjaadzol-q-68878.png "/>



"Full stack Java notes" is a can help you from Zeto long for the full stack of Java Engineer Series of notes. The author is called Mr. G,10 years Java research and development experience, has been in the digital, Aerospace Institute of a Research and development center engaged in software design and research and development work, from small white gradually achieve engineers, senior engineers, architects. Proficient in Java Platform Software development, Master Java EE, familiar with various popular development framework.


The notes contain six parts from shallow into deep:

A-java Introductory Phase

B-Database from beginner to proficient

C-hand Edge mobile front end and web front end

D-j2ee from understanding to combat

E-java Advanced Frame Fine Solution

F-linux and Hadoop


This article is from the "full stack Java Notes" blog, be sure to keep this source http://javanew.blog.51cto.com/12931675/1952663

13.4-Full Stack Java notes: Combat Aircraft Game Project |offscreenimage| Gameobject| Plane

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.