Posts

Showing posts from April 7, 2014

Java Applet program to draw and move rectangle

import java.applet.Applet;
import java.awt.*;
import java.math.*;

/*<applet code="pro" height=100 width=100></applet>
  */

public class pro extends Applet implements Runnable
{
Thread t;

public void init()
{
t=new Thread(this);
t.start();
}

public void run()
{
try
{
while(true)
{
repaint();
t.sleep(1000);
}
}catch(Exception e)
{
System.out.println(e);
}
}

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

public void paint(Graphics g)
{

Dimension d=this.getSize();

g.setColor(Color.black);
g.fillRect(0,0,d.width,d.height);


double x=Math.random()*d.width;
double y=Math.random()*d.height;
g.setColor(Color.blue);
g.fillRect((int)x,(int)y,50,50);
}

}

Applet program to display random rectangle drawing on screen

import java.applet.Applet;
import java.awt.*;
import java.math.*;

/*<applet code="pro" height=100 width=100></applet>
  */

public class pro extends Applet implements Runnable
{
Thread t;

public void init()
{
t=new Thread(this);
t.start();
}

public void run()
{
try
{
while(true)
{
repaint();
t.sleep(1000);
}
}catch(Exception e)
{
System.out.println(e);
}
}

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

public void paint(Graphics g)
{
setBackground(Color.black);

Dimension d=this.getSize();
double x=Math.random()*d.width;
double y=Math.random()*d.height;
setForeground(Color.blue);
g.fillRect((int)x,(int)y,50,50);
}

}