67,538
社区成员
发帖
与我相关
我的任务
分享
public class RandomPic extends TagSupport
{
@Override
public int doEndTag() throws JspException
{
int width=100, height=18, r1, g1, b1;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
r1=200+random.nextInt(50);
g1=200+random.nextInt(50);
b1=200+random.nextInt(50);
g.setColor(new Color(r1, g1, b1));
g.fillRect(1, 1, width-1, height-1);
g.setColor(new Color(102,102,102));
g.drawRect(0, 0, width-1, height-1);
r1=160+random.nextInt(40);
g1=160+random.nextInt(40);
b1=160+random.nextInt(40);
g.setColor(new Color(r1, g1, b1));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x,y,x + xl,y + yl);
}
for (int i = 0;i < 70;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x,y,x - xl,y - yl);
}
String sRand="";
for (int i=0;i<6;i++)
{
String tmp="";
long itmp = 0;
char ctmp = '\u0000';
switch ((int)Math.round(Math.random() * 2))
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char)itmp;
tmp = String.valueOf(ctmp);
break;
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char)itmp;
tmp = String.valueOf(ctmp);
break;
default :
itmp = Math.round(Math.random() * 9);
tmp = String.valueOf(itmp);
}
sRand += tmp;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(tmp,15*i+10,15);
}
g.dispose();
//已经配置好tld了。应该这样直接输出图片?前提是图片不用保存在服务器上。
/**
* <Random:RandomPic/>
* 上面一句会输出一张图片,
*
*
*/
//ImageIO.write(image, "jpeg", pageContext.getOut());
return EVAL_PAGE;
}