package Appletexam;

import java.applet.Applet;
import java.awt.*;
import java.net.*;

/**
 * <p>Title: VisioCom.jar's Usage  Example--Applet</p>
 * <p>Description: Show how to use VisioCom.jar in an Applet</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: Beijing Dawninfo Tech. Co., Ltd.</p>
 * @author Junjie Ruan
 * @version 1.0
 */

public class VisioApplet extends Applet implements  Runnable  {
  public Thread thrd;
  PaintPanel panel = null;
  private Label label;
  public URL appletUrl;

  public VisioApplet() throws HeadlessException {
  }

  public void init()
  {
    super.init();

    appletUrl = this.getCodeBase();

    String str = "Reading the files of Visio documents on the server, Please wait...";
    label = new Label(str);
    Font font = new Font("Default", Font.PLAIN, 20);
    label.setFont(font);
    add("Center",label);

    //create a new Thread
    thrd=new Thread(this,"CONTENT");
    thrd.start();
  }

  public void run()
  {
    try
    {
      //!!add an instance of Panel which implements GdiFrame in the window of applet
      panel = new PaintPanel(this);
      Rectangle rec = this.getBounds();
      panel.setBounds(rec);
      panel.setVisible(false);

      remove(label);
      this.add("Center",panel);
      panel.setVisible(true);
      panel.requestFocus();

    } catch(Exception e) {
      this.stop();
      return;
    }

    while(true) {
      try {
        thrd.sleep(1000);
      } catch(InterruptedException e) {
        System.err.println("Thread Error!");
      }
    }

  }

  public void stop()
  {
    panel.removeAll();
    this.removeAll();
    panel = null;
    thrd.interrupt();
    thrd = null;
    this.destroy();
  }

  public void showReadingProcess(int curBytes,int totBytes,int lastBytes,boolean ifFirstTime)
  {
      boolean ifDrawed;
      float percent = 0.0f;
      Graphics g = this.getGraphics();
      Rectangle appletRec = this.getBounds();
      Rectangle rec = new Rectangle(appletRec.x+30,appletRec.y+appletRec.height/2-30,
                                    appletRec.width-60,60);
      if (ifFirstTime)
      {
        g.setColor(Color.white);
        g.fillRect(rec.x,rec.y,rec.width,rec.height);
        g.setColor(Color.pink);
        g.drawRect(rec.x,rec.y,rec.width,rec.height);
        g.setColor(Color.blue);
        g.drawString("percentage Completion",rec.x+10,rec.y+20);
        return;
      }
      if (totBytes > 0)
        percent = (float)curBytes/(float)totBytes;
      g.setColor(Color.blue);
      g.fillRect(rec.x,rec.y+39,(int)(percent*rec.width),20);
  }

}