cell

给大家一个web开发的示例:
Java code
   
   
public class CellService { private static final int CELL_WIDTH = 6 ; public static final long DELAY_SEC = 1L ; // 延时刷新秒数 private static Map < String,BufferedImage > map = new HashMap < String,BufferedImage > (); private static Map < String, int [][] > dataMap = new HashMap < String, int [][] > (); private static CellService instance = null ; private int width; private int height; private CellService(){ this .width = 200 ; this .height = 180 ; } public static CellService getInstance(){ if (instance == null ){ instance = new CellService(); } return instance; } public BufferedImage draw( int seeds,String key){ String mapKey = seeds + key; BufferedImage bi = null ; if ( ! map.containsKey(mapKey)){ bi = drawImage(seeds,key); } else { bi = map.get(mapKey); } return bi; } public void draw(OutputStream out, int seeds, String key){ int w = this .width / CELL_WIDTH; int h = this .height / CELL_WIDTH; if (seeds > ( int )(w * h * 0.9 )){ seeds = ( int )(w * h * 0.9 ); } else if (seeds < ( int )(w * h * 0.1 )){ seeds = ( int )(w * h * 0.1 ); } try { ImageIO.write(drawImage(seeds,key), " jpeg " , out); } catch (IOException e) { e.printStackTrace(); } } private synchronized BufferedImage drawImage( int seed, String key){ BufferedImage bi = new BufferedImage( this .width, this .height,BufferedImage.TYPE_INT_RGB); Graphics g2 = bi.createGraphics(); int [][] imageData = null ; int [][] tmpImageData = null ; if ( ! map.containsKey(seed + key)){ imageData = new int [ this .width / CELL_WIDTH][ this .height / CELL_WIDTH]; tmpImageData = randomData(seed); } else { imageData = dataMap.get(seed + key); tmpImageData = generateData(imageData); } dataMap.put(seed + key, tmpImageData); drawCells(g2,tmpImageData); g2.dispose(); bi.flush(); map.put(seed + key, bi); return bi; } private void drawCells(Graphics g2, int [][] imageData){ g2.clearRect( 0 , 0 , this .width, this .height); int row = this .width / CELL_WIDTH; int col = this .height / CELL_WIDTH; for ( int i = 0 ; i < row; i ++ ){ for ( int j = 0 ; j < col; j ++ ){ if (imageData[i][j] == 1 ){ g2.setColor(Color.red); } else { g2.setColor(Color.blue); } g2.fillRect(i * CELL_WIDTH, j * CELL_WIDTH, (i + 1 ) * CELL_WIDTH, (j + 1 ) * CELL_WIDTH); } } // 画边框线 g2.setColor(Color.white); for ( int i = 0 ; i < col; i ++ ){ g2.drawLine( 0 , i * CELL_WIDTH, row * CELL_WIDTH, i * CELL_WIDTH); } for ( int j = 0 ; j <= row; j ++ ){ g2.drawLine(j * CELL_WIDTH, 0 , j * CELL_WIDTH, col * CELL_WIDTH); } } private int [][] randomData( int seeds){ int w = this .width / CELL_WIDTH; int h = this .height / CELL_WIDTH; int [][] result = new int [w][h]; int [] tmp = randomNum(seeds,w * h - 1 ); for ( int i = 0 ; i < w; i ++ ){ mid: for ( int j = 0 ; j < h; j ++ ){ for ( int k = 0 ; k < tmp.length; k ++ ){ if (tmp[k] == i * h + j){ result[i][j] = 1 ; continue mid; } } } } return result; } /** * cell生长 * 根据生存法则重新生成数据[最多3*3方格] * 1,"1"周围最多只有1个"1"时,该"Cell因孤独死去",变成"0" * 2,"1"周围有4-8个"1"时,该"Cell因拥挤死去",变成"0" * 3,"1"周围有2-3个"1"时,该"Cell生存",数据不变 * 4,"0"周围有3个"1"时,"生出一个Cell",数据变成1 * @param imageData * @return */ private int [][] generateData( int [][] imageData){ int w = imageData.length; int h = imageData[ 0 ].length; int [][] tmp = new int [w][h]; for ( int i = 0 ; i < w; i ++ ){ for ( int j = 0 ; j < h; j ++ ){ int sum = - 1 ; if (i == 0 ){ // if (j == 0 ){ // 左上角 sum = imageData[i][j + 1 ] + imageData[i + 1 ][j + 1 ] + imageData[i + 1 ][j]; } else if (j == h - 1 ){ // 右上角 sum = imageData[i][j - 1 ] + imageData[i + 1 ][j - 1 ] + imageData[i + 1 ][j]; } else { // 上面 sum = imageData[i][j - 1 ] + imageData[i + 1 ][j - 1 ] + imageData[i + 1 ][j] + imageData[i + 1 ][j + 1 ] + imageData[i][j + 1 ]; } } else if (i == w - 1 ){ // if (j == 0 ){ // 左下 sum = imageData[i - 1 ][j] + imageData[i - 1 ][j + 1 ] + imageData[i][j + 1 ]; } else if (j == h - 1 ){ // 右下 sum = imageData[i][j - 1 ] + imageData[i - 1 ][j - 1 ] + imageData[i - 1 ][j]; } else { // 下面 sum = imageData[i][j - 1 ] + imageData[i - 1 ][j - 1 ] + imageData[i - 1 ][j] + imageData[i - 1 ][j + 1 ] + imageData[i][j + 1 ]; } } else { if (j == 0 ){ // 左边 sum = imageData[i - 1 ][j] + imageData[i - 1 ][j + 1 ] + imageData[i][j + 1 ] + imageData[i + 1 ][j + 1 ] + imageData[i + 1 ][j]; } else if (j == h - 1 ){ // 右边 sum = imageData[i - 1 ][j] + imageData[i - 1 ][j - 1 ] + imageData[i][j - 1 ] + imageData[i + 1 ][j - 1 ] + imageData[i + 1 ][j]; } else { // 中间 sum = imageData[i - 1 ][j - 1 ] + imageData[i - 1 ][j] + imageData[i - 1 ][j + 1 ] + imageData[i][j + 1 ] + imageData[i + 1 ][j + 1 ] + imageData[i + 1 ][j] + imageData[i + 1 ][j - 1 ] + imageData[i][j - 1 ]; } } if (imageData[i][j] == 0 ){ if (sum == 3 ){ tmp[i][j] = 1 ; } else { tmp[i][j] = imageData[i][j]; } } else { if (sum <= 1 ){ tmp[i][j] = 0 ; } else if (sum >= 4 ){ tmp[i][j] = 0 ; } else { tmp[i][j] = imageData[i][j]; } } } } return tmp; } /** * 生成num个不重复的且不大于max的正整数,然后由小到大排序 * @param num要生成的随机数个数 * @param max生成的随机数中最大的数据 * @return */ private int [] randomNum( int num, int max){ Set < Integer > set = new TreeSet < Integer > (); while (set.size() < num){ set.add(( int )(Math.random() * max)); } int [] tmp = new int [num]; int i = 0 ; for (Integer it:set){ tmp[i ++ ] = it; } return tmp; } }

Java code
   
   
public class ImageServlet extends HttpServlet { private static final long serialVersionUID = - 2985868496678827231L ; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // resp.setContentType("text/html;charset=gb2312"); resp.setContentType( " image/jpeg " ); String seed = req.getParameter( " seed " ); String host = req.getRemoteAddr().replaceAll( " //. " , "" ); OutputStream out = resp.getOutputStream(); CellService cs = CellService.getInstance(); cs.draw(out,Integer.parseInt(seed), host); out.close(); } }

HTML code
   
   
<% @ page language = " java " import = " java.util.* " pageEncoding = " GB18030 " %> <% String path = request.getContextPath(); String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ; %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > < html > < head > < base href ="<%=basePath%>" > < title > 细胞生长演示图 </ title > < meta http-equiv ="pragma" content ="no-cache" > < meta http-equiv ="cache-control" content ="no-cache" > < meta http-equiv ="expires" content ="0" > <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> < script type ="text/javascript" > function loadImage(){ var container = document.getElementById( " image_id " ); var seeds = document.getElementById( " seeds " ).value; container.innerHTML = " <img width='200' height='180' src='imageServlet?seed= " + seeds + " &time= " + new Date().getTime() + " ' /> " ; } </ script > </ head > < body > 细胞生长演示图 < br > < div >< a id ="image_id" ></ a ></ div > < div > 初始细胞个数 < input type ="text" id ="seeds" size ="13" value ="100" ></ div > </ body > </ html > < script type ="text/javascript" > loadImage(); setInterval( ' loadImage() ' , 1000 ); </ script >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值