问题描述
如题!但是不需要在网页上运行!只要在电脑里直接打开就可以!在线等!求不要出现webservlet了...
解决方案
解决方案二:
/****/importjava.awt.*;importjava.awt.image.*;importjava.util.*;importjavax.swing.ImageIcon;importjavax.swing.JFrame;importjavax.swing.JLabel;publicclassImageCode{publicStringsRand="";publicStringsgbRand="";publicColorgetRandColor(intfc,intbc){//给定范围获得随机颜色Randomrandom=newRandom();if(fc>255)fc=255;if(bc>255)bc=255;intr=fc+random.nextInt(bc-fc);intg=fc+random.nextInt(bc-fc);intb=fc+random.nextInt(bc-fc);returnnewColor(r,g,b);}publicBufferedImagecreatImage(){//在内存中创建图象intwidth=60,height=20;BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//获取图形上下文Graphicsg=image.getGraphics();//生成随机类Randomrandom=newRandom();//设定背景色g.setColor(getRandColor(100,250));g.fillRect(0,0,width,height);//设定字体g.setFont(newFont("ComicSansMS",Font.PLAIN,18));//画边框//g.setColor(newColor(0,0,0));//g.drawRect(0,0,width-1,height-1);//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到g.setColor(getRandColor(160,200));for(inti=0;i<155;i++){intx=random.nextInt(width);inty=random.nextInt(height);intxl=random.nextInt(12);intyl=random.nextInt(12);g.drawLine(x,y,x+xl,y+yl);}//取随机产生的认证码(4位数字)for(inti=0;i<4;i++){Stringrand=String.valueOf(random.nextInt(10));sRand+=rand;//将验证码显示到图象中g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));g.drawString(rand,13*i+6,16);}//图象生效g.dispose();returnimage;}publicstaticvoidmain(String[]args){ImageCodeimg=newImageCode();JFramejf=newJFrame();jf.setSize(200,300);Containerct=jf.getContentPane();ImageIconii=newImageIcon(img.creatImage());JLabeljl=newJLabel(ii);ct.add(jl);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setVisible(true);}}
解决方案三:
引用1楼huxiweng的回复:
/****/importjava.awt.*;importjava.awt.image.*;importjava.util.*;importjavax.swing.ImageIcon;importjavax.swing.JFrame;importjavax.swing.JLabel;publicclassImageCode{publicStringsRand="";publicStringsgbRand="";publicColorgetRandColor(intfc,intbc){//给定范围获得随机颜色Randomrandom=newRandom();if(fc>255)fc=255;if(bc>255)bc=255;intr=fc+random.nextInt(bc-fc);intg=fc+random.nextInt(bc-fc);intb=fc+random.nextInt(bc-fc);returnnewColor(r,g,b);}publicBufferedImagecreatImage(){//在内存中创建图象intwidth=60,height=20;BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//获取图形上下文Graphicsg=image.getGraphics();//生成随机类Randomrandom=newRandom();//设定背景色g.setColor(getRandColor(100,250));g.fillRect(0,0,width,height);//设定字体g.setFont(newFont("ComicSansMS",Font.PLAIN,18));//画边框//g.setColor(newColor(0,0,0));//g.drawRect(0,0,width-1,height-1);//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到g.setColor(getRandColor(160,200));for(inti=0;i<155;i++){intx=random.nextInt(width);inty=random.nextInt(height);intxl=random.nextInt(12);intyl=random.nextInt(12);g.drawLine(x,y,x+xl,y+yl);}//取随机产生的认证码(4位数字)for(inti=0;i<4;i++){Stringrand=String.valueOf(random.nextInt(10));sRand+=rand;//将验证码显示到图象中g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));g.drawString(rand,13*i+6,16);}//图象生效g.dispose();returnimage;}publicstaticvoidmain(String[]args){ImageCodeimg=newImageCode();JFramejf=newJFrame();jf.setSize(200,300);Containerct=jf.getContentPane();ImageIconii=newImageIcon(img.creatImage());JLabeljl=newJLabel(ii);ct.add(jl);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);jf.setVisible(true);}}
想在这个主函数里加个输入框怎么办啊求代码
解决方案四:
Servlet生成验证码http://www.cppblog.com/biao/archive/2010/12/28/137589.htmlWeb也是生成一个BufferedImage,然后写到Response里,把这段代码提取出来就可以了