Range of Color Components Values

5.1.6. Color control

In Java, we control colors using three primary colors: red, green, and blue. Java uses the RGB color model. The object of the Color class contains three integers for the parameters red, green, and blue. The following table shows the possible values ​​of these colors:

Ingredient

Scope

Red

0-255

Green

0-255

Blue

0-255

Maybe you are interested!

Table 5.2. Value ranges of color components

Use the above values ​​to create a custom color. The syntax of the constructor to create a color is as follows:

color (int red, int green, int blue);

The following table shows the values ​​of common colors:


Color

Red

Green

Blue

White

255

255

255

Light Gray

192

192

192

Gray

128

128

128

Dark Gray

64

64

64

Black

0

0

0

Pink

255

175

175

Orange

255

200

0

Yellow

255

255

0

Magenta

255

0

255

Table 5.3. RGB values

Different color objects can be created using these values. These objects can be used to draw or paint graphic objects. For example, to create pink, we use the following command:

color c = new Color(255, 175, 175);

We can set the color by using the following command:

g.setColor(c); //g is an object of Graphics class

Use a combination of RGB values ​​to create a custom color. To make it easier, the Color class provides some built-in colors.

color.white

color.black

color.orange

color. gray

color.lightgray

color.darkgray

color.red

color.green

color.blue

color.pink

color.cyan

color.magenta

color.yellow


Table 5.4. Common colors

The following code illustrates how to create a custom color:

Color color1 = new Color(230, 140, 60);

Color color4 = new Color(90, 210, 130); g.setColor(color1);

int myred = color1.getRed();

int mygreen = color1.getGreen(); int myblue = color1.getBlue(); color1 = color1.darker();

color4 = color4.brighter();

5.1.7. Font Control

Java provides Font class in java.awt package to use different types of fonts. This class includes several methods.

To use the font, we should check if the system supports it or not.

The „getAllFont()‟ method returns all the fonts supported by the system.

First, declare an object of the GraphicsEnvironment class as follows:

GraphicsEnvironment ge;

ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

This object uses the following syntax to get all the fonts contained in the Font array:

Font f[] = ge.getAllFonts();

getAllFont() method is used here. getAllFonts() method belongs to GraphicsEnvironment class. This is abstract class, so we cannot instantiate this class. To access getAllFont() method, we use

„getLoacalGraphicsEnvironment()‟ of the GraphicsEnvironment class.

ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

A reference to this class is assigned to the variable ge. This variable calls the getAllFont() method. We use different fonts to display different strings. The getFont() method returns the default font used to display the string, when no font is selected.

Font defaultFont = g.getFont(); //g is the Graphics object g.drawString (“Default Font is”, 30, 50);

Dialog is the system default font.

To change the system default font to another font, we create an object of the Font class. The Font constructor has the following 3 parameters:

The name of the font. We can get the name through the getFontList() method.

Font type. For example: Font.BOLD, Font.PLAIN, Font.ITALIC.

Font size.

The following syntax illustrates the above parameters:

Font f1 = new Font (“SansSerif”, Font.ITALIC, 16); g.setFont(f1);

The three parameters passed here are: „SanSerif‟ – the name of the font, Font.BOLD – the font style, 14 is the font size. These parameters create the f1 object. We can combine two font styles together. Consider the following example:

Font f3 = new Font (“Monospaced”, Font.ITALIC+Font.BOLD, 20); Here the font style of f3 is both bold and italic.

5.1.8. FontMetrics class

This class defines the size of different characters in different fonts. The size definition includes height, baseline, descent, and leading. This is important because each character occupies a specific space when printed. You need to calculate the size needed when printing characters to avoid overlapping characters.

Height: font height.

Baseline: defines the base of the characters (excluding the lowest part of the character)

Ascent: distance from baseline to top of character.

Descent: the distance from the baseline to the bottom of the character.

Leading: the space between letters.

The following example 5.6 illustrates the use of different methods available in the FontMetrics class. In this program, we use different methods to look at the details of different fonts. The FontMetric class is abstract. The getFontMetrics() method takes an object of the Font class as a parameter, since FontMetrics is associated with a particular font.

FontMetrics fm = g.getFontMetrics(f1);

This command creates an fm object of the FontMetrics class, along with an f1 object. Now, we use fm to get the font details.

The getHeight, getAscent(), getDescent(), and getLeading() methods return font details. The getFont() method of the FontMetrics class returns the Font associated with the FontMetrics class object. The getName() method of the Font class returns the Font name.

Example 5.6: Using the different methods provided by the FontMetrics class

import java.awt.*;

class FontMetricsUse extends Frame

