Java - Explain how to set the background color within the applet area

Explain how to set the background color within the applet area.

- In an applet, by default it has gray background when displayed in a browser.

- If you want to change the background color of an applet, then you can call the setBackground(java.awt.Color) method.

- Using the setBackground(java.awt.Color) method you can choose whichever color you want.

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

public class BackgroundColorApplet extends Applet
{
   public void init()
   {
       setBackgroundColor(Color.YELLOW);
   }
}

- In the above example, defining the background color in the init() method will change the color as soon as the applet initialized. The BackgroundColorApplet class extends from the Applet class, for accessing these classes you have to import the applet packages at the start of the program.

You can set the background color of an applet in the following manner:
<applet code="MyApplet.class" width="100" height="100">
<param name="background-color" value="#ffffff">
<param name="foreground-color" value="#000000">
</applet>
Java - What are methods that controls an applet’s life cycle, i.e. init, start, stop and destroy?
Methods that controls an applet’s life cycle - Life Cycle of an Applet: Basically, there are four methods in the Applet class on which any applet is built...
Java - What are the methods that control an applet’s on-screen appearance? I.e. update and paint
Methods that control an applet’s on-screen appearance - The paint() method is called in situations the applet window being overwritten by another window or uncovered or the applet window being resized...
Java - Explain how to read information from the applet parameters
How to read information from the applet parameters - The getParameter() method can be used within the init method to access the parameter data. It takes the parameter name as an argument...
Post your comment