Aircraft war - java basic applet (Beginner project) 05

Shoot ing game day 5:

1. Add an access control modifier to the members in the class

2. Design Images Image Tools

Knowledge points:

1.package:

1) Function: avoid class name conflict

2) Full name of class: package name The class name cannot conflict with the class name in the package

3) Package names often have a hierarchy. 4) suggestion: all letters of the package name are lowercase

import:

1) Classes in the same package can be accessed directly. Classes in different packages cannot be accessed directly. If you want to access:

1.1) import declaration class first and then access class ----- suggestion

1.2) full name of class -------------------------- too cumbersome, not recommended

2. Access control modifier: ------------ protect data security

1)public: public, any class

2)private: private, this class

3)protected: protected, including this class, derived class and the same package class

4) Default: write nothing. Default permissions are not recommended for this class and the same package class ----------- java

Note: 1) the access permission of class can only be public or default

2) the access rights of the members in the class can be the above four

3.final: final and unchangeable ---- the application rate alone is very low

1) Modify variables: variables cannot be changed

2) Modifier method: method cannot be overridden

3) Decorated class: class cannot be inherited

4.static: static

1) Static variables:

1.1) modified by static

1.2) it belongs to class and is stored in the method area. There is only one copy

1.3) it is often accessed through class name points

When to share all audio data (1.4)

2) Static method:

2.1) modified by static

2.2) it belongs to class and is stored in the method area. There is only one copy

2.3) it is often accessed through class name points

2.4) there is no implicit this transfer in the static method, so the instance members cannot be accessed directly

2.5) when to use: the operation of the method has nothing to do with the object

3) Static block:

3.1) modified by static

3.2) it belongs to a class and is automatically executed during the loading of the class. Because the class is loaded only once, the static block is also executed only once

3.3) when to use: load / initialize static resources (pictures, audio, video, etc.)

 

//Explain that it's impossible for us to finish eating chicken, so everyone gives us a picture, so the server will explode long ago, and everyone in this range can only use one picture

Now let's create an image class

 

 

 

 

 

 

 

 

 

 

 

 

code:

package cn;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.image.BufferedImage;

public class World extends JPanel {

	private Hero hero = new Hero();
	private Sky sky = new Sky();
	private FlyingObject[] enemies;// 0

	private Bullet[] bullet = {};

	public void action() {
		BufferedImage img = Images.sky;
		enemies = new FlyingObject[5];
		enemies[0] = new Airplane();
		enemies[1] = new Airplane();
		enemies[2] = new BigAirplane();
		enemies[3] = new BigAirplane();
		enemies[4] = new Bee();
		for (int x = 0; x < enemies.length; x++) {
			FlyingObject f = enemies[x];
			System.out.println(f.x + "," + f.y);
			f.say();
		}
	}


	public static void main(String[] args) {

		JFrame frame = new JFrame();
		World world = new World();
		frame.add(world);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 700);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		world.action();

	}

}
package cn;

import java.util.Random;


public class Airplane extends FlyingObject {
	
	private  int speed;//Moving speed
	public Airplane(){
		super(48,50);
		
		Random rand=new Random();
		x= rand.nextInt(400-width);
		y=-height;
		speed=2;
	}
	public void say(){
		System.out.println("I would like to be your guardian angel");
	}
	
	

}

package cn;

import java.util.Random;



public class Bee extends FlyingObject{
	
	private int xspeed;//x movement speed
	private int yspeed;//y movement speed
	private int awardType;//Reward type
	public Bee(){
		super(20,50);
		
		Random rand=new Random();
		x= rand.nextInt(400-width);
		y=-height;
		xspeed=1;
		yspeed=2;
		awardType=rand.nextInt(8);
	}
	public void say(){
		System.out.println("I would like to be your guardian angel");
	}
	
	}
package cn;
import java.util.Random;



public class BigAirplane extends FlyingObject{
	
	private  int speed;//Moving speed
	public BigAirplane(){
		super(66,89);
	
		Random rand=new Random();
		x= rand.nextInt(400-width);
		y=-height;
		speed=2;
	}
	
	public void say(){
		System.out.println("I would like to be your guardian angel");
	}

}
package cn;



public class Bullet  extends FlyingObject{
	//bullet

	private int speed;//Moving speed
	public Bullet(int x,int y){
		super(8,20,x,y);
		
		
		speed=3;
		
	}
	public void say(){
		System.out.println("I would like to be your guardian angel");
	}
	

}
package cn;

import java.util.Random;

public class FlyingObject {
	protected int width;
	protected int height;
	protected int x;
	protected int y;

	public FlyingObject(int width, int height) {// It is specially provided for little bee, big enemy aircraft and small enemy aircraft. I think the x and y of the three aircraft are different, so I have to write live
		this.width = width;
		this.height = height;
		Random rand = new Random();
		x = rand.nextInt(400 - width);
		y = -height;
	}

	public FlyingObject(int width, int height, int x, int y) {// It is specially provided for hero aircraft and sky bullets. I think the x and y of the three aircraft are different, so I have to write live
		this.width = width;
		this.height = height;
		this.x = x;
		this.y = y;
	}

	public void say() {
		System.out.println("The flying object moved");
	}
}
package cn;


public class Hero  extends FlyingObject{
	private int life;
	private int fire;
	public Hero(){
		super(140,100,140,100);

	
		life=3;
		fire=0;
	}
	public void say(){
		System.out.println("I would like to be your guardian angel");
	}
}
package cn;


import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;


public class Images {
	public static BufferedImage sky;
	public static BufferedImage bullet;
	public static BufferedImage[] heros;
	public static BufferedImage[] airo;
	public static BufferedImage[] airs;
	public static BufferedImage[] bairs;
	public static BufferedImage[] bees;

	static {// Initialize static resources
		sky =readImage("background.png");
		bullet=readImage("bullet.png");
		heros=new BufferedImage[2];
		heros[0]=readImage("hero0.png");
		heros[0]=readImage("hero1.png");
		airs=new BufferedImage[5];
		bairs=new BufferedImage[5];
		bees=new BufferedImage[5];
		airs[0]=readImage("airplane.png");
		bairs[0]=readImage("bigairplane.png");
		bees[0]=readImage("bee.png");
	for(int i=1;i<airs.length;i++){	
		airs[i]=readImage("bom"+i+".png");
		bairs[i]=readImage("bom"+i+".png");
		bees[i]=readImage("bom"+i+".png");
	}
	
	}
	

	public static BufferedImage readImage(String fileName) {
		try {
			BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName));//Read from the same package
			return img;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
}


package cn;


public class Sky extends FlyingObject{
	
	private int speed;//Moving speed
	private int y1;//y coordinate of the second figure
	/**Construction method*/
	public Sky(){
		super(400,700,0,0);
		
		speed=1;
		y1=-700;
		
	}
	public void say(){
		System.out.println("I would like to be your guardian angel");
	}
	
	

}

(write the old rule five times. Don't study today and become waste tomorrow)

Tags: Java

Posted by 182x on Sat, 21 May 2022 17:45:51 +0300