坦克大战为什么药两个repaint
小弟我最近学到韩顺平视频线程这,有一个疑问请各位豪侠:MyPanel的事件处理KeyListener中已有repaint函数,为什么还要把MyPanel做成线程,在run中再写一个repaint函数,才能把子弹重绘?
package game.tank;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public class DrawTank extends JFrame
{
Mypanel mypanel ;
public DrawTank()
{
mypanel = new Mypanel();
mypanel.setBackground(Color.black);
Thread t = new Thread(mypanel);
t.start();
this.add(mypanel);
this.addKeyListener(mypanel);
this.setSize(500, 400);
this.setLocation(500, 350);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new DrawTank();
}
}
// 定义我的面板
class Mypanel extends JPanel implements KeyListener, Runnable
{
Mytank mytank;
// 定义敌人的tank数组
Vector<Enemytank> enemytank = new Vector<Enemytank>();
// 定义敌人tank出现的数量
int etSize = 3;
public Mypanel()
{
mytank = new Mytank(200, 340);
mytank.setColor(0);
for (int i = 0; i < etSize; i++)
{
Enemytank et = new Enemytank((i + 1) * 110, 0);
et.setColor(1);
et.setDirect(2);
enemytank.add(et);
}
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.cyan);
this.draw(mytank.getX(), mytank.getY(), g, this.mytank.direct, 0);
// 画出子弹
if (mytank.b != null)
{
g.draw3DRect(mytank.b.x, mytank.b.y, 1, 1, false);
}
// 画出敌人的坦克
for (int i = 0; i < enemytank.size(); i++)
{
this.draw(enemytank.get(i).getX(), enemytank.get(i).getY(), g,
enemytank.get(i).getDirect(), 1);
}
}
public void draw(int x, int y, Graphics g, int direct, int type)
{
switch (type)
{
case 0:
g.setColor(Color.cyan);
break;
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 + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y);
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 + 4, 10, 10);
g.drawLine(x + 15, y + 10, x + 30, 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 + 4, y + 10, 10, 10);