package com.njust.JunH.JavaStart;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class FontTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable(){
public void run(){
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class FontFrame extends JFrame {
public FontFrame(){
super.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
super.setTitle("FontTest");
FontComponent component = new FontComponent();
add(component);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
class FontComponent extends JComponent{
public void printComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
String msg = "Jun.H Love Shuang.W!";
Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f);
//measure the size of the message
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(msg, context);
double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
//System.out.println(x + " " + y);
double accent = -bounds.getY();
double baseY = y + accent;
g2.drawString(msg, (int)x, (int)baseY);
g2.setPaint(Color.LIGHT_GRAY);
g2.draw(new Line2D.Double(x, baseY, x+bounds.getWidth(), baseY));
Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
g2.draw(rect);
//g2.drawRect(0, 2, 34, 34);
}
}
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class FontTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable(){
public void run(){
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class FontFrame extends JFrame {
public FontFrame(){
super.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
super.setTitle("FontTest");
FontComponent component = new FontComponent();
add(component);
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
class FontComponent extends JComponent{
public void printComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
String msg = "Jun.H Love Shuang.W!";
Font f = new Font("Serif", Font.BOLD, 36);
g2.setFont(f);
//measure the size of the message
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(msg, context);
double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;
//System.out.println(x + " " + y);
double accent = -bounds.getY();
double baseY = y + accent;
g2.drawString(msg, (int)x, (int)baseY);
g2.setPaint(Color.LIGHT_GRAY);
g2.draw(new Line2D.Double(x, baseY, x+bounds.getWidth(), baseY));
Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight());
g2.draw(rect);
//g2.drawRect(0, 2, 34, 34);
}
}