java applet paint,Java applet的paint方法麻烦

In code I am calling repaint() method from init() method but Output is not as per I expect.

I called repaint() method 10 times but It called paint() only once(See Screenshot of O/P).

Am I making any mistake. please help me.

Thanks

code

import java.awt.*;

import java.applet.Applet;

/*

*/

public class test extends Applet

{

int x,y;

public void init()

{

x=5;

y=10;

for(int i=1;i<10;i++)

{

System.out.println("From init "+i);

x+=(i*2);

y+=(i*3);

repaint();

}

}

public void paint(Graphics g)

{

System.out.println("Paint");

g.drawLine(50,50,x,y);

}

}

ik8mJ.png

解决方案

Please understand that you do not have complete control over if or when paint(...) get's called, and that repaint() calls are only a suggestion to the JVM to paint. If too many repaint() requests come in and they stack up as yours are doing, then they will be combined. For more details on this, please have a look at this article, Painting in AWT and Swing.

Regardless, even if your code worked, it still wouldn't cause any visible changes for the user of your code, since all the x and y changes and painting would be done almost instantaneously. Perhaps you really want to use a Swing Timer? Perhaps you want to re-code this in Swing and not AWT as well.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值