Home Java Example Java Swing Graphics2D Font Derivation

Related Tutorials


 
 

Share on Google+Share on Google+

Font Derivation

Advertisement
Font derivation refers to the different styles provided by the font. The method deriveFont() provides different styles to the characters.

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(.30);
  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(400250);
  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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2008

Related Tutorials

Discuss: Font Derivation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com