永发信息网

使用JSP怎么在网页中绘图?

答案:3  悬赏:0  手机版
解决时间 2021-01-19 03:04
使用JSP怎么在网页中绘图?
最佳答案
response.setContentType("image/png");

// 创建一个 610x400 的图像
int width = 610, height = 400;

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);

// 绘制新背景
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height,
new Color(200, 200, 200), false);
g2d.setPaint(grayGP);
g2d.fillRoundRect(20, 20, width - 20, height - 20, 50, 50);

g2d.setPaint(Color.WHITE);
g2d.fillRoundRect(12, 12, width - 20, height - 20, 50, 50);

BasicStroke bs = new BasicStroke(4.0f);
g2d.setStroke(bs);
g2d.setPaint(new Color(53, 76, 112));
g2d.drawRoundRect(12, 12, width - 20, height - 20, 50, 50);

GradientPaint blueGP = new GradientPaint(120, 60, new Color(215, 230, 252),
120, 300, Color.WHITE, false);
g2d.setPaint(blueGP);
g2d.fillRect(120, 60, 440, 300);

// 绘制图表标题
String chartTitle = "计算机编程类图书2004年月销售量统计图";
g2d.setFont(new Font("方正粗宋简体", Font.PLAIN, 22));
g2d.setColor(Color.BLACK);
g2d.drawString(chartTitle, 140, 40);

// 创建虚线笔划
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 = "2004-";
int stringLength = 0;

for (int i = 1; i <= 12; i++)
{
// 绘制垂直方向虚线
g2d.drawLine(80+i * 40, 50, 80+i * 40, 360);

// 绘制横轴上月份的说明文字
str += i;
stringLength = g2d.getFontMetrics().stringWidth(str);
if (i % 2 == 0)
{
g2d.drawString(str, 80+i * 40 - stringLength / 2, 387);
}
else
{
g2d.drawString(str, 80+i * 40 - stringLength / 2, 375);
}
str = "2004-";
}

str = "";
int stringHeight = 0;

for (int i = 0; i < 300; i += 30)
{
// 绘制水平方向虚线
g2d.drawLine(120, 60+i, 570, 60+i);

// 绘制纵轴上销售量的说明文字
str += 100-i / 3;
stringHeight = g2d.getFontMetrics().getAscent();
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 110-stringLength, 60+i + stringHeight / 2);
str = "";
}

// 绘制坐标轴
g2d.setStroke(new BasicStroke(3.0f));
g2d.setColor(new Color(53, 76, 112));
g2d.drawLine(120, 50, 120, 360);
g2d.drawLine(120, 360, 570, 360);

// 绘制纵坐标上的标题
g2d.setFont(new Font("黑体", Font.PLAIN, 15));
g2d.drawString("月销售量", 40, 45);

// 调用TriangleServlet类,绘制坐标轴上的箭头
TriangleServlet ts = new TriangleServlet();
ts.setFillColor(new Color(53, 76, 112));
ts.setBaseLine(10);
ts.setAlpha(60);
ts.drawTrigangle(570, 360, 2, 2, g2d); // 绘制横坐标轴上的箭头
ts.drawTrigangle(120, 50, 1, 2, g2d); // 绘制纵坐标轴上的箭头

String[] bookTitle = { "JAVA", "C#" };
Color[] bookColor = { Color.RED, Color.ORANGE };
int[] sales = new int[12];
int[] month = new int[12];

g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
for (int i = 0; i < bookTitle.length; i++)
{
// 初始化绘制数据
int bookSales = 0;
for (int j = 0; j < sales.length; j++)
{
bookSales = 45+(int)(Math.random() * 50);
sales[j] = 360-bookSales * 3;
month[j] = 120+j * 40;
}

// 重新设置笔划
g2d.setStroke(new BasicStroke(5.0f));
g2d.setColor(bookColor[i]);

// 绘制月销售量折线
g2d.drawPolyline(month, sales, sales.length);

// 绘制图例
g2d.fillRect(30, 140+i * 20, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(bookTitle[i], 45, 150+i * 20);
}

// 部署图形
g2d.dispose();

// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
全部回答
可以使用Jfreechart.jar组件
简单的可以用java.awt.geom java.awt.image 这两个包
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 创建一个 610x400 的图像
int width = 610, height = 400;
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);
// 绘制新背景
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height,
new Color(200, 200, 200), false);
g2d.setPaint(grayGP);
g2d.fillRoundRect(20, 20, width - 20, height - 20, 50, 50);
g2d.setPaint(Color.WHITE);
g2d.fillRoundRect(12, 12, width - 20, height - 20, 50, 50);
BasicStroke bs = new BasicStroke(4.0f);
g2d.setStroke(bs);
g2d.setPaint(new Color(53, 76, 112));
g2d.drawRoundRect(12, 12, width - 20, height - 20, 50, 50);
GradientPaint blueGP = new GradientPaint(120, 60, new Color(215, 230, 252),
120, 300, Color.WHITE, false);
g2d.setPaint(blueGP);
g2d.fillRect(120, 60, 440, 300);
// 绘制图表标题
String chartTitle = "计算机编程类图书2004年月销售量统计图";
g2d.setFont(new Font("方正粗宋简体", Font.PLAIN, 22));
g2d.setColor(Color.BLACK);
g2d.drawString(chartTitle, 140, 40);
// 创建虚线笔划
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 = "2004-";
int stringLength = 0;
for (int i = 1; i <= 12; i++)
{
// 绘制垂直方向虚线
g2d.drawLine(80+i * 40, 50, 80+i * 40, 360);
// 绘制横轴上月份的说明文字
str += i;
stringLength = g2d.getFontMetrics().stringWidth(str);
if (i % 2 == 0)
{
g2d.drawString(str, 80+i * 40 - stringLength / 2, 387);
}
else
{
g2d.drawString(str, 80+i * 40 - stringLength / 2, 375);
}
str = "2004-";
}
str = "";
int stringHeight = 0;
for (int i = 0; i < 300; i += 30)
{
// 绘制水平方向虚线
g2d.drawLine(120, 60+i, 570, 60+i);
// 绘制纵轴上销售量的说明文字
str += 100-i / 3;
stringHeight = g2d.getFontMetrics().getAscent();
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 110-stringLength, 60+i + stringHeight / 2);
str = "";
}
// 绘制坐标轴
g2d.setStroke(new BasicStroke(3.0f));
g2d.setColor(new Color(53, 76, 112));
g2d.drawLine(120, 50, 120, 360);
g2d.drawLine(120, 360, 570, 360);
// 绘制纵坐标上的标题
g2d.setFont(new Font("黑体", Font.PLAIN, 15));
g2d.drawString("月销售量", 40, 45);
// 调用TriangleServlet类,绘制坐标轴上的箭头
TriangleServlet ts = new TriangleServlet();
ts.setFillColor(new Color(53, 76, 112));
ts.setBaseLine(10);
ts.setAlpha(60);
ts.drawTrigangle(570, 360, 2, 2, g2d); // 绘制横坐标轴上的箭头
ts.drawTrigangle(120, 50, 1, 2, g2d); // 绘制纵坐标轴上的箭头
String[] bookTitle = { "JAVA", "C#" };
Color[] bookColor = { Color.RED, Color.ORANGE };
int[] sales = new int[12];
int[] month = new int[12];
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
for (int i = 0; i < bookTitle.length; i++)
{
// 初始化绘制数据
int bookSales = 0;
for (int j = 0; j < sales.length; j++)
{
bookSales = 45+(int)(Math.random() * 50);
sales[j] = 360-bookSales * 3;
month[j] = 120+j * 40;
}
// 重新设置笔划
g2d.setStroke(new BasicStroke(5.0f));
g2d.setColor(bookColor[i]);
// 绘制月销售量折线
g2d.drawPolyline(month, sales, sales.length);
// 绘制图例
g2d.fillRect(30, 140+i * 20, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(bookTitle[i], 45, 150+i * 20);
}
// 部署图形
g2d.dispose();
// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
偶滴歌神啊丁当那期浩浩和嘉宾唱的是什么歌
息子母亲五十路,五十路中出母,日语息子什么
生姜末洗发水,防脱生发
简述可能性与现实性的关系及意义
转眼间34年过去了变换语序
always的al和talk的al发音相同吗?
螳螂怎么打末日.求方法,求大神
05年的大河剧《国盗物语》里明智光秀临死的时
怎样办理外国人持外国驾照申领中国驾照
4.72除以10.6除以3.2及减3.6的简便计算是
荷叶跟什么泡水最减肥
怎么验算59除以6
从传统工厂到智能工厂,还有哪些路要走
怎样自己泡萝卜皮好吃
德语问题 hierher 和hierhin 有什么区别
推荐资讯
蓝梦女子沙龙地址在哪,我要去那里办事,
立春,古人有哪些食俗
情人约我去开房,什么时间最安全,要注意那方面
浴缸去了,地面该怎么弄
《被嫌弃的松子一生》中松子的一生为什么会变
开车去农村照相,生意不好有什么办法。让我生
哈尼和雷霆分手了吗
While college graduates a lack of availab
冷草 暖草 区别
梦见湿手和几个老总握手
眼镜属于哪个行业?
EVE A 小航刷怪
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?