Font Derivation : Font « 2D Graphics GUI « Java






Font Derivation

Font Derivation
    
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontDerivation extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    // Create a 1-point font.
    Font font = new Font("Serif", Font.PLAIN, 1);
    float x = 20, y = 20;

    Font font24 = font.deriveFont(24.0f);
    g2.setFont(font24);
    g2.drawString("font.deriveFont(24.0f)", x, y += 30);

    Font font24italic = font24.deriveFont(Font.ITALIC);
    g2.setFont(font24italic);
    g2.drawString("font24.deriveFont(Font.ITALIC)", x, y += 30);

    AffineTransform at = new AffineTransform();
    at.shear(.2, 0);
    Font font24shear = font24.deriveFont(at);
    g2.setFont(font24shear);
    g2.drawString("font24.deriveFont(at)", x, y += 30);

    Hashtable attributes = new Hashtable();
    attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
    Font font24bold = font24.deriveFont(attributes);
    g2.setFont(font24bold);
    g2.drawString("font24.deriveFont(attributes)", x, y += 30);
  }

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new FontDerivation());
    f.setSize(350, 250);
    f.show();
  }
}

           
         
    
    
    
  








Related examples in the same category

1.Ascii font pattern
2.A cache of the dynamically loaded fonts found in the fonts directory
3.Load font from ttf file
4.Outline Font paintOutline Font paint
5.Font paintFont paint
6.Font centeredFont centered
7.Font ListFont List
8.FontDemo lists the system fonts and provides a sample of each one(Have some problems)
9.Finds and displays available fonts
10.Obtain FontMetrics of different fonts
11.Draw font inside a Rectangle
12.Display font in a grid
13.Create font from true type font
14.Wrap string according to FontMetrics
15.Draw base line and enclosing line for a font
16.List all Fonts