Program to draw a text at random location in random color

This program will display the text at random location with random colours.




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

public class RandomColorTextExc  extends Applet implements Runnable {

 Thread t;
 Graphics g;
 public void init()
 {
  g = getGraphics();
  Font f = new Font("Cambria",Font.BOLD,16);
  g.setFont(f);
  t = new Thread(this);
  t.start();
 }
 public void run()
 {
  while(true)
  {
   int x = (int)(Math.random() * 34534 % 500);
   int y = (int)(Math.random() * 34534 % 500);
   int r1 = (int)(Math.random() * 34534 % 256);
   int r2 = (int)(Math.random() * 34534 % 256);
   int r3 = (int)(Math.random() * 34534 % 256);
   
   Color c = new Color(r1,r2,r3);
   g.setColor(c);
   g.drawString("Java",x,y);
   
   try
   {
    Thread.sleep(70);
   }catch(Exception ee){ }
  }
 }
}
/* <applet code="RandomColorTextExc.class" width="250" height="250">
   </applet>  */

OUTPUT

C:\>javac RandomColorTextExc.java
C:\>appletviewer RandomColorTextExc.java


Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative