📄 multilinelayout.jsp
字号:
<!--
Fig. 7.11_03: multiLineLayout.jsp
功能: 基于Java2D的Web图表实例14: 文本自动分行显示
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.*"
import="javax.imageio.*"
import="java.awt.geom.*"
import="java.awt.image.*"
import="java.awt.font.*"
import="java.text.*"
%>
<%
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 创建一个 500X375 的图像
int width = 500, height = 375;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 创建Java2D对象
Graphics2D g2d = image.createGraphics();
// 填充背景
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
Font font = new Font("华文隶书", Font.BOLD + Font.ITALIC, 35);
String text = "欢迎来到《精通Java Web动态图表编程》。这本书展示了如何使用" +
"Java Applet、Servlet、Java Server Pages (JSP) 及JavaBean来" +
"帮助你开发奇妙的Web图表应用程序 - 以一种跨平台的、小巧的和" +
"结构清晰的模式在Web上生成动态图表。 " +
"作者:钟京馗、唐桓" ;
AttributedString as = new AttributedString(text);
as.addAttribute(TextAttribute.FONT, font);
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = g2d.getFontRenderContext();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
float x = 10.f;
float y = 10.f;
// 定义文本分行的宽度
float wrappingWidth = 450;
// 绘制文本,如果文本的长度超过wrappingWidth的值,则进行分行处理
while (lbm.getPosition() < aci.getEndIndex())
{
TextLayout textLayout = lbm.nextLayout(wrappingWidth);
y += textLayout.getAscent();
textLayout.draw(g2d, x, y);
y += textLayout.getDescent() + textLayout.getLeading();
}
// 部署图形
g2d.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 + -