帮我看看这段代码,运行的时候为什么cpu占用率达到了100%哪里出问题了吗,里面的其他一些类不用管应该就是这段代码出问题了
package Exercise;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Exercise extends JFrame
{
public Exercise()
{
super("这是一次测试");
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel myPanel=new panel();
addKeyListener(myPanel);
this.add(myPanel);
}
public static void main(String[] args)
{
Exercise e=new Exercise();
e.setVisible(true);
}
}
class panel extends JPanel implements KeyListener
{
myTank mt=null;
public panel()
{
mt=new myTank(100,100);
}
public void paint(Graphics g)
{
super.paint(g);
this.drawTank(mt.getX(),mt.getY(),g,mt.getType(),0);
if(mt.s!=null)
{
g.draw3DRect(mt.s.getX(), mt.s.getY(), 2, 2, false);
}
}
public void drawTank(int x,int y,Graphics g,int type,int color)//画坦克(根据坦克的类型画出各种坦克)
{
switch(color)
{
case 0:
g.setColor(Color.red);
break;
case 1:
g.setColor(Color.CYAN);
break;
}
//用switch语句画出
switch(type)
{
case 0:
//向上
g.fill3DRect(x-10, y-15, 5, 30, false);
g.fill3DRect(x-5, y-10, 10, 20, false);
g.fill3DRect(x+5, y-15, 5, 30, false);
g.drawLine(x, y, x, y-15);
break;
case 1:
//向右
g.fill3DRect(x-15, y-10, 30, 5, false);
g.fill3DRect(x-10, y-5, 20, 10, false);
g.fill3DRect(x-15, y+5, 30, 5, false);
g.drawLine(x, y, x+15, y);
break;
case 2:
//向下
g.fill3DRect(x-10, y-15, 5, 30, false);
g.fill3DRect(x-5, y-10, 10, 20, false);
g.fill3DRect(x+5, y-15, 5, 30, false);
g.drawLine(x, y, x, y+15);
break;
case 3:
//向左
g.fill3DRect(x-15, y-10, 30, 5, false);
g.fill3DRect(x-10, y-5, 20, 10, false);
g.fill3DRect(x-15, y+5, 30, 5, false);
g.drawLine(x, y, x-15, y);
break;
}
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W)
{
mt.setType(0);
mt.moveup();
}else
if(e.getKeyCode()==KeyEvent.VK_D)
{