熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

一個圖形計數器的例子

2013-11-15 12:05:46  來源: JSP教程 

  import javaio*;
import javautil*;
import comsunimagecodecjpeg*;
import javaxservlet*;
import javaxservlet*;

import javaawt*;
import javaawtimage*;
public class JPEGServlet extends HttpServlet {

private String strFile="c:counttxt";
private Font mFont=new Font("Courier" FontBOLD);;

public void doGet(HttpServletRequest requestHttpServletResponse response)
throws ServletExceptionIOException {

String s;
int intCount;
s=getCount();
intCount=new Integer(s)intValue();
intCount=intCount+;
writeCount(intCount+"");

responsesetContentType("image/jpeg");
ServletOutputStream out=responsegetOutputStream();
BufferedImage image=new BufferedImage(BufferedImageTYPE_INT_RGB);
Graphics g=imagegetGraphics();
gsetColor(Colorblack);
gfillRect();
gsetColor(Colorwhite);
gsetFont(mFont);

char c;
for(int i=;i<;i++) {
c=scharAt(i);
gdrawString(c+""i*+);
gdrawLine((i+)*(i+)*);
}

JPEGImageEncoder encoder=JPEGCodeccreateJPEGEncoder(out);
encoderencode(image);
outclose();
}

public void doPost(HttpServletRequest requestHttpServletResponse response)
throws ServletExceptionIOException {

doGet(requestresponse);
}

public synchronized void writeCount(String content) {
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(strFile));
pwprintln(content);
pwclose();
} catch(IOException e) {
Systemoutprintln("Write file error:"+egetMessage());
}
}

public synchronized String getCount() {
String strCount;
int len;
int leftLen;

try {
BufferedReader inf = new BufferedReader(new FileReader(strFile));
strCount=infreadLine();
infclose();
} catch(Exception e) {
Systemoutprintln("Get counter error:"+egetMessage());
return reChar();
}
len=strCountlength();
if (len<=) {
return reChar();
}

if (len>) {
return reChar();
}

leftLen=len;
return reChar(leftLen)+strCount;
}

private String reChar(char cint num){
int i;
if (num<=) return "";
String s="";
for(i=;i<num;i++) {
s=s+c;
}
return s;
}
}
 


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20276.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.