Program to display message "Hello World!" in different colors in applet

This Java example displays the message "Hello World!" in different colours in applet. The java.awt.Color class is used to get pre-defined colors in Java. We have created an array of objects of Color class which contains the pre-defined colours in Java.


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

public class PrintHelloWorldInDifferentColors extends Applet {
 
 public void paint(Graphics g) {
  
  Color clr[ ] = {Color.black, Color.red, Color.yellow, Color.blue, Color.cyan, 
       Color.gray, Color.green, Color.magenta, Color.pink, Color.orange };
  
  for(int i=0; i<clr.length; i++) {
   
   g.setColor(clr[i]);
   g.drawString("Hello World!", 10, 25*(i+1));
  }
 }
}

/* <applet code="PrintHelloWorldInDifferentColors.class" width="200" height="300">
   </applet>
*/

OUTPUT

C:\>javac PrintHelloWorldInDifferentColors.java
C:\>appletviewer PrintHelloWorldInDifferentColors.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