Tuesday, 29 November 2016

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.

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.drawString("Hello World!", 10, 25*(i+1));

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


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

No comments:

Post a comment