📄 pear.jsp
字号:
<!--
Fig. 7.12_01: pear.jsp
功能: 基于Java2D的Web图表实例15: 梨子的画法,Area的布尔运算示例
-->
<%@ 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.*"
%>
<%
// 清空缓冲区
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);
Ellipse2D.Double circle, oval, leaf, stem;
Area circ, ov, leaf1, leaf2, st1, st2;
circle = new Ellipse2D.Double();
oval = new Ellipse2D.Double();
leaf = new Ellipse2D.Double();
stem = new Ellipse2D.Double();
circ = new Area(circle);
ov = new Area(oval);
leaf1 = new Area(leaf);
leaf2 = new Area(leaf);
st1 = new Area(stem);
st2 = new Area(stem);
double x = 100 , y = 100;
// 绘制第一片叶子
leaf.setFrame(x - 16, y - 29, 15.0, 15.0);
leaf1 = new Area(leaf);
leaf.setFrame(x - 14, y - 47, 30.0, 30.0);
leaf2 = new Area(leaf);
leaf1.intersect(leaf2);
g2d.setColor(Color.GREEN);
g2d.fill(leaf1);
// 绘制第二片叶子
leaf.setFrame(x + 1, y - 29, 15.0, 15.0);
leaf1 = new Area(leaf);
leaf2.intersect(leaf1);
g2d.fill(leaf2);
// 绘制茎
stem.setFrame(x, y - 42, 40.0, 40.0);
st1 = new Area(stem);
stem.setFrame(x + 3, y - 47, 50.0, 50.0);
st2 = new Area(stem);
st1.subtract(st2);
g2d.setColor(Color.BLACK);
g2d.fill(st1);
// 绘制梨子主体部分
circle.setFrame(x - 25, y , 50.0, 50.0);
oval.setFrame(x - 19, y - 20, 40.0, 70.0);
circ = new Area(circle);
ov = new Area(oval);
circ.add(ov);
g2d.setColor(Color.YELLOW);
g2d.fill(circ);
// 部署图形
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 + -