Motivasi Hidup

Hanya orang yang berani gagal total,akan meraih keberhasilan total (Jhon F.Kennedy)

Kamis, 08 Desember 2016

Animasi Bandul

Membuat Animasi Bandul...

codingnya.....

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication7;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
/**
 *
 * @author Elpido SC
 */
public class bandul extends Applet implements Runnable  {
    double x,y,w,z,waktu =2,waktu1 =3.28;int terang;
    public void init()
    {
        this.setSize (600,600);
            }
    public void start()
    {
        Thread trit= new Thread (this);
        trit.start();
    }
    public void run ()
    {
        while(true)
        {
            while (waktu<=3)
            {
                waktu+=0.02;
                hitung();
                repaint ();
               
                try  {
                    Thread.sleep(10);
                } catch (Exception e) {
                    System.out.print(e);
                }
            }
            while (waktu1<=4.28)
            {
                waktu1+=0.02;
                hitung();
               
                repaint();
                try {
                    Thread.sleep(10);
                } catch (Exception e){
                    System.out.print(e);
                }
            }
             while (waktu1>=3.28)
            {
                waktu1-=0.02;
                hitung();
               
                repaint();
                try {
                    Thread.sleep(50);
                } catch (Exception e){
                    System.out.print(e);
                }
            }
             while (waktu>=2)
            {
                waktu-=0.02;
                hitung();
               
                repaint();
                try {
                    Thread.sleep(50);
                } catch (Exception e){
                    System.out.print(e);
                }
            }
        }
    }
    public void hitung()
    {
        y=Math.cos(waktu) * 250;
        x= Math.sin(waktu) * 250;
        w=Math.cos(waktu1) * 250;
        z= Math.sin(waktu1) * 250;
    }
    public void paint (Graphics g)
    {
        hitung();
        g.setColor (Color.red);
        g.fillRect(50, 120, 450,30);
        g.drawLine(270,150,290-(int)x,150-(int)y);
        g.drawLine(270,150,255-(int)z,150-(int)w);
        g.setColor(Color.GREEN);
       g.fillOval(260-(int)x, 150-(int)y,50, 50);
       g.setColor(Color.YELLOW);
       g.fillOval(240-(int)z, 150-(int)w, 50, 50);                
       
    }
   

}


maka output yang dihasilkan dari coding diatas sperti gambar dibawah ini




Tidak ada komentar:

Posting Komentar