📄 fonteffect.jsp
字号:
<!--
Fig. 6.04_02_02: fontEffect.jsp
功能: 绘制多种效果的字体
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.image.*"
import="java.awt.*"
import="javax.imageio.*"
import="java.util.*"
%>
<%!
int ShiftNorth(int p, int distance)
{
return (p - distance);
}
int ShiftSouth(int p, int distance)
{
return (p + distance);
}
int ShiftEast(int p, int distance)
{
return (p + distance);
}
int ShiftWest(int p, int distance)
{
return (p - distance);
}
%>
<%
// 清空缓冲区
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();
// 填充背景
Color bg = new Color(150, 150, 150);
g.setColor(bg);
g.fillRect(0, 0, width, height);
// 生成阴影字体效果
int x = 10, y = 100;
g.setFont(new Font("方正粗宋简体", Font.PLAIN, 35));
g.setColor(new Color(50, 50, 50));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 2), ShiftSouth(y, 2));
g.setColor(new Color(220, 220, 220));
g.drawString("欢迎来到Java Web图表编程的世界", x, y);
// 生成浮雕字体效果
x = 10;
y = 150;
g.setColor(new Color(220, 220, 220));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 1), ShiftSouth(y, 1));
g.setColor(new Color(50, 50, 50));
g.drawString("欢迎来到Java Web图表编程的世界", x, y);
// 生成轮廓字体效果
x = 10;
y = 200;
g.setColor(Color.RED);
g.drawString("欢迎来到Java Web图表编程的世界", ShiftWest(x, 1), ShiftNorth(y, 1));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftWest(x, 1), ShiftSouth(y, 1));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 1), ShiftNorth(y, 1));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 1), ShiftSouth(y, 1));
g.setColor(Color.YELLOW);
g.drawString("欢迎来到Java Web图表编程的世界", x, y);
// 生成空心字体效果
x = 10;
y = 250;
g.setColor(Color.BLACK);
g.drawString("欢迎来到Java Web图表编程的世界", ShiftWest(x, 1), ShiftNorth(y, 1));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftWest(x, 1), ShiftSouth(y, 1));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 1), ShiftNorth(y, 1));
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 1), ShiftSouth(y, 1));
g.setColor(bg);
g.drawString("欢迎来到Java Web图表编程的世界", x, y);
// 生成百页窗字体效果
x = 10;
y = 300;
int w = (g.getFontMetrics()).stringWidth("欢迎来到Java Web图表编程的世界");
int h = (g.getFontMetrics()).getHeight();
int d = (g.getFontMetrics()).getDescent();
g.setColor(new Color(220, 220, 220));
g.drawString("欢迎来到Java Web图表编程的世界", x, y);
g.setColor(bg);
for (int i = 0; i < h; i += 3)
{
g.drawLine(x, y + d - i, x + w, y + d - i);
}
// 生成3D字体效果
x = 10;
y = 350;
Color topColor = new Color(200, 200, 0);
Color sideColor = new Color(100, 100, 0);
for (int i = 0; i < 5; i++)
{
g.setColor(topColor);
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, i), ShiftNorth
(ShiftSouth(y, i), 1));
g.setColor(sideColor);
g.drawString("欢迎来到Java Web图表编程的世界", ShiftWest(ShiftEast(x, i), 1),
ShiftSouth(y, i));
}
g.setColor(Color.YELLOW);
g.drawString("欢迎来到Java Web图表编程的世界", ShiftEast(x, 5), ShiftSouth(y, 5));
// 生成运动字体效果
x = 300;
y = 400;
int fontSize = 0;
for (int i = 0; i < 20; i++)
{
fontSize = 12+i;
g.setFont(new Font("方正粗宋简体", Font.PLAIN, fontSize));
w = g.getFontMetrics().stringWidth("欢迎来到Java Web图表编程的世界");
g.setColor(new Color(0, 65+i * 10, 0));
g.drawString("欢迎来到Java Web图表编程的世界", (width - w) / 2, ShiftSouth(y,
2 * i));
}
// 部署图形
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 + -