Penggunaan font di Java 2D dapat dilakukan dengan memanggil method setFont dan drawstring di Graphics2D. Font yang tersedia di lingkungan tergantung pada platform. Di Java mendukung beberapa font diantaranya :
Serif
SanSerif
Monospaced
Dialog
DialogInput
Selain itu terdapat parameter style yang memungkinkan untuk memanipulasi bentuk font diantaranya :
PLAIN
ITALIC
BOLD
Berikut ini beberapa contoh source code untuk mendemonstrasikan penggunaan font.
/** * * @author Wim Sonevel */ import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class FontFun1 extends JApplet{ public static void main(String[] args){ JFrame frame = new JFrame(); frame.setTitle("Fonts"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new FontFun1(); applet.init(); frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); } @Override public void init(){ JPanel panel = new FontPanel(); getContentPane().add(panel); } } class FontPanel extends JPanel{ FontPanel(){ setPreferredSize(new Dimension(640, 480)); setBackground(Color.white); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; //menentukan nama, bentuk dan ukuran font Font font = new Font("Serif", Font.BOLD, 75); //transformasi affine AffineTransform tx = new AffineTransform(); tx.shear(0.5, 0.1); //font turunan hasil modifikasi dengan tranformasi affine g2.setFont(font.deriveFont(tx)); g2.drawString("Wim Sonevel", 100, 225); } }
Outputnya :
/** * * @author Wim Sonevel */ import java.awt.*; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; import java.awt.geom.*; import javax.swing.*; public class FontFun2 extends JApplet{ public static void main(String[] args){ JFrame frame = new JFrame(); frame.setTitle("Fonts"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new FontFun2(); applet.init(); frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true); frame.setLocationRelativeTo(null); } @Override public void init(){ JPanel panel = new FontPanel2(); getContentPane().add(panel); } } class FontPanel2 extends JPanel{ FontPanel2(){ setPreferredSize(new Dimension(640, 480)); setBackground(Color.white); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; Font font = new Font("Serif", Font.BOLD|Font.PLAIN|Font.ITALIC, 75); FontRenderContext frc = g2.getFontRenderContext(); GlyphVector gv = font.createGlyphVector(frc, "Wim Sonevel"); Shape glyph = gv.getOutline(100,100); g2.setStroke(new BasicStroke(3.0f)); g2.rotate(Math.toRadians(45), 100, 100); g2.draw(glyph); g2.setColor(Color.blue); g2.fill(glyph); } }
Output :
/** * * @author Wim Sonevel */ import javax.swing.*; import java.awt.*; import java.awt.font.*; public class FontFun3 extends JApplet{ public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Font Fun 3"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new FontFun3(); applet.init(); frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true); } @Override public void init(){ JPanel panel = new FontPanel3(); getContentPane().add(panel); } } class FontPanel3 extends JPanel{ public FontPanel3(){ setPreferredSize(new Dimension(600, 500)); setBackground(Color.white); } @Override public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; Font font = new Font("Serif",Font.BOLD,50); //font render FontRenderContext fr = g2.getFontRenderContext(); GlyphVector gv = font.createGlyphVector(fr, "Wim Sonevel"); Shape text = gv.getOutline(100, 100); g2.setStroke(new BasicStroke(5.0f)); g2.setColor(Color.red); g2.rotate(Math.toRadians(30),100,100); g2.draw(text); } }
Outputnya :
0 Komentar
Penulisan markup di komentar