Draw Rectangle Dots at Random Locations in an Applet


/*   Write a program to Draw Rectangle Dots at Random Locations in an Applet
This Java example shows draw dots at random locations at specified interval using Java Thread and Applet classes. */

import java.applet.Applet;
import java.awt.*;
import java.awt.Dimension;
import java.awt.Graphics;
public class doot extends Applet implements Runnable{

Thread t;
public void init(){
//start new Thread
t = new Thread(this);
t.start();
}
public void run(){
try{
while(true){
// Request repaint
repaint();
Thread.sleep(200);
}
}
catch(Exception e){
}
}
public void update(Graphics g){
paint(g);
}

public void paint(Graphics g){
Dimension d = getSize();
int x = (int)(Math.random() * d.width);
int y = (int)(Math.random() * d.height);
g.setColor(Color.red );
g.fillRect(x,y,10,10);
Dimension d1 = getSize();
int x1 = (int)(Math.random() * d1.width);
int y1 = (int)(Math.random() * d1.height);
g.setColor(Color.blue );
g.fillRect(x1,y1,10,10);
}

}

/*
<applet code = “doot.class” width = 500 height = 300>
</applet>
*/

Output:

jap

Related Posts

Comments are closed.