|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
/** * jeruGraphics v 1.0 * * 看到一些动态生成图象的例子都是servlet完成的, * 而且程序很长,觉得不是无论从实用性还是可读性来说都不是太好。 * 这里给了段代码,命令行生成图象文件。这样是不是简朴易用些呢? * * 创建一个 BufferedImage 对象,将你的“画”放到这个缓冲里, * 再打开一个文件,将图像流编码后输入这个文件,这样就有一个 * jpg文件出现了,试试吧。。。 * * Mender : * Jeru Liu * Homepage : * http://javaren.126.com * Email: jeru@163.net * * 这仅仅是一个范例程序,没什么实用,却极具参考价值。 * */ import java.io.*; import java.util.*; import com.sun.image.codec.jpeg.*; import java.awt.image.*; import java.awt.*; public class jeruGraphics { BufferedImage image; // 创建 jpg 文件到指定路径下 public void createJpg(String path) { try { FileOutputStream fos = new FileOutputStream(path); BufferedOutputStream bos = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); encoder.encode(image); bos.close(); } catch(FileNotFoundException fnfe) { System.out.println(fnfe); } catch(IOException ioe) { System.out.println(ioe); } } public static void main(String[] args) { int width=400, height=200; int xLength=300, yLength=150; int count=5; Vector data=new Vector(); data.addElement(new Integer(100)); data.addElement(new Integer(120)); data.addElement(new Integer(150)); data.addElement(new Integer(40)); data.addElement(new Integer(5)); jeruGraphics jg = new jeruGraphics(); jg.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = jg.image.getGraphics(); // 画坐标 g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setColor(Color.blue); g.drawLine(10,height-10,10,height-10-yLength); g.drawLine(10,height-10,10+xLength,height-10); // 连线 int yTo; int yFrom = ((Integer)(data.elementAt(0))).intValue(); for (int i=1; i<count; i++) { yTo=((Integer)(data.elementAt(i))).intValue(); g.drawLine(10+i*xLength/count,height-10,10+i*xLength/count,height-15); g.drawLine(10+(i-1)*xLength/count,yFrom,10+i*xLength/count,yTo); yFrom=yTo; } jg.createJpg("d://aaa.jpg"); } } 返回类别: 教程 上一教程: 给你一个例子:FileFilter接口的使用。 下一教程: Java程序编码规范 您可以阅读与"好东西介绍,直接用 java 命令行动态生成jpg文件"相关的教程: · JSP调用JAVABEAN在网页上动态生成柱状图 · 使用Java Servlet动态生成图片 · 在服务器端动态生成gif or jpg示例 · 怎样使用JAVA SERVLET动态生成图片详解 · JAVA生成JPG缩略图 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |