by Dinesh Thakur Category: AWT and Applets

The Color class provides various methods to use any color you want in display. It defines various color constants which can be directly used only by specifying the color of your choice. In addition, the Color class allows creation of millions of colors. The Color class contains three primitive colors namely, red, blue and green and all other colors are a combination of these three colors. One of the constructors that is used to create color of your choice is

Color (int red, int green, int blue)

where,

red, green, blue can take any value between 0 and 255.

Setting Background and Foreground Color

To set the color of the background of an applet window, setBackground () method is used.

The general form of the setBackground () method is

void setBackground(mycolor)

Similarly, to set the foreground color to a specific color, that is, the color of text,

setForeground () method is used.

The general form of the setForeground () method is

void setForeground(mycolor)

where,

mycolor is one of the color constants or the new color created by the user

The list of color constants is given below:

• Color.red

• Color.orange

• Color.gray

• Color.darkGray

• Color.lightGray

• Color.cyan

• Color.pink

• Color.white

• Color.blue

• Color.green

• Color.black

• Color.yellow

import java.applet.Applet;
import java.awt.*;                //Color and Graphics belongs to java.awt,
import java.awt.event.*;
public class ColorApplet extends Applet
{
        public static void main(String[] args)
       {
        Frame ForegroundBackgroundColor = new Frame("Change Background and Foreground Color of Applet");
        ForegroundBackgroundColor.setSize(420, 180);
        Applet ColorApplet = new ColorApplet();
        ForegroundBackgroundColor.add(ColorApplet);
        ForegroundBackgroundColor.setVisible(true);
ForegroundBackgroundColor.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
                                                                                            });
     }
  public void paint(Graphics g)
{
    Color c = getForeground();
    setBackground(Color.yellow);  /*There are many predefined colors, or you can create your own predefined colors to Change are
black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow */
    setForeground(Color.red);    /*There are many predefined colors, or you can create your own predefined colors to Change are
black,blue,cyan,darkGray,gray,green,lightGray,magenta,orange,pink,red,white,yellow */
    g.drawString("Foreground color set to red", 100, 50); // Drawing texts on the graphics screen:
    g.drawString(c.toString(), 100, 80); /*The drawString() method, takes parameters as instance of String containing where the text to be drawn, and two integer values specifying
the coordinates where the text should place.*/
   g.drawString("Change Background and Foreground Color of Applet", 50, 100);
  }
}

 

    Change Background and Foreground Color of Applet





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles