To display colored bubbles of different sizes


In this program,Applet is used to display coloured bubbles of different sizes.The choice of the colour or size is done randomly.The array colors is used to store the different colours.The Math.random()method is used to randomly select the colour from array and to calculate the size of the bubble.

//Write a program in Java for the usage of Threads with Applets

import java.awt.*;
import java.applet.*;
public class bub extends Applet implements Runnable
{
int top,left,w;
Thread t;
Color colors[]={Color.blue,Color.green,Color.red,Color.yellow,Color.black,Color.pink,Color.gray,Color.cyan,Color.white};
int i;
public  void init()
{
w=100;
t=new Thread(this);
t.start();
}
public void paint(Graphics g)
{
for(int a=0;a<=50;a++)
{
while(true)
{
i=(int)(Math.random()*1000)/25;
if(i>=0 && i<=6)
break;
}
w=(int)(Math.random()*1000)/(25-i);
top=(int)(Math.random()*6000)/(30-i);
left=(int)(Math.random()*10000)/(35-i);
g.setColor(colors[i]);
g.fillOval(top,left,w,w);
}
}
public void run()
{
while(true)
{
try
{
repaint();
Thread.sleep(1000);
}
catch(InterruptedException e)
{
}
}}}
/*
<applet code=”bub.class” height=500  width=500>
</applet>*/

OUTPUT:

Related Posts

Comments are closed.