First, the game: aircraft fired bullets
Second, the implementation of the principle: Rewrite the Paintcomponet method, according to a certain time interval, so that the coordinates of the bullets have been reduced to the y-axis, so that this realization of the bullet movement effect, the beginning of the bullet is the coordinates of the plane. Third, the code: on the first two basis, the code for some simple optimization, as follows:
package Com.jack;import Java.awt.graphics;import Java.awt.image;import Java.awt.image.imageobserver;import Javax.swing.jpanel;import com.jack.imp.iprocess;/** * Game Background * * @author laughing * @date November 20, 2014 PM 8:12:25 */public CL BackGround implements Iprocess {private static final image[]bg_images= imagemanager.getinstance (). Getimagesbytype ( "Background");p rivate static final intspeed= 20;private inttopx= 0;private inttopy= -480;private intbelowx= 0;private int belowy= 0; @Overridepublic void drawmyself (Graphics g, JPanel p) {g.drawimage (bg_images[1], TopX, Topy, (ImageObserver) p) ; G.drawimage (Bg_images[0], belowx, Belowy, (ImageObserver) p);} @Overridepublic void Move () {if (topy >= 0) {topy = -480;} else {topy + = speed;} if (belowy >= 480) {belowy = 0;} else {belowy + = speed;}} @Overridepublic Boolean Collison () {//TODO auto-generated method Stubreturn false;}}
Package Com.jack;import Java.awt.graphics;import Java.awt.image;import java.awt.image.imageobserver;import Javax.swing.jpanel;import com.jack.imp.iprocess;/** * Game bullets * * @author laughing * @date November 20, 2014 morning 10:27:12 */public C Lass Bullet implements iprocess {public static final image[]bullet_images= imagemanager.getinstance (). Getimagesbytype ( "Bullet");p ublic static final intfly_speed= 20;private booleandead= false;public intx;public inty;public Bullet (int x, in T y) {this.x = X-50;this.y = y;} public Boolean outofbounds () {if (This.y = = 0) {This.dead = True;return false;} return true;} /** * @return the y */public int GetY () {return y;} /** * @return The x */public int GetX () {return x;} /* * (NON-JAVADOC) * * @see com.jack.imp.iprocess#drawmyself (java.awt.Graphics, * javax.swing.JPanel) * * @Overridepublic void Drawmyself (Graphics g, JPanel p) {//TODO auto-generated method Stubg.drawimage (bullet_images[0],x,y-bullet_images [0].getheight (NULL), (ImageObserver) p);} /* * (NON-JAVADOC) ** @see com.jack.imp.iprocess#move () */@Overridepublic void Move () {if (Outofbounds ()) this.y-= fly_speed;} /* * (NON-JAVADOC) * * @see Com.jack.imp.iprocess#collison () */@Overridepublic Boolean collison () {//TODO auto-generated Method Stubreturn false;} /* * (NON-JAVADOC) * * @see java.lang.object#tostring () */@Overridepublic String toString () {return "Bullet [dead=" + DEA D + ", x=" + x + ", y=" + y + "]";} /** * @return The Dead */public boolean isdead () {return dead;}}
Package Com.jack;import Java.awt.graphics;import Java.awt.image;import java.awt.event.keyevent;import Java.awt.event.keylistener;import Java.awt.image.imageobserver;import Java.util.hashmap;import Java.util.Map; Import Javax.swing.jpanel;import com.jack.imp.iprocess;/** * Fighter * * @author laughing * @date November 20, 2014 afternoon 2:16:21 */pub Lic class Plan implements Iprocess, KeyListener {private static final image[]plan_images= imagemanager.getinstance (). Getimagesbytype ("plan");p ublic static Map<integer, bullet>bulletmap= new Hashmap<integer, bullet> (); private static final intplan_move_speed= 10;private static intbulletid= 0;public static intimageindex= 0;private Intx;pri Vate inty;public Plan (int x, int y) {this.x = X;this.y = y;} public void Shut () {Bullet b = new Bullet (x, y); Bulletmap.put (++bulletid, b); System.out.println ("crate" + Bulletmap);} @Overridepublic void Drawmyself (Graphics g, JPanel p) {g.drawimage (Plan_images[imageindex], X, Y, (ImageObserver) p);} public void UpdateplAn () {if (ImageIndex = = 5) ImageIndex = 0;imageindex++;} @Overridepublic void Move () {} @Overridepublic Boolean Collison () {//TODO auto-generated method Stubreturn false;} @Overridepublic void keytyped (KeyEvent e) {//TODO auto-generated method stub} @Overridepublic void Keypressed (KeyEvent e) {int key = E.getkeycode (), if (key = = keyevent.vk_up) {if (y-plan_move_speed >= 0) {y-= plan_move_speed;}} if (key = = Keyevent.vk_down) {if (y + plan_move_speed <= gamepanel.height-plan_images[0].getheight (NULL) * 2) {y + = P Lan_move_speed;}} if (key = = Keyevent.vk_left) {if (x-plan_move_speed >= 0) x-= Plan_move_speed;} if (key = = Keyevent.vk_right) {if (x + plan_move_speed < Gamepanel.weight-plan_images[0].getwidth (null)) x + = Plan_mov E_speed;} if (key = = Keyevent.vk_space) {shut ();}} @Overridepublic void keyreleased (KeyEvent e) {//TODO auto-generated method stub}}
Package Com.jack;import Java.awt.color;import Java.awt.graphics;import javax.swing.jpanel;import javax.swing.border.softbevelborder;/** * Game Container * * @author laughing * @date November 16, 2014 PM 7:58:11 */public Class Gamepanel Extends JPanel implements Runnable {public static final intheight= 480;public static final intweight= 320;public static P lanplan= new Plan;p ublic static backgroundground= new BackGround ();p ublic Gamepanel () {setSize (WEIGHT, HEIGHT) ; SetBorder (New Softbevelborder (1, Color.White, color.white));//sets the focusable state of this Component to the Specifie D value.//This value overrides the Component ' s default focusability.setfocusable (TRUE);//Listener Event Addkeylistener (plan); new Thread (This). Start ();} /* * (NON-JAVADOC) * * @see javax.swing.jcomponent#paintcomponent (java.awt.Graphics) */@Overrideprotected void Paintcomponent (Graphics g) {//TODO auto-generated method Stubsuper.paintcomponent (g); Ground.drawmyself (g, this); Plan.drawmyself (g, this); for (int Key:plan.bulleTmap.keyset ()) {Bullet b = plan.bulletMap.get (key), if (!b.isdead ()) {b.drawmyself (g, this);}}} /* * (NON-JAVADOC) * * @see java.lang.runnable#run () */@Overridepublic void Run () {//TODO auto-generated method Stubwhil E (True) {Plan.updateplan (); Ground.move (); for (int key:plan.bulletMap.keySet ()) {Bullet b = plan.bulletMap.get (key); if (!b.isdead ()) {B.move ();}} Repaint (); try {thread.sleep ($);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ( );}}}}
Package Com.jack;import javax.swing.jframe;/** * * @author laughing * @date November 16, 2014 afternoon 8:11:39 */public class Main Exte NDS JFrame {gamepanelpanel= new Gamepanel ();p ublic Main () {setSize (Panel.getwidth (), Panel.getheight ()); Getcontentpane (). Add (Panel); setvisible (true); setresizable (false); Setdefaultcloseoperation (jframe.exit_on_close ); Setlocationrelativeto (null);} public static void Main (String args[]) {new Main ()}}
Iv. Source code: Click to open the link
Swing animated Bullets