编写一个Applet,实现一个字符串沿正弦曲线移动

public class SinxDemo extends Applet{

    int x,y;
    int h;
    Dimension d;    //Dimension 类封装单个对象中组件的宽度和高度(精确到整数)。
    public void init() {
        d=getSize();   //获取此 Dimension 对象的大小。
        h=d.height/2;
        x=20;
    }
    
    public void paint(Graphics g) {
        if(x<d.width) {
            y=(int) (1.0+Math.sin((x)*0.05)*h);
            //y=(int) (Math.sin(x*0.01)*h);
            x++;
        }
        else 
            {  x=20;  }   //当x大于d.width,x重新复制为20
        g.drawString("你好", x, y);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            showStatus(e.toString());  //请求将参数字符串显示在“状态窗口”中。
        }
        repaint();
    }
}
 


运行结果:

猜你喜欢

转载自blog.csdn.net/xxx_1_1/article/details/82497990
今日推荐