不需要看代码做什么买就是看看那个函数是什么时候,如何运行调用的。。。下面已标出importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;classMyTankGameextendsJFrame{MyP...
不需要看代码做什么买就是看看那个函数是什么时候,如何运行调用的。。。下面已标出
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MyTankGame extends JFrame
{
MyPanel mp=null;
public static void main(String[] args)
{
MyTankGame mgt= new MyTankGame();
}
public MyTankGame()
{
mp=new MyPanel();
this.add(mp);
this.setSize(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Tank
{
int x=0; //坦克的横坐标
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(100,100);
}
public void paint(Graphics g)————————————、、这个函数什么时候运行的?没有调用啊?
{
super.paint(g);
g.fillRect(0,0,400,300);
this.drawTank(hero.getX(),hero.getY(),g,0,0);
}
public void drawTank(int x,int y,Graphics g,int direct,int type)
{
switch (type)
{
case 0: g.setColor(Color.red); break;
case 1: g.setColor(Color.cyan);break;
case 2: g.setColor(Color.green);break;
}
switch (direct)
{
case 0:
g.fill3DRect(x,y,7,30,false);
g.fill3DRect(x+20,y,7,30,false);
g.fill3DRect(x+7,y+5,13,20,false);
g.fillOval(x+8,y+9,10,12);
g.drawLine(x+13,y+9,x+13,y-2);
break;
}
}
}
为什么要加一句 super.paint(g); ???
展开