• info@dcc-dp.id
  • Alamat: Jl. Perintis Kemerdekaan III, BTN Antara Blok A11/02 Tamalanrea, Makassar, Indonesia

membuat bola bergerak dengan java applet

Admins 5 Comments Agustus 15, 2018

membuat bola bergerak dengan java applet

Assalamu' alaikum Wr. Wb.

Halo, Selamat malam kawan-kawan..

Pada kesempatan kali ini saya akan membagikan source kode untuk membuat bola bergerak dengan Java Applet..

public class bola extends Applet implements Runnable
{
     Thread runner;
    int xpos, xpos2;
    int ux1,ux2;     
    public void start()
    {
        if (runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }
    public void stop()
    {
        if (runner != null)
        {
            runner.stop();
            runner = null;
        }
    }
    public void run()
    {
        while (true)
        {
            //bergerak ke atas
            for(xpos2 = 200; xpos2 >2; xpos2-=4)
            {
                xpos = 5;
                ux1 = xpos;  
                repaint();
                try
                {
                    Thread.sleep(10);
                }
                catch(InterruptedException e)
                {
                    
                }
                if(ux2 == 00)
                {
                    ux2 = xpos2 + 90;
                }
            }
            
            //bergerak ke kanan
            for(xpos = 5; xpos <= 200; xpos+=4)
            {
                xpos2 = 5;
                ux2 = xpos + 90;
                repaint();
                try
                {
                    Thread.sleep(10);
                }
                catch(InterruptedException e)
                {
                   
                }
                if(ux1 == 00)
                {
                    ux1 = xpos;
                }
            }
            //bergerak ke bawah
             for(xpos2 = 5; xpos2 <=320; xpos2+=4)
            {
                xpos = 320;
                ux1 = xpos;
                repaint();
                try
                {
                    Thread.sleep(10);
                }
                catch(InterruptedException e)
                {
                    
                }
                if(ux2 == 00)
                {
                    ux2 = xpos2 + 90;
                }
            }            
             //bergerak ke kiri
            for(xpos = 320; xpos > 5; xpos-=4)
            {
                xpos2 = 320;
                ux1 = xpos;
                repaint();
                try
                {
                     //Kecepatan Bola
                    Thread.sleep(10);
                }
               
                catch(InterruptedException e)
                {
                    //kosongji
                }
                if(ux2 == 00)
                {
                    ux2 = xpos +90;
                }
            }   
        }
    }
    public void update(Graphics g)
    {
       // g.clipRect(ux1, 5, ux2-ux1, 95);
        paint(g);
    }
    public void paint(Graphics g)
    {
        //Warna Bidang Kotak
        g.setColor(Color.yellow);
        g.fillRect(0, 0, 200, 200);
        g.setColor(Color.blue);
        g.fillRect(200, 0, 200, 200);
        g.setColor(Color.yellow);  
        g.fillRect(200, 200, 200, 200);
        g.setColor(Color.blue);
        g.fillRect(0, 200, 200, 200);
      
        
        //Warna Bola
        g.setColor(Color.red);
        g.fillOval(xpos, xpos2, 70, 70);
        ux1 = ux2 = 0;
    }      }

Oke.. Semoga bermanfaat yah kawan-kawan.. Salam Logika.. Salam Algoritma

Sekian.. Wassalamu'alaikum Wr. Wb

  • Share: