Program to set background and foreground color of an Applet

To set the background and foreground color of an applet setBackground(Color c) and setForeground(Color c) methods are used respectively. You have to use these methods in applet's init( ) method. The output of program is shown in OUTPUT section.


PROGRAM
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class SetBackColor extends Applet {
 
 public void init() 
 {
  setBackground(Color.cyan);
  setForeground(Color.red);
 }
 
 public void paint(Graphics g)
 {
  g.drawString("Hello Java",50,50);
 }
}

/* 
<applet code="SetBackColor" width=200 height=200>
</applet>
*/

OUTPUT

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