import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Rei52 extends JApplet implements Runnable, ActionListener{ // アプレットにするためにはJAppletを継承する

       private int x=100, y=0;

       private volatile Thread th=null;

      

       class Rei52Panel extends JPanel{

              public void paintComponent(Graphics g){

                     super.paintComponent(g);

                     Font f=new Font((g.getFont()).getName(), Font.BOLD,40);

                     g.setFont(f);

                     g.drawString("",x,y);

              }

       }

      

       public void init(){       // アプレットではコンストラクタの代わりにinitメソッドで初期化を行う

// initメソッドはアプレットがhtmlファイルから呼び出された時に、最初に実行される

              setSize(300,250);

              Container c=getContentPane();

              c.add(new Rei52Panel(), "Center");

              JButton bt1 = new JButton("開始/停止");

              c.add(bt1,"North");

              bt1.addActionListener(this);

       }

      

       public void actionPerformed(ActionEvent e){

              if(th==null){

                     th=new Thread(this);

                     th.start();

              }

              else

                     th=null;

       }

      

       public void run(){

              Thread thisThread = Thread.currentThread();

              while(th==thisThread){

                     repaint();

                     try{

                            th.sleep(300);

                     }

                     catch(InterruptedException e){}

                     x=x-10 ; y=y+10;

                     if(x<0){

                            x=(int) (Math.random()*getWidth() );

                            y=0;

                     }

              }

       }

}