Find us on Google+ Kill the code: moving
Showing posts with label moving. Show all posts
Showing posts with label moving. Show all posts

Saturday, 25 February 2012

Moving Circle in JAVA

An applet program to move circle in JAVA

import java.applet.*;
import java.awt.*;

/*
  <applet code="moving_circle" width=300 height=100>
  </applet>
*/

public class moving_circle extends Applet implements Runnable
{
    int x=0;
    Thread t;
    Image buffer;
    Graphics bufferg;

    public void init()
    {
        t=new Thread(this);
        t.start();
        Dimension d=getSize();
        buffer=createImage(d.width,d.height);
    }
    public void run()
    {
        try
        {
            while(true)
            {
               repaint();
               Thread.sleep(500);
            }
        }
        catch(Exception e)
        {
        }
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        if(bufferg==null)
            bufferg=buffer.getGraphics();

        Dimension d=getSize();
        bufferg.setColor(Color.black);
        bufferg.fillRect(0,0,d.width,d.height);
        bufferg.setColor(Color.white);
        bufferg.fillOval(x,d.height/4,50,50);

        g.drawImage(buffer,0,0,this);
        x+=5;
        if(x+50>d.width)
            x=0;
    }
}