Draw Tank __ Tank movable

Source: Internet
Author: User

One, the code is as follows
Package Www.tainiu.gui__V2;import Java.awt.color;import Java.awt.graphics;import java.awt.event.keyevent;import Java.awt.event.keylistener;import Javax.swing.jframe;import Javax.swing.jpanel;public class af__TankGame__V2 Extends Jframe{mypanel_v2 mp= null;public static void Main (string[] args) {//TODO auto-generated method Stubaf__tankgame_ _V2 tv= new Af__tankgame__v2 ();} Public Af__tankgame__v2 () {//TODO auto-generated constructor stubmp = new Mypanel_v2 (); This.add (MP); This.setsize (400, This.setdefaultcloseoperation (Jframe.exit_on_close); this.setvisible (true);//Binding monitoring This.addkeylistener (MP);}} Class Tank_v2 {int x = 0;int y = 0;public tank_v2 (int x, int y) {//TODO auto-generated constructor stubthis.x = X;this.y = y;}} Class Hero_v2 extends Tank_v2 {public hero_v2 (int x, int y) {//TODO auto-generated constructor Stubsuper (x, y);}} Class Mypanel_v2 extends JPanel implements KeyListener {hero_v2 Hero = null;public mypanel_v2 () {//TODO auto-generated Co Nstructor Stubhero = new Hero_v2 (10, 10);} @Overridepublic void Paint (Graphics g) {//TODO auto-generated method Stubsuper.paint (g);//g.setcolor (color.yellow);// G.drawrect (hero.x, HERO.Y, 5, 0),//g.setcolor (Color.cyan), G.fillrect (0, +), G.setcolor (Color.cyan);// G.fill3drect (hero.x, HERO.Y, 5, false)//g.fill3drect (hero.x+15, HERO.Y, 5, +, false);//g.fill3drect (Hero.x+5, Hero.y+15,//g.filloval (hero.x+4, hero.y+20, Ten),//g.drawline (hero.x+10, hero.y+20, hero.x+10, 60); /Draw your own tank This.drawtank (hero.x, Hero.y, G, 1, 0);} private void Drawtank (int x, int y, Graphics g, int derict, int type) {//TODO auto-generated Method Stubswitch (type) {CA        Se 0://own tank g.setcolor (Color.cyan); Case 1://enemy Tank G.setcolor (color.yellow); break;default:break;}  Switch (derict) {case 1://direction to G.fill3drect (x, Y, 5, 5, false) G.fill3drect (x+15, y, N/A, ()); G.fill3drect (x+5, y+15, G.filloval (X+4, y+20, Ten), G.drawline (x+10, y+20, x+10, y+60), break;case 2://direction to left G.fill3drect (x, y, 5, +, false); G.fill3drect (x+15, Y, 5,, false); G.fill3drect (x+5, y+15, ten, false); G.filloval (X+4, y+20, ten); g . DrawLine (X+10, y+20, x+10,) break;case 3://(x, Y, 5, +, False) G.fill3drect (G.fill3drect, Y, 5, F, false); Fill3drect (x+5, y+15, ten, false), G.filloval (X+4, y+20, Ten), G.drawline (x+10, y+20, x+10), break;case 4://direction think right g. Fill3drect (x, Y, 5, N, false); G.fill3drect (x+15, Y, 5, +, False) G.fill3drect (x+5, y+15, ten, false); G.filloval (X+4, Y+20, G.drawline (x+10, y+20, x+10,); break;}}  @Overridepublic void keytyped (KeyEvent e) {//TODO auto-generated method stub} @Overridepublic void Keypressed (KeyEvent e) {//TODO auto-generated method Stub//system.out.println ("pp"); if (e.getkeycode () = = keyevent.vk_a) {this.hero.x-=1;} else if (e.getkeycode () = = Keyevent.vk_d) {this.hero.x +=1;} else if (e.getkeycode () = = Keyevent.vk_w) {this.hero.y-=1;} else if (e.getkeycode () = = keyevent.vk_s) {this.hero.y +=1;} This.repaint ();} @Overridepublic void KeyrelEased (KeyEvent e) {//TODO auto-generated method stub}} 

Draw Tank __ Tank movable

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.