java小程序paint方法麻烦 [英] java applet paint method trouble

查看:31
本文介绍了java小程序paint方法麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中,我从 init() 方法调用 repaint() 方法,但输出与我预期的不一样.我调用了 repaint() 方法 10 次,但它只调用了一次 paint()(参见 O/P 的屏幕截图).我是不是犯了什么错误.请帮我.谢谢

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

代码

import java.awt.*;
import java.applet.Applet;
/*
    <applet code="test" height=300 width=300>
    </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);
    }   
}

推荐答案

请理解您无法完全控制是否或何时调用paint(...),以及repaint() 调用只是建议 JVM 进行绘制.如果有太多的 repaint() 请求进来并且它们像您一样堆叠起来,那么它们将被合并.有关这方面的更多详细信息,请查看这篇文章,Painting in AWT and Swing.

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.

无论如何,即使您的代码有效,它仍然不会对您的代码用户造成任何可见的更改,因为所有 x 和 y 更改以及绘制几乎是立即完成的.也许您真的想使用摇摆计时器?也许您想在 Swing 中重新编码而不是在 AWT 中.

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.

这篇关于java小程序paint方法麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