Tuesday, 17 September 2013

Pendulum Clock

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication16;
 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
 
/**
 *
 * @author gaurav
 */
public class timmer extends Applet implements Runnable,ActionListener {
 
    /**
     * Initialization method that will be called after the applet is loaded into
     * the browser.
     */
    int i=200;int j;
    int counter=0;
    double q;
    boolean b=true,a=true;
    Thread t;
    Button star,stop,resume,reset;
    public void init() {
        star=new Button("START");
        
        stop=new Button("stop");add(stop);
        stop.addActionListener(this);
        resume=new Button("resume");add(resume);
        resume.addActionListener(this);
        reset=new Button("reset");add(reset);
        reset.addActionListener(this);
        add(star);
        star.addActionListener(this);
        t=new Thread(this);
        
        // TODO start asynchronous download of heavy resources
    }public void run(){
        try
        {
            for(i=200;i<=300;i=i+10){
            repaint();
            counter++;
            
        Thread.sleep(300);}
            
            do{
            for(i=300;i>=100;i=i-10){
            repaint();
        Thread.sleep(300);
            counter++;}
        for(i=100;i<=300;i=i+10){
            repaint();
        Thread.sleep(300);}
        
         counter++;
        }while(b);}
        catch(InterruptedException e){System.out.println("gaurav");}
    
    
    
    
    
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==star){
            if(a!=false)
            t.start();
            else
            {t.resume();t.notify();}
        }
        else if(ae.getSource()==stop)
        {t.suspend();
        
        double p=(double)counter/11;
        q=p*3.92;
        JOptionPane.showMessageDialog(null,String.valueOf(q)+"sec");
        }
        else if(ae.getSource()==resume){t.resume();
        t.notify();}
        else if(ae.getSource()==reset){counter=0;i=200;j=300;t.suspend();
        a=false;
       }
    }
    // TODO overwrite start(), stop() and destroy() methods
    public void paint(Graphics g)
    {g.setColor(Color.ORANGE);
   // g.drawString(String.valueOf(q)+"sec",50,20);
        g.drawLine(100, 100, 300, 100);
        g.drawRect(75,100, 250,300);if(i<=300&&i>250){g.setColor(Color.blue);
    setBackground(Color.GRAY);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.red);
    
    g.fillOval(i-10,j-10,20,20);
    }else if(i<=250&&i>200)
    {g.setColor(Color.white);
    setBackground(Color.GRAY);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.black);
    
    g.fillOval(i-10,j-10,20,20);
    }
    else if(i>150&&i<=200){g.setColor(Color.yellow);
    setBackground(Color.white);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.blue);
    
    g.fillOval(i-10,j-10,20,20);
    }
    else{g.setColor(Color.red);
    setBackground(Color.black);
    int k=i-200;
    double l=Math.pow(k,2);
    double m=40000-l;
       double n=Math.sqrt(m);
          int  j=(int)n+100;
    
    g.drawLine(200,100,i,j);
    g.setColor(Color.yellow);
    
    g.fillOval(i-10,j-10,20,20);
    }
    }
}
 

No comments:

Post a Comment