Font Derivation

In this section, you will studied about the font derivation.
Font derivation refers to the different styles provided by the font. The method deriveFont()
provides different styles to the characters. We are providing you an example.
The method deriveFont(float size) creates a font object, applying a size to
it and replicates
the current font object . The method deriveFont(int
style)
creates a new font object, applying a new style to it and replicates the current
font object. The method deriveFont(AffineTransform affineTransform) creates a new font object, applying a new transform to it
and replicates the current font
object. The class AffineTransform provides properties like shearing,
translation, scaling and rotation.
Here is the code of FontDerivatonExample.java
import java.awt.*;
import javax.swing.*;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
public class FontDerivationExample extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Font font = new Font("Monotype Corsiva", Font.PLAIN, 1);
float p = 15, q = 15;
Font font1 = font.deriveFont(20.0f);
g2d.setFont(font1);
g2d.drawString("Java is an Object Oriented Programming Language.",p, q += 25);
Font bold = font1.deriveFont(Font.BOLD);
g2d.setFont(bold);
g2d.drawString("Java is an Object Oriented Programming Language.",p, q += 25);
Font italic = font1.deriveFont(Font.ITALIC);
g2d.setFont(italic);
g2d.drawString("Java is an Object Oriented Programming Language.",p, q += 25);
AffineTransform affineTransform = new AffineTransform();
affineTransform.shear(.3, 0);
Font shear = font1.deriveFont(affineTransform);
g2d.setFont(shear);
g2d.drawString("Java is an Object Oriented Programming Language.",p, q += 25);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Font Derivation Example");
frame.getContentPane().add(new FontDerivationExample());
frame.setSize(400, 250);
frame.show();
}
}
|
Output will be displayed as

The first sentence is written in plain font, second one is written in
bold, third one is written in italics and the last one is written in shear font.
Download Source Code

|