为什么执行结果是这个样子_JDK1.7

理论上应该出现“Hello World!”的字样,怎么执行结果是这个样子,不解
package com.practice.test;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.geom.Ellipse2D.Double;

import javax.swing.*;
/**
* 测试字体相关
* @author KangMing
* @version 1.7 2014-09-03
*/
public class FontTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});

}
}
/**
* A frame with a text message component
*
*/
class FontFrame extends JFrame{
public FontFrame(){
setTitle("FontTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

//将组件加到框架中
FontComponent component = new FontComponent();
add(component);
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}

class FontComponent extends JComponent{
public void FontComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;

String message = "Hello World!";

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(message, context);

//set(x, y) = top - left corner of text

double x = (getWidth() - bounds.getWidth()) / 2;
double y = (getHeight() - bounds.getHeight()) / 2;

//add ascent to y to reach the baseline

double ascent = -bounds.getY();
double baseY = y + ascent;

//draw the message

g2.drawString(message, (int) x, (int) baseY);
g2.setPaint(Color.LIGHT_GRAY);

//draw the baseline

g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(),baseY));

//draw the enclosing rectangle

Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(),bounds.getHeight());
g2.draw(rect);

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值