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



Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details