📄 texturedpaint.jsp
字号:
<!--
Fig. 7.3_02: texturedPaint.jsp
功能: Java2D的纹理填充
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.*"
import="javax.imageio.*"
import="java.awt.geom.*"
import="java.awt.image.*"
import="java.io.*"
%>
<%
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 创建一个 320X400 的图像
int width = 320, height = 400;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 创建Java2D对象
Graphics2D g2d = (Graphics2D)g;
// 创建纹理背景图像
BufferedImage bg = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_RGB);
Graphics2D bg2d = bg.createGraphics();
bg2d.setColor(Color.YELLOW);
bg2d.fillRect(0, 0, 10, 10);
bg2d.setColor(Color.RED);
bg2d.fillRect(1, 1, 6, 6);
bg2d.setColor(Color.BLUE);
bg2d.fillRect(2, 2, 4, 4);
bg2d.setColor(Color.BLACK);
bg2d.fillRect(4, 4, 3, 3);
// 创建纹理填充对象
TexturePaint tp1 = new TexturePaint(bg, new Rectangle(10,10));
g2d.setPaint(tp1);
g2d.fillRect(0, 0, width, height);
// 加载图像文件
File jpgFile =
new File(getServletContext().getRealPath("javaLogo.jpg"));
BufferedImage im = ImageIO.read(jpgFile);
// 创建纹理填充对象
TexturePaint tp2 =
new TexturePaint(im, new Rectangle(im.getWidth(),im.getHeight()));
// 填充椭圆
g2d.setPaint(tp2);
g2d.fillOval(50, 50, width -100, height-100);
// 部署图形
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 + -