Java源代码-Applet-居中显示文字

来源:互联网 发布:程序员有多少种语言 编辑:程序博客网 时间:2024/05/02 10:54

import java.awt.*;
import java.applet.*;

public class PrintCenterString extends Applet
{
Font F;
Color C;
FontMetrics FM;
int X,Y;
int Ascent,Descent,Width,AppletWidth,AppletHeight;
String str;

public void init()
{
str=new String("Cent");
AppletWidth=getSize().width;
AppletHeight=getSize().height;
setBackground(Color.green);
}


public void paint(Graphics g)
{
F=new Font("宋体",Font.BOLD+Font.ITALIC,72);
C=new Color(110,110,110);
g.setFont(F);
g.setColor(C);
FM=g.getFontMetrics();
Ascent=FM.getAscent();
Descent=FM.getDescent();
Width=FM.stringWidth(str);
X=(AppletWidth-Width)/2;
Y=(AppletHeight-(Ascent + Descent ))/2;
g.drawString(str,X,Y);
g.setColor(Color.white);
g.drawString(str,X+5,Y+5);
}

原创粉丝点击