java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to change an applet background color?

By default the applet will have a gray background. If you want to change it then you can call the setBackground(java.awt.Color) and choose the color you want. Defining the background color in the init method will change the color as soon as the applet initialized.

package com.javacoderanch.example.applet;

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

public class WhiteBackgroundApplet extends Applet {
    public void init() {
	//
	// Here we change the default gray color background of an applet to
	// white background.
	//
	setBackground(Color.WHITE);
    }
    
    public void paint(Graphics g) {
	g.setColor(Color.BLACK);
	g.drawString("Applet background example", 0, 50);
    }
}