java paint方法哪个周期调用_新手求教,java 这个函数public void paint( Graphics g) 是什么时候调用的?...

不需要看代码做什么买就是看看那个函数是什么时候,如何运行调用的。。。下面已标出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); ???

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值