{


public FontMetricsUse()

{


super("Detail of Fonts"); setSize(400, 300); setVisible(true);

}


public void paint (Graphics g)

{


Font f1 = new Font ("Times Roman", Font.PLAIN, 22); FontMetrics fm = g.getFontMetrics(f1);

String name = fm.getFont().getName(); g.drawString ("Details of Font " + name, 30, 50);


125);


150);


250);


275);

}

g.drawString("Height: " + String.valueOf (fm.getHeight()), 50, 75); g.drawString("Ascent: " + String.valueOf(fm.getAscent()), 50, 100); g.drawString("Descent: " + String.valueOf(fm.getDescent()), 50,


g.drawString("Leading: " + String.valueOf(fm.getLeading()), 50,


Font f2 = new Font("DialogInput", Font.PLAIN, 22); fm = g.getFontMetrics(f2);

name = fm.getFont().getName();

g.drawString ("Details of Font " + name, 30, 175);

g.drawString("Height: " + String.valueOf(fm.getHeight()), 50, 200); g.drawString("Ascent: " + String.valueOf(fm.getAscent()), 50, 225); g.drawString("Descent: " + String.valueOf(fm.getDescent()), 50,


g.drawString("Leading: " + String.valueOf(fm.getLeading()), 50,


public static void main (String args[])

{


new FontMetricsUse();

}

}


Results of the above program:


Figure 5.7. Result of running example 5.6

Example 5.7 illustrates how the FontMetrics class is used to print multi-font, multi-line text. In this program, we need to print multi-font text on multiple lines. The FontMetrics class helps us determine the spacing required to print a line of text for a particular font. This is necessary because the second line is printed immediately after the first line.

First we print msg1 using Monospaced font. Then we print msg2 using DiaglogInput font. To do this, we need to calculate the spacing required to print msg1. The stringWidth() method of the FontMetrics class is used to calculate the total spacing required to print msg1. When we add this spacing to the variable x, we get the position where we start printing the next paragraph of text, msg2. The setFont() method is used to set the font for printing text.

Next, we print msg1 and msg2 on different lines using the same Monospaced font. Here, we need to know the height of the font, to print the next line. The getHeight() method is used to do this.

Example 5.7: Demonstrates how the FontMetrics class is used to print multi-font, multi-line text.

import java.awt.*;

class MultiFontMultiLine extends Frame

{


public MultiFontMultiLine()

{


super("Multiline Text"); setSize(450, 200); setVisible(true);

}


public void paint (Graphics g)

{


Font f1 = new Font("MonoSpaced", Font.BOLD, 18); Font f2 = new Font("DialogInput", Font.PLAIN, 14); int x = 20;

int y = 50;

String msg1 ="Java Language";

String msg2 ="A new approach to programming"; FontMetrics fm = g.getFontMetrics(f1); g.setFont(f1);

g.drawString(msg1, x, y);

x = x + fm.stringWidth(msg1); g.setFont(f2);

g.drawString(msg2, x, y); g.setFont(f1);

y = 100;

x = 20;

int height = fm.getHeight(); g.drawString(msg1, x, y); y += height;

g.drawString(msg2, x, y);

}


public static void main (String args[])

{


new MultiFontMultiLine ();

}

}


Results of the above program:



Figure 5.8. Result of running example 5.7

5.1.9. Select the mode to draw


Objects are drawn using the drawing mode. When a new object is drawn, it overwrites previously drawn shapes. Similarly, when objects are drawn over and over again,

repeatedly, they will erase the previously drawn objects. Only the contents of the new object are displayed. To make the contents of the old object and the new content appear on the same background, the Graphics class provides the method setXORMode(Color c);

Example 5.8 illustrates the convenience of using the setXORMode() method. Here, we use the setXORMode() method to color different graphics without overlapping the other shapes. As a result, when using XOR mode, it is obvious that all the shapes are fully displayed. This means that new shapes do not overlap the old shapes. Instead, the common parts between the shapes are displayed in a different color. But when not using XOR mode, the new shapes completely obscure the previous shapes.

import java.awt.*;

class PaintMode extends Frame

{


public PaintMode()

{


super("Paint Mode"); setSize(300, 300); setVisible(true);

}


public void paint (Graphics g)

{


g.setPaintMode(); g.setColor(Color.blue); g.fillRect(50,50,40, 30); g.setColor(Color.pink); g.fillOval(70, 65, 30, 60); g.setColor(Color.cyan);

g.fillRoundRect(90, 80, 70, 50, 20, 30); g.setColor(Color.blue);

g.fillRect(50, 150, 40, 30); g.setXORMode(Color.yellow); g.fillOval(70, 165, 30, 60);

Comment


Agree Privacy Policy *