本帖最后由 曾欢欢 于 2014-5-7 09:11 编辑
package com.Tank;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyTank extends JFrame{
Mypanel mp = null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
new MyTank();
}
public MyTank() {
mp = new Mypanel();
this.add(mp);
this.setLocation(300, 100);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Tank{
int x = 0;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
int y = 0;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
}
class Hero extends Tank {
public Hero(int x, int y) {
super(x, y);
}
}
class Mypanel extends JPanel implements KeyListener{
Hero hero = null;
public Mypanel() {
hero = new Hero(10, 10);
}
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(hero.getX(), hero.getY(), g, 1, 0);
}
// 封装画坦克方法
public void drawTank(int x, int y, Graphics g, int type, int direct) {
switch (type) {
// 我的Tank
case 0:
g.setColor(Color.CYAN);
break;
// 敌人Tank
case 1:
g.setColor(Color.YELLOW);
break;
}
switch (direct) {
// 向上走
case 0:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x+5, y+5, 10, 20, false);
g.fillOval(x+4, y+10, 10, 10);
g.drawLine(x+10, y, x+10, y+10);
}
}
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
System.out.println("按了:"+e.getKeyChar());
}
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
System.out.println("按了");
}
public void keyReleased(KeyEvent e) {
// TODO 自动生成的方法存根
System.out.println("释放了");
}
}
|
|