📄 vbar3dchart_nogood.jsp
字号:
<!--
Fig. 7.5_04_04: vBar3DChart_NoGood.jsp
功能: 因绘制顺序不同而产生的绘制结果
-->
<%@ 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.*"
import="com.fatcat.webchart.*"
%>
<%!
// 定义绘制斜线的方法
public void drawSlantLine(double startPtX, double startPtY,
double thickness, double alpha, Graphics2D g2d)
{
Point2D.Double newStartPoint =
getPoint2DPoint(startPtX, startPtY, thickness, alpha);
Point2D.Double newEndPoint = new Point2D.Double(startPtX, startPtY);
Line2D.Double slantLine = new Line2D.Double(newStartPoint, newEndPoint);
g2d.draw(slantLine);
}
// 定义绘制垂直直线的方法
public void drawVerticalLine(double startPtX, double startPtY,
double thickness, double alpha, double length, Graphics2D g2d)
{
Point2D.Double newStartPoint =
getPoint2DPoint(startPtX, startPtY, thickness, alpha);
Point2D.Double newEndPoint =
new Point2D.Double(newStartPoint.getX(), newStartPoint.getY() - length);
Line2D.Double verticalLine = new Line2D.Double(newStartPoint, newEndPoint);
g2d.draw(verticalLine);
}
// 定义绘制水平直线的方法
public void drawHorizontalLine(double startPtX, double startPtY,
double thickness, double alpha, double length, Graphics2D g2d)
{
Point2D.Double newStartPoint =
getPoint2DPoint(startPtX, startPtY, thickness, alpha);
Point2D.Double newEndPoint =
new Point2D.Double(newStartPoint.getX() + length, newStartPoint.getY());
Line2D.Double horizontalLine =
new Line2D.Double(newStartPoint, newEndPoint);
g2d.draw(horizontalLine);
}
// 定义获取水平偏移量的方法
public double getOffsetX(double thickness, double alpha)
{
return thickness * Math.cos(alpha * Math.PI / 180);
}
// 定义获取垂直偏移量的方法
public double getOffsetY(double thickness, double alpha)
{
return thickness * Math.sin(alpha * Math.PI / 180);
}
// 定义创建新的Point2D.Double对象的方法
public Point2D.Double getPoint2DPoint(double startPtX, double startPtY,
double thickness, double alpha)
{
double newStartPtX = startPtX + getOffsetX(thickness, alpha);
double newStartPtY = startPtY - getOffsetY(thickness, alpha);
Point2D.Double point = new Point2D.Double(newStartPtX, newStartPtY);
return point;
}
%>
<%
// 清空缓冲区
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.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
// 绘制阴影,由灰色渐进圆角矩形组成
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY,
width, height, new Color(218, 214, 212), false);
g2d.setPaint(grayGP);
RoundRectangle2D.Float bgRR =
new RoundRectangle2D.Float(5, 5, width-5, height-5 , 50, 50);
g2d.fill(bgRR);
// 绘制渐进蓝色圆角矩形背景
GradientPaint blueGP = new GradientPaint(0, 0, new Color(14, 97, 147),
0, height, new Color(240, 243, 247), false);
g2d.setPaint(blueGP);
g2d.fillRoundRect(0, 0, width - 5, height - 5, 50, 50);
// 绘制深蓝色圆角矩形轮廓
BasicStroke bs = new BasicStroke(1.2f);
g2d.setStroke(bs);
g2d.setPaint(new Color(55,71, 105));
g2d.drawRoundRect(0, 0, width -5, height -5, 50, 50);
// 定义3D坐标轴厚度及夹角度数
int thickness = 10;
int alpha = 45;
// 创建图表绘图区域
Rectangle2D.Double drawArea = new Rectangle2D.Double();
// 定义图表绘制区域的大小
int areaWidth = 400, areaHeight = 300;
// 定义图表绘制区域的右下方基准点坐标
int basePointX = 60, basePointY = 345;
// 定义图表绘制区左上角坐标
int topLeftPointX = basePointX , topLeftPointY = basePointY - areaHeight;
// 填充图表绘图区域
g2d.setPaint(Color.WHITE);
drawArea =
new Rectangle2D.Double(topLeftPointX, topLeftPointY, areaWidth, areaHeight );
drawArea.setRect(topLeftPointX + thickness * Math.cos(alpha * Math.PI / 180),
topLeftPointY - thickness * Math.sin(alpha * Math.PI / 180),
areaWidth, areaHeight );
g2d.fill(drawArea);
// 创建DrawParallelogram类的实例
DrawParallelogram parallelogram = new DrawParallelogram();
// 绘制基准点右边水平方向的平行四边形
parallelogram.setFillColor(new Color(252, 198, 112));
parallelogram.setBasePoint(basePointX, basePointY );
parallelogram.setWidth(areaWidth);
parallelogram.setHeight(0);
parallelogram.setThickness(thickness);
parallelogram.setAngle(alpha);
parallelogram.drawParallelogram(2, g2d);
parallelogram.setOutlineColor(Color.BLACK);
parallelogram.drawParallelogram(1, g2d);
// 绘制基准点上方垂直方向的平行四边形
parallelogram.setPRightFillColor(new Color(252, 198, 112));
parallelogram.setWidth(0);
parallelogram.setHeight(areaHeight);
parallelogram.setThickness(thickness);
parallelogram.setAngle(alpha);
parallelogram.drawParallelogram(2, g2d);
parallelogram.setOutlineColor(Color.BLACK);
parallelogram.drawParallelogram(1, g2d);
// 描绘图表绘图区域边框
g2d.setPaint(Color.BLACK);
g2d.draw(drawArea);
// 绘制图表标题
String chartTitle = "计算机编程类图书2004年月销售量3D统计图";
g2d.setFont(new Font("华文隶书", Font.PLAIN, 22));
int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
g2d.setColor(Color.WHITE);
g2d.drawString(chartTitle, (width - stringLength) / 2, 25 );
// 创建虚线笔划
float[] dashes = { 3.f };
bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
10, dashes, 0);
g2d.setStroke(bs);
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
String str = "";
stringLength = 0;
g2d.setPaint(Color.BLACK);
g2d.setFont(new Font("宋体", Font.PLAIN, 12));
for (int i = 0; i <= 12; i++)
{
// 绘制垂直方向虚线
drawVerticalLine(basePointX+i * areaWidth/13, basePointY ,
thickness, alpha, areaHeight , g2d);
// 绘制横轴上的斜线
drawSlantLine(basePointX+i * areaWidth/13, basePointY ,
thickness, alpha, g2d);
// 绘制横轴上月份的说明文字
if (i != 0)
{
str += i + "月";
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, topLeftPointX + i * areaWidth/13 - stringLength/2, 360);
}
// 重置月份说明文字
str = "";
}
g2d.setFont(new Font("Arial", Font.BOLD, 14));
str = "";
int stringHeight = 0;
for (int i = 0; i <= areaHeight ; i += 30)
{
// 绘制水平方向虚线
drawHorizontalLine(topLeftPointX, topLeftPointY+i, thickness, alpha, areaWidth, g2d);
// 绘制纵轴上的斜线
drawSlantLine(topLeftPointX, topLeftPointY+i, thickness, alpha, g2d);
// 绘制纵轴上销售量的说明文字
str += 100-i / 3;
stringHeight = g2d.getFontMetrics().getAscent();
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 55-stringLength, topLeftPointY + i + stringHeight / 2);
// 重置销售量说明文字
str = "";
}
String[] bookTitle = { "JAVA", "C#" };
Color[] bookColor = {new Color(230,111, 71) , new Color(107,165,239)};
double[] sales = new double[12];
int[] month = new int[12];
double bookSales = 0.0;
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
g2d.setStroke(new BasicStroke());
for (int i = 0; i < bookTitle.length; i++)
{
for (int j = 0; j < sales.length; j++)
{
bookSales = 1 + Math.random() * (areaHeight - 5);
sales[j] = basePointY - bookSales ;
month[j] = topLeftPointX + ((j+1) * areaWidth)/13;
// 初始化3D矩形的绘制参数
parallelogram.setBasePoint(month[j]-10 + i*10, basePointY);
parallelogram.setWidth(10);
parallelogram.setHeight(basePointY - (int)sales[j]);
parallelogram.setThickness(thickness);
parallelogram.setAngle(alpha);
// 填充3D矩形
parallelogram.setFillColor(bookColor[i]);
parallelogram.drawParallelogram(2, g2d);
// 描绘3D矩形轮廓
parallelogram.setOutlineColor(Color.BLACK);
parallelogram.drawParallelogram(1, g2d);
}
// 绘制图例
g2d.setColor(bookColor[i]);
g2d.fillRect(425, 40+i * 12, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(bookTitle[i], 438, 50+i * 12);
}
// 部署图形
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 + -