概述
BufferedImageimage=newBufferedImage(Constants.IMAGE_WIDTH,Constants.IMAGE_HEIGHT,BufferedImage.TYPE_INT_RGB);Graphicsg=image.getGraphics();g.setColor(getRandColor());g.fi...
BufferedImage image = new BufferedImage(Constants.IMAGE_WIDTH,
Constants.IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(getRandColor());
g.fillRect(0,0,Constants.IMAGE_WIDTH,Constants.IMAGE_HEIGHT);
for(int i=0;i<4;i++)
drawCode(g,i);
System.out.println("生成的验证码:"+codeNumbers);
codeNumbers="";
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "GIF", sos);
sos.close();
}
private void drawCode(Graphics graphics, int i) {
Random random=new Random();
Integer j=random.nextInt((Constants.IMAGE_CHAR.length()));
String number= Constants.IMAGE_CHAR.substring(j,j+1);
graphics.setFont(Constants.codeFont[i]);
graphics.setColor(Constants.color[i]);
graphics.drawString(number, 20 + i * 20, 30);
codeNumbers+=number;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
private Color getRandColor(){
Random random=new Random();
int i=random.nextInt(155);
int g=random.nextInt(134);
int h=random.nextInt(142);
return new Color(i,g,h, 0);
}
展开
最后
以上就是彪壮火车为你收集整理的setcolor ()java_类型 Graphics 中的方法 setColor(java.awt.Color)对于参数不适用的全部内容,希望文章能够帮你解决setcolor ()java_类型 Graphics 中的方法 setColor(java.awt.Color)对于参数不适用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复