The first stage: JAVA quick start (lesson 90: JAVA_Object class design)

Definition of GameObject class

We found that all objects in the window (aircraft, artillery shells, etc.) have many commonalities: "image object, coordinate position, running speed, width and height". In order to facilitate the program development, we need to design a GameObject class, which can be used as the parent class of all game objects, so it is convenient for us to program.

GameObject class:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

/**
 * Parent class of game object
 * @author Zhao Guang Lu
 *
 */
public class GameObject {
	  Image  img;
	  double  x,y;
	   int   speed;
	  int  width, height;
	
	public  void  drawSelf(Graphics  g){
		g.drawImage(img, (int)x,(int) y, null);
	}

	public GameObject(Image img, double x, double y, int speed, int width, int height) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.width = width;
		this.height = height;
	}

	public GameObject(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
	public GameObject() {
	}
	
	/**
	 * Returns the rectangle of the object. Convenient for subsequent collision detection
	 * @return
	 */
	public  Rectangle   getRect(){
		return  new Rectangle((int)x, (int)y, width, height);
	}
	
	
}

Design aircraft

With GameObject as the parent class, we can design aircraft classes very simply. At present, aircraft classes have no particularly complex requirements. We just need simple inheritance to use:

Class Plane:

import java.awt.Graphics;
import java.awt.Image;

public class Plane  extends GameObject {
	
	public  void  drawSelf(Graphics  g){
		g.drawImage(img, (int)x,(int) y, null);
		x++;
	}
	
	public  Plane(Image  img, double x, double y){
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
}

Through inheritance, we found that the implementation of new classes, a lot of cool!

Adjustment of the calling mode of MyGameFrame class

After we encapsulate the Plane class, we don't need to add so many attributes of the aircraft in the MyGameFrame class. We encapsulate all the attributes in the Plane class, so the call becomes easier.

MyGameFrame class after encapsulation:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

/**
 * Main window of airplane game
 * @author Gao Qi
 *
 */
public class MyGameFrame  extends  JFrame {
	
	Image   planeImg  = GameUtil.getImage("images/plane.png");
	Image   bg  = GameUtil.getImage("images/bg.jpg");
	
	Plane   plane = new Plane(planeImg,250,250);
	
	@Override
	public void paint(Graphics g) {		//Automatically called. g is equivalent to a brush
		
		g.drawImage(bg, 0, 0, null);
		
		plane.drawSelf(g);  //Draw a plane
		
	}
	
	
	//Help us redraw the window again and again!
	class  PaintThread  extends  Thread  {
		@Override
		public void run() {
			while(true){
				repaint();		//Redraw
				
				try {
					Thread.sleep(40);   	//1s=1000ms
				} catch (InterruptedException e) {
					e.printStackTrace();
				}		
			}
		}
		
	}
	
	//Defining internal classes for keyboard listening
	class   KeyMonitor extends  KeyAdapter  {

		@Override
		public void keyPressed(KeyEvent e) {
			System.out.println("Press:"+e.getKeyCode());
		}

		@Override
		public void keyReleased(KeyEvent e) {
			System.out.println("Lift up:"+e.getKeyCode());
		}
		
		
	}
	
	
	/**
	 * Initialize window
	 */
	public  void  launchFrame(){
		this.setTitle("Shangxuetang students_Works of procedural apes");
		this.setVisible(true);
		this.setSize(500, 500);
		this.setLocation(300, 300);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		new PaintThread().start();	//Start redraw window thread
		addKeyListener(new KeyMonitor());   //Add keyboard monitor to window
	}
	
	public static void main(String[] args) {
		MyGameFrame  f = new MyGameFrame();
		f.launchFrame();
	}
	
}

After the object-oriented encapsulation, if we want to create more than one aircraft, it is also very simple.

Create multiple aircraft:

public class MyGameFrame extends JFrame {
    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 is used to draw the whole window and internal contents. Called automatically 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 is the same as the previous version. In order to save space, it is highlighted and not attached.
}

Operation effect:

 

 

 

Published 90 original articles, won praise 6, visited 10000+
Private letter follow

Tags: Java REST

Posted on Sun, 02 Feb 2020 14:01:12 -0500 by Saviola