超级连接做的,贴图就是那样,他是用很多张图组成的,随机变换!刷新就可以!
其实楼主所提问的回贴就变的图,现在在论坛里很吃香的。回贴就变,即使你不回复,每刷新一次页面那副图都会变,那为什么会这样呢?其实用Servlet来实现这种效果是非常简单的。看看下面的代码:
import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream; import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class CreateImage extends HttpServlet implements Servlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); String imagePath=request.getSession().getServletContext().getRealPath("images")+"\\"+getRandom()+".jpg"; InputStream imageIn = new FileInputStream(new File(imagePath)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } public String getRandom(){ int i1 = (int)(java.lang.Math.random()*10); int i2 = (int)(java.lang.Math.random()*10); return String.valueOf(i1)+String.valueOf(i2); }}
在Images目录下放入100个从00-99命名的jpg文件,然后你在页面中使用
当然有时我们可能会想在图片上动态加入一些文字,那应该怎么,其实也很简单,就是利用Graphics这个类来对图片内容进行修改,如下:
import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;
import javax.servlet.Servlet;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageDecoder;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CreateImage extends HttpServlet implements Servlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); String imagePath=request.getSession().getServletContext().getRealPath("images")+"\\01.jpg"; InputStream imageIn = new FileInputStream(new File(imagePath)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); Graphics g = image.getGraphics(); g.setColor(new Color(255,0,0)); //更改输出字体的颜色 g.setFont(new Font("Default",Font.BOLD,30)); //更改字体大小 g.drawString("Hello",30,100); //在图片上坐标0,13的位置上输出"Hello everyone!" JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); }
明白了吗?