Login    New User    Forgot Password    Help      

Java Color,Font,Back Color

Java Tutorials

Color

The Color class is used encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace. Every color has an implicit alpha value of 1.0 or an explicit one provided in the constructor. The alpha value defines the transparency of a color and can be represented by a float value in the range 0.0 - 1.0 or 0 - 255. An alpha value of 1.0 or 255 means that the color is completely opaque and an alpha value of 0 or 0.0 means that the color is completely transparent. When constructing a Color with an explicit alpha or getting the color/alpha components of a Color, the color components are never premultiplied by the alpha component.

Color

Predefined color constants or long integer corresponding to an RGB value.   Predefined constants include: 

black
blue
green
cyan
red
pink
orange
magenta
gray
darkGray
skyBlue
lightGreen
violet
brickRed
seaGreen
hotPink

lightGray
yellow
paleYellow
white

Colors can be specified using any of three techniques.  The first is limited to the color names shown in the list to the left.  The second is consistent with color specifications in HTML pages, and the third is a variant of this with the value being expressed as an integer value.

Format #1 - Specify by Name:

PenColor.1="red"

Format #2 - Specify a hex RGB value:

BackgroundColor="#FF00CC"

Format #3 - Specify an integer RGB value:

TitleColor="255"

 

Font

The applet fonts are controlled via three parameters per font.  Those include the font name, size, and style.  When using the template editors, these parameters are editable in a WYSIWYG mode.  When assigning them manually via applet parameters or name/value pairs in a URL, there are rules as to the range of values for these properties.  The font name is one from the following list:

  • SansSerif

  • Monospaced

  • TimesRoman

  • Helvetica

  • Courier

  • Serif

  • Dialog

  • DialogInput

  • ZapfDingbats

The font style is an integer parameter that utilizes the following style constants:

  • 0 : Plain

  • 1 : Bold

  • 2 : Italic

Example

import java.awt.*; import java.applet.*; /* applet code ="app1" width =200 height = 60> */ public class app2 extends java.applet.Applet { Font f = new Font("TimesRoman", Font.BOLD, 36); public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString("Hello again!", 5, 40); } }

Output

Output in Applet

Java Tutorials