ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
绘制3辆敌方坦克和1辆我方坦克 Test.java ~~~ package tkdz; import javax.swing.*; import java.awt.*; public class Test extends JFrame{ MyPanel mp =null; public static void main(String[] args){ Test t1 =new Test(); } public Test(){ mp =new MyPanel();//面板 this.add(mp); this.addKeyListener(mp);//添加键盘监听 this.setSize(400,300); this.setLocation(300, 280); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } ~~~ Tank.java ~~~ package tkdz; public class Tank { int x=0,y=0; int fangxiang=0;//方向 int sudu=1;//速度 public int getFangxiang() { return fangxiang; } public void setFangxiang(int fangxiang) { this.fangxiang = fangxiang; } public int getSudu() { return sudu; } public void setSudu(int sudu) { this.sudu = sudu; } 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; } public Tank(int x, int y) { super(); this.x = x; this.y = y; } } ~~~ MyTank.java ~~~ package tkdz; public class MyTank extends Tank{ public MyTank(int x, int y) { super(x, y); } public void xiangshang() { y-=sudu; } public void xiangzuo() { x-=sudu; } public void xiangxia() { y+=sudu; } public void xiangyou() { x+=sudu; } } ~~~ DiTank.java ~~~ package tkdz; public class DiTank extends Tank{ public DiTank(int x, int y) { super(x, y); } } ~~~ MyPanel.java ~~~ package tkdz; import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.*; public class MyPanel extends JPanel implements KeyListener{ MyTank mt=null; Vector<DiTank> dtk =new Vector<DiTank>();//敌坦克集合,多线程用Vector int tksl =3;//敌人坦克的数量 public MyPanel(){ mt=new MyTank(140,232);//我的坦克,1辆 for(int i=0;i<tksl;i++){//敌坦克数量,3辆 DiTank dt=new DiTank((i)*181+5,0); dtk.add(dt); } } public void paint(Graphics g){ super.paint(g); g.fillRect(0, 0, 400, 300);//黑色400*300画布 this.drawTank(mt.getX(), mt.getY(), g, 0, 0);//绘制我方坦克 for(int i = 0; i < dtk.size(); i++){//绘制敌方坦克 this.drawTank(dtk.get(i).getX(), dtk.get(i).getY(), g, 2, 1); } } public void drawTank(int x,int y,Graphics g,int fangxiang,int leixing) { switch(leixing) { case 0://我的坦克 g.setColor(Color.yellow); break; case 1://敌人的坦克 g.setColor(Color.green); break; } switch(fangxiang) { 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+5, y+10, 10, 10); g.drawLine(x+10, y+15, x+10, y-3); break; case 1://左 g.fill3DRect(x, y, 30, 5,false); g.fill3DRect(x, y+15, 30, 5, false); g.fill3DRect(x+5, y+5, 20, 10, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+10, x-3, y+10); break; case 2://下 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+5, y+10, 10, 10); g.drawLine(x+10, y+15, x+10, y+33); break; case 3://右 g.fill3DRect(x, y, 30, 5,false); g.fill3DRect(x, y+15, 30, 5, false); g.fill3DRect(x+5, y+5, 20, 10, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+10, x+33, y+10); break; } this.repaint(); } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W) { this.mt.setFangxiang(0);//方向为0 this.mt.xiangshang();//启动函数向上 }else if(e.getKeyCode()==KeyEvent.VK_A) { this.mt.setFangxiang(1); this.mt.xiangzuo(); }else if(e.getKeyCode()==KeyEvent.VK_S) { this.mt.setFangxiang(2); this.mt.xiangxia(); }else if(e.getKeyCode()==KeyEvent.VK_D) { this.mt.setFangxiang(3); this.mt.xiangyou(); } this.repaint(); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } } ~~~ ![](https://box.kancloud.cn/00b081b8ea1cc593c89c748f4c13894c_395x297.png)