📄 fontinfo.jsp
字号:
<!--
Fig. 6.04_02_01: fontInfo.jsp
功能: 获取字符的精确信息
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.image.*"
import="java.awt.*"
import="javax.imageio.*"
%>
<%
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 创建一个 640X480 的图像
int width = 640, height = 480;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 填充背景
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
Font[] font = new Font[2];
font[0] = new Font("方正粗宋简体", Font.PLAIN, 20);
font[1] = new Font("华文隶书", Font.BOLD, 18);
String title="欢迎来到Java Web图表编程的世界";
String info="";
int ascent, descent, fontHeight, leading, stringLength;
for (int i= 0; i<font.length; i++)
{
g.setFont(font[i]);
ascent = g.getFontMetrics().getAscent();
descent = g.getFontMetrics().getDescent();
fontHeight = g.getFontMetrics().getHeight();
leading = g.getFontMetrics().getLeading();
stringLength = g.getFontMetrics().stringWidth(title);
info = "高度是: " + fontHeight + ", 上升值是: " +
ascent + ", 下降值是: " + descent +
", 超出值是: " + leading;
g.drawString("当前字体:" + font[i].getName() +
", 字体信息:" , 10, 50 + i*100);
g.drawString(info , 10, 80 + i* 100);
g.drawString("字符串:\""+ title +"\"的长度是:"+ stringLength,
10, 110 + i*100);
}
// 部署图形
g.dispose();
// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
%>
<%
/**************************************************************************
* (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors of this code have used their *
* best efforts in preparing the code. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these codes. The authors *
* shall not be liable in any event for incidental or consequential *
* damages in connection with, or arising out of, the furnishing, *
* performance, or use of these programs. *
**************************************************************************/
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -