Java by API/java.awt
- AWTEvent
- AWTEventMulticaster
- AWTPermission
- ActionListener
- AlphaComposite
- AudioClip
- BasicStroke
- BorderLayout
- CardLayout
- Color
- Component
- ComponentOrientation
- Container
- Cursor
- DebugGraphics
- Desktop
- Dialog
- DisplayMode
- EventQueue
- FileDialog
- FlowLayout
- FocusTraversalPolicy
- Frame
- GradientPaint
- Graphics
- Graphics2D
- GraphicsConfiguration
- GraphicsDevice
- GraphicsEnvironment
- GridBagConstraints
- GridBagLayout
- GridLayout
- Image
- ItemSelectable
- KeyboardFocusManager
- LayoutManager
- LayoutManager2
- MediaTracker
- Point
- Rectangle
- RenderingHints
- Robot
- Shape
- SplashScreen
- SpringLayout
- SpringLayout.Constraints
- SystemColor
- SystemTray
- TexturePaint
- Toolkit
- Transparency
- TrayIcon
Содержание
- 1 Font.BOLD
- 2 Font.BOLD | Font.ITALIC
- 3 Font: createFont(int fontFormat, InputStream fontStream)
- 4 Font: createGlyphVector(FontRenderContext frc, String str)
- 5 Font: deriveFont(int style, float size)
- 6 Font: getFamily()
- 7 Font: getFontName()
- 8 Font: getLineMetrics(String str, FontRenderContext frc)
- 9 Font: getName()
- 10 Font: getSize()
- 11 Font: getStringBounds(String str, FontRenderContext frc)
- 12 Font: getStyle()
- 13 Font.ITALIC
- 14 FontMetrics: getAscent()
- 15 FontMetrics: getDescent()
- 16 FontMetrics: getHeight()
- 17 FontMetrics: getLeading()
- 18 FontMetrics: getMaxAscent()
- 19 FontMetrics: getMaxDecent()
- 20 FontMetrics: stringWidth(String s)
- 21 Font.PLAIN
- 22 Font.TRUETYPE_FONT
- 23 new Font(String name, int style, int size)
Font.BOLD
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Font f = new Font ("Serif", Font.PLAIN, 12);
g.setFont (f);
g.drawString ("Serif - PLAIN - 12", 10, 30);
f = new Font ("Sanserif", Font.ITALIC, 10);
g.setFont (f);
g.drawString ("Sanserif - ITALIC - 10", 10, 60);
f = new Font ("Monospaced", Font.BOLD | Font.ITALIC, 14);
g.setFont (f);
g.drawString ("Monospaced - BOLD and ITALIC - 14", 10, 90);
f = new Font ("Dialog", Font.PLAIN, 12);
g.setFont (f);
g.drawString ("Dialog - PLAIN - 12", 10, 120);
f = new Font ("DialogInput", Font.BOLD + Font.ITALIC, 10);
g.setFont (f);
g.drawString ("DialogInput - BOLD and ITALIC - 10", 10, 150);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Font.BOLD | Font.ITALIC
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Font f = new Font ("Serif", Font.PLAIN, 12);
g.setFont (f);
g.drawString ("Serif - PLAIN - 12", 10, 30);
f = new Font ("Sanserif", Font.ITALIC, 10);
g.setFont (f);
g.drawString ("Sanserif - ITALIC - 10", 10, 60);
f = new Font ("Monospaced", Font.BOLD | Font.ITALIC, 14);
g.setFont (f);
g.drawString ("Monospaced - BOLD and ITALIC - 14", 10, 90);
f = new Font ("Dialog", Font.PLAIN, 12);
g.setFont (f);
g.drawString ("Dialog - PLAIN - 12", 10, 120);
f = new Font ("DialogInput", Font.BOLD + Font.ITALIC, 10);
g.setFont (f);
g.drawString ("DialogInput - BOLD and ITALIC - 10", 10, 150);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Font: createFont(int fontFormat, InputStream fontStream)
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void loadFont() throws FontFormatException, IOException{
String fontFileName = "yourfont.ttf";
InputStream is = this.getClass().getResourceAsStream(fontFileName);
Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, is);
Font ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);
}
public static void main(String[] args) {
}
}
Font: createGlyphVector(FontRenderContext frc, String str)
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
String s = "www.jexp.ru";
Font font = new Font("Serif", Font.PLAIN, 24);
FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, s);
g2.drawGlyphVector(gv, 40, 60);
}
}
Font: deriveFont(int style, float size)
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void loadFont() throws FontFormatException, IOException{
String fontFileName = "yourfont.ttf";
InputStream is = this.getClass().getResourceAsStream(fontFileName);
Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, is);
Font ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);
}
public static void main(String[] args) {
}
}
Font: getFamily()
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class MainClass {
public static void main(String[] args) throws Exception {
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i = 0; i < fonts.length; i++) {
System.out.print(fonts[i].getFontName() + " : ");
System.out.print(fonts[i].getFamily() + " : ");
System.out.print(fonts[i].getName());
System.out.println();
}
}
}
Font: getFontName()
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class MainClass {
public static void main(String[] args) throws Exception {
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i = 0; i < fonts.length; i++) {
System.out.print(fonts[i].getFontName() + " : ");
System.out.print(fonts[i].getFamily() + " : ");
System.out.print(fonts[i].getName());
System.out.println();
}
}
}
Font: getLineMetrics(String str, FontRenderContext frc)
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 72);
g2.setFont(font);
String s = "www.jexp.ru";
float x = 50, y = 150;
FontRenderContext frc = g2.getFontRenderContext();
float width = (float) font.getStringBounds(s, frc).getWidth();
Line2D baseline = new Line2D.Float(x, y, x + width, y);
g2.setPaint(Color.lightGray);
g2.draw(baseline);
// Draw the ascent.
LineMetrics lm = font.getLineMetrics(s, frc);
Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
g2.draw(ascent);
// Draw the descent.
Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
g2.draw(descent);
// Draw the leading.
Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width, y
+ lm.getDescent() + lm.getLeading());
g2.draw(leading);
// Render the string.
g2.setPaint(Color.black);
g2.drawString(s, x, y);
}
}
Font: getName()
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class MainClass {
public static void main(String[] args) throws Exception {
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i = 0; i < fonts.length; i++) {
System.out.print(fonts[i].getFontName() + " : ");
System.out.print(fonts[i].getFamily() + " : ");
System.out.print(fonts[i].getName());
System.out.println();
}
}
}
Font: getSize()
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] a) {
Main f = new Main();
f.setSize(300, 300);
f.setVisible(true);
}
public void paint(Graphics g) {
Font f = g.getFont();
String fontName = f.getName();
String fontFamily = f.getFamily();
int fontSize = f.getSize();
int fontStyle = f.getStyle();
String msg = "Family: " + fontName;
msg += ", Font: " + fontFamily;
msg += ", Size: " + fontSize + ", Style: ";
if ((fontStyle & Font.BOLD) == Font.BOLD)
msg += "Bold ";
if ((fontStyle & Font.ITALIC) == Font.ITALIC)
msg += "Italic ";
if ((fontStyle & Font.PLAIN) == Font.PLAIN)
msg += "Plain ";
g.drawString(msg, 4, 16);
}
}
Font: getStringBounds(String str, FontRenderContext frc)
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(new Font("Serif", Font.PLAIN, 48));
FontRenderContext frc = g2.getFontRenderContext();
String s = "www.jexp.ru";
Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
float width = (float) bounds.getWidth();
int centerX = 100;
int baselineY = 70;
g2.drawString(s, centerX - width / 2, baselineY);
}
}
Font: getStyle()
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] a) {
Main f = new Main();
f.setSize(300, 300);
f.setVisible(true);
}
public void paint(Graphics g) {
Font f = g.getFont();
String fontName = f.getName();
String fontFamily = f.getFamily();
int fontSize = f.getSize();
int fontStyle = f.getStyle();
String msg = "Family: " + fontName;
msg += ", Font: " + fontFamily;
msg += ", Size: " + fontSize + ", Style: ";
if ((fontStyle & Font.BOLD) == Font.BOLD)
msg += "Bold ";
if ((fontStyle & Font.ITALIC) == Font.ITALIC)
msg += "Italic ";
if ((fontStyle & Font.PLAIN) == Font.PLAIN)
msg += "Plain ";
g.drawString(msg, 4, 16);
}
}
Font.ITALIC
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Font f = new Font ("Serif", Font.PLAIN, 12);
g.setFont (f);
g.drawString ("Serif - PLAIN - 12", 10, 30);
f = new Font ("Sanserif", Font.ITALIC, 10);
g.setFont (f);
g.drawString ("Sanserif - ITALIC - 10", 10, 60);
f = new Font ("Monospaced", Font.BOLD | Font.ITALIC, 14);
g.setFont (f);
g.drawString ("Monospaced - BOLD and ITALIC - 14", 10, 90);
f = new Font ("Dialog", Font.PLAIN, 12);
g.setFont (f);
g.drawString ("Dialog - PLAIN - 12", 10, 120);
f = new Font ("DialogInput", Font.BOLD + Font.ITALIC, 10);
g.setFont (f);
g.drawString ("DialogInput - BOLD and ITALIC - 10", 10, 150);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
FontMetrics: getAscent()
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
String s = "www.jexp.ru";
int stringWidth = fm.stringWidth(s);
int w = 200;
int h = 200;
int x = (w - stringWidth) / 2;
int baseline = fm.getMaxAscent() +
(h - (fm.getAscent() + fm.getMaxDecent()))/2;
int ascent = fm.getMaxAscent();
int descent = fm.getMaxDecent();
int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();
g.setColor(Color.white);
g.fillRect(x, baseline-ascent , stringWidth, fontHeight);
g.setColor(Color.gray);
g.drawLine(x, baseline, x+stringWidth, baseline);
g.setColor(Color.red);
g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);
g.setColor(Color.blue);
g.drawLine(x, baseline-ascent,
x+stringWidth, baseline-ascent);
g.setColor(Color.black);
g.drawString(s, x, baseline);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
FontMetrics: getDescent()
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends JFrame {
public Main() {
super("Demonstrating FontMetrics");
setSize(510, 210);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.setFont(new Font("SansSerif", Font.BOLD, 12));
FontMetrics fm = g.getFontMetrics();
g.drawString("Current font: " + g.getFont(), 10, 40);
g.drawString("Ascent: " + fm.getAscent(), 10, 55);
g.drawString("Descent: " + fm.getDescent(), 10, 70);
g.drawString("Height: " + fm.getHeight(), 10, 85);
g.drawString("Leading: " + fm.getLeading(), 10, 100);
Font font = new Font("Serif", Font.ITALIC, 14);
fm = g.getFontMetrics(font);
g.setFont(font);
g.drawString("Current font: " + font, 10, 130);
g.drawString("Ascent: " + fm.getAscent(), 10, 145);
g.drawString("Descent: " + fm.getDescent(), 10, 160);
g.drawString("Height: " + fm.getHeight(), 10, 175);
g.drawString("Leading: " + fm.getLeading(), 10, 190);
}
public static void main(String args[]) {
Main app = new Main();
}
}
FontMetrics: getHeight()
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends JFrame {
public Main() {
super("Demonstrating FontMetrics");
setSize(510, 210);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.setFont(new Font("SansSerif", Font.BOLD, 12));
FontMetrics fm = g.getFontMetrics();
g.drawString("Current font: " + g.getFont(), 10, 40);
g.drawString("Ascent: " + fm.getAscent(), 10, 55);
g.drawString("Descent: " + fm.getDescent(), 10, 70);
g.drawString("Height: " + fm.getHeight(), 10, 85);
g.drawString("Leading: " + fm.getLeading(), 10, 100);
Font font = new Font("Serif", Font.ITALIC, 14);
fm = g.getFontMetrics(font);
g.setFont(font);
g.drawString("Current font: " + font, 10, 130);
g.drawString("Ascent: " + fm.getAscent(), 10, 145);
g.drawString("Descent: " + fm.getDescent(), 10, 160);
g.drawString("Height: " + fm.getHeight(), 10, 175);
g.drawString("Leading: " + fm.getLeading(), 10, 190);
}
public static void main(String args[]) {
Main app = new Main();
}
}
FontMetrics: getLeading()
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends JFrame {
public Main() {
super("Demonstrating FontMetrics");
setSize(510, 210);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.setFont(new Font("SansSerif", Font.BOLD, 12));
FontMetrics fm = g.getFontMetrics();
g.drawString("Current font: " + g.getFont(), 10, 40);
g.drawString("Ascent: " + fm.getAscent(), 10, 55);
g.drawString("Descent: " + fm.getDescent(), 10, 70);
g.drawString("Height: " + fm.getHeight(), 10, 85);
g.drawString("Leading: " + fm.getLeading(), 10, 100);
Font font = new Font("Serif", Font.ITALIC, 14);
fm = g.getFontMetrics(font);
g.setFont(font);
g.drawString("Current font: " + font, 10, 130);
g.drawString("Ascent: " + fm.getAscent(), 10, 145);
g.drawString("Descent: " + fm.getDescent(), 10, 160);
g.drawString("Height: " + fm.getHeight(), 10, 175);
g.drawString("Leading: " + fm.getLeading(), 10, 190);
}
public static void main(String args[]) {
Main app = new Main();
}
}
FontMetrics: getMaxAscent()
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
String s = "www.jexp.ru";
int stringWidth = fm.stringWidth(s);
int w = 200;
int h = 200;
int x = (w - stringWidth) / 2;
int baseline = fm.getMaxAscent() +
(h - (fm.getAscent() + fm.getMaxDecent()))/2;
int ascent = fm.getMaxAscent();
int descent = fm.getMaxDecent();
int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();
g.setColor(Color.white);
g.fillRect(x, baseline-ascent , stringWidth, fontHeight);
g.setColor(Color.gray);
g.drawLine(x, baseline, x+stringWidth, baseline);
g.setColor(Color.red);
g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);
g.setColor(Color.blue);
g.drawLine(x, baseline-ascent,
x+stringWidth, baseline-ascent);
g.setColor(Color.black);
g.drawString(s, x, baseline);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
FontMetrics: getMaxDecent()
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
String s = "www.jexp.ru";
int stringWidth = fm.stringWidth(s);
int w = 200;
int h = 200;
int x = (w - stringWidth) / 2;
int baseline = fm.getMaxAscent() +
(h - (fm.getAscent() + fm.getMaxDecent()))/2;
int ascent = fm.getMaxAscent();
int descent = fm.getMaxDecent();
int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();
g.setColor(Color.white);
g.fillRect(x, baseline-ascent , stringWidth, fontHeight);
g.setColor(Color.gray);
g.drawLine(x, baseline, x+stringWidth, baseline);
g.setColor(Color.red);
g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);
g.setColor(Color.blue);
g.drawLine(x, baseline-ascent,
x+stringWidth, baseline-ascent);
g.setColor(Color.black);
g.drawString(s, x, baseline);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
FontMetrics: stringWidth(String s)
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
String s = "www.jexp.ru";
int stringWidth = fm.stringWidth(s);
int w = 200;
int h = 200;
int x = (w - stringWidth) / 2;
int baseline = fm.getMaxAscent() +
(h - (fm.getAscent() + fm.getMaxDecent()))/2;
int ascent = fm.getMaxAscent();
int descent = fm.getMaxDecent();
int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();
g.setColor(Color.white);
g.fillRect(x, baseline-ascent , stringWidth, fontHeight);
g.setColor(Color.gray);
g.drawLine(x, baseline, x+stringWidth, baseline);
g.setColor(Color.red);
g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);
g.setColor(Color.blue);
g.drawLine(x, baseline-ascent,
x+stringWidth, baseline-ascent);
g.setColor(Color.black);
g.drawString(s, x, baseline);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Font.PLAIN
/*
* Output:
*
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
FontMetrics fm = g.getFontMetrics();
String s = "www.jexp.ru";
int stringWidth = fm.stringWidth(s);
int w = 200;
int h = 200;
int x = (w - stringWidth) / 2;
int baseline = fm.getMaxAscent() +
(h - (fm.getAscent() + fm.getMaxDecent()))/2;
int ascent = fm.getMaxAscent();
int descent = fm.getMaxDecent();
int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();
g.setColor(Color.white);
g.fillRect(x, baseline-ascent , stringWidth, fontHeight);
g.setColor(Color.gray);
g.drawLine(x, baseline, x+stringWidth, baseline);
g.setColor(Color.red);
g.drawLine(x, baseline+descent, x+stringWidth, baseline+descent);
g.setColor(Color.blue);
g.drawLine(x, baseline-ascent,
x+stringWidth, baseline-ascent);
g.setColor(Color.black);
g.drawString(s, x, baseline);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Font.TRUETYPE_FONT
/*
* @(#)DemoFonts.java 1.17 06/08/29
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
/*
* @(#)DemoFonts.java 1.17 06/08/29
*/
import java.awt.Font;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A cache of the dynamically loaded fonts found in the fonts directory.
*/
public class Main {
// Prepare a static "cache" mapping font names to Font objects.
private static String[] names = { "A.ttf" };
private static Map<String, Font> cache = new ConcurrentHashMap<String, Font>(names.length);
static {
for (String name : names) {
cache.put(name, getFont(name));
}
}
public static Font getFont(String name) {
Font font = null;
if (cache != null) {
if ((font = cache.get(name)) != null) {
return font;
}
}
String fName = "/fonts/" + name;
try {
InputStream is = Main.class.getResourceAsStream(fName);
font = Font.createFont(Font.TRUETYPE_FONT, is);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println(fName + " not loaded. Using serif font.");
font = new Font("serif", Font.PLAIN, 24);
}
return font;
}
}
new Font(String name, int style, int size)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Dimension d = this.getPreferredSize();
int fontSize = 20;
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
g.setColor(Color.red);
g.drawString("www.jexp.ru", 10, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
}