畫坦克__坦克可移動

來源:互聯網
上載者:User

標籤:code   end   oval   released   lis   java   www.   val   etc   

一、代碼如下
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, 300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//綁定監聽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 constructor 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, 30);//g.setColor(Color.CYAN);g.fillRect(0, 0, 400, 300);g.setColor(Color.CYAN);//g.fill3DRect(hero.x, hero.y, 5, 30, false);//g.fill3DRect(hero.x+15, hero.y, 5, 30, false);//g.fill3DRect(hero.x+5, hero.y+15, 10, 20, false);//g.fillOval(hero.x+4, hero.y+20, 10, 10);//g.drawLine(hero.x+10, hero.y+20, hero.x+10, 60);//畫出自己的坦克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) {case 0://自己坦克g.setColor(Color.CYAN);break;        case 1://敵人坦克        g.setColor(Color.yellow);break;default:break;}switch (derict) {case 1://方向想下g.fill3DRect(x, y, 5, 30, false);g.fill3DRect(x+15, y, 5, 30, false);g.fill3DRect(x+5, y+15, 10, 20, false);g.fillOval(x+4, y+20, 10, 10);g.drawLine(x+10, y+20, x+10, y+60);break;case 2://方向想左g.fill3DRect(x, y, 5, 30, false);g.fill3DRect(x+15, y, 5, 30, false);g.fill3DRect(x+5, y+15, 10, 20, false);g.fillOval(x+4, y+20, 10, 10);g.drawLine(x+10, y+20, x+10, 60);break;case 3://方向想上g.fill3DRect(x, y, 5, 30, false);g.fill3DRect(x+15, y, 5, 30, false);g.fill3DRect(x+5, y+15, 10, 20, false);g.fillOval(x+4, y+20, 10, 10);g.drawLine(x+10, y+20, x+10, 60);break;case 4://方向想右g.fill3DRect(x, y, 5, 30, false);g.fill3DRect(x+15, y, 5, 30, false);g.fill3DRect(x+5, y+15, 10, 20, false);g.fillOval(x+4, y+20, 10, 10);g.drawLine(x+10, y+20, x+10, 60);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}}

 

畫坦克__坦克可移動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.