+62 852-4056-7490 •

Artikel

Membuat Bola Bergerak Dengan Java Applet

Rahman 3 Comments Agustus 14, 2018
img

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


Tags: algoritma

3 Comments

reply

John Doe

June 29, 2014 - 11:23 Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
reply

Diana Doe

June 29, 2014 - 11:23 Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
reply

Melissa Doe

June 29, 2014 - 11:23 Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Peter Doe

June 29, 2014 - 11:23 Proin eget tortor risus. Cras ultricies ligula sed magna dictum porta. Pellentesque in ipsum id orci porta dapibus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Leave a reply Your email address will not be published.


Allowed HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Inappropriate and irrelevant comments will be removed at an admin's discretion.
Your email is used for verification purposes only, it will never be shared.