import java.awt.image.*,java.util.*,javax.imageio.*
好象是这些包楼主可能要自己试一下,不清楚对不对的
response.setContenType("image/jpeg")
// 在内存中创建图象,可以想象成一个画板大小是20mm长,60mm宽
int width=60, height=20
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
// 获取图形上下文,g想象成一个画笔
Graphics g = image.getGraphics()
//生成随机类
Random random = new Random()
//画线 x,y是坐标
g.drawLine(x,y,x+xl,y+yl)
//随即声成颜色
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)))
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(“画图”)在画版中画一个字符串,
}
还有画圆画方的很多方法,楼主可以试试
// 图象生效
g.dispose()
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream())
然后页面用<img src="servlet的url地址得到这幅画"/>
简单的可以用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对象
/**
Graphics g = image.getGraphics()
Graphics2D g2d = (Graphics2D)g
*/
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 = 1i <= 12i++)
{
// 绘制垂直方向虚线
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 = 0i <300i += 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 = 0i <bookTitle.lengthi++)
{
// 初始化绘制数据
int bookSales = 0
for (int j = 0j <sales.lengthj++)
{
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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)