To generate a square map of N * N7 on the browser page, the solution is to generate a grid chart between the browser and the server and return it to the front end.
For example, canvas:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119522292-0.png "title =" 12.png" data-pinit = "registered"/>
Note: The lattice subgraph consists of a red and black lattice. The default color is red. The black color indicates a special display lattice.
To generate a nine-square-lattice pattern, we thought of four methods. JavaScript implemented three methods, and Java implemented one.
1. Generate a square grid by using JavaScript to generate a Table.
Generate table:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119522413-1.png "title =" table.png "data-pinit =" registered "/>
Source code:
// Function drawTable (edge, n) generated by the table {var size = edge/n; var sb = new StringBuffer (); sb. append ("<table border = '1' height = '" + edge + "'width ='" + edge + "'bgcolor = 'red'> "); for (var I = 0; I <n; I ++) {sb. append ("<tr>"); for (var j = 0; j <n; j ++) {var p = I + ''+ j; sb. append ("<td width = '" + size + "'height ='" + size + "'"); if (point. contains (p) {sb. append ("bgcolor = '#000000'>");} else {sb. append ("bgcolor = '# ffff'>");} sb. append ("</td>");} sb. append ("</tr>");} sb. append ("</table>"); document. getElementById ("div "). innerHTML = sb. toString ();}
Note: The edge parameter indicates the pixels of the width and height of the Table, and n indicates the number of small grids in a row in the square Table.
2. JavaScript uses span tag combination and pure color image to generate squares.
Generate span:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201308/191715372.png "title =" image.png "data-pinit =" registered "/>
Source code:
function drawImage(edge, n) { var size = edge / n; var flag = n >= 100 ? 100 : 100 > n >= 75 ? 75 : 75 > n >= 50 ? 50 : 25; var imgetrue = "cell/"+flag+"/1.png"; var imgefalse = "cell/"+flag+"/0.png"; var sb = new StringBuffer(); for (var i = 0; i < n; i++) { sb.append("<span>"); for (var j = 0; j < n; j++) { var p = i + '' + j; if (point.contains(p)) { sb.append("<br/>") } document.getElementById("div").innerHTML = sb.toString(); }
Note: The parameters are the same as above.
Two images with solid width = height and different pixels are used. The generation policy determines the number of small grids in a row to use images with different pixels.
3. Use the Canvas function of HTML to draw images.
See the canvas in the first figure in this article.
Source code:
// HTML5Canvas draw function drawCanvas (edge, n) {document. getElementById ("div "). innerHTML = ""; var size = edge/n; canvas = document. getElementById ("canvas"); canvas. setAttribute ("height", edge); canvas. setAttribute ("width", edge); cxt = canvas. getContext ('2d '); cxt. strokeStyle = "rgb (255,255,255)"; for (var I = 0; I <n; I ++) {for (var j = 0; j <n; j ++) {var p = I + "" + j; if (point. contains (p) {cxt. fillStyle = "rgb (0, 0, 0)";} else {cxt. fillStyle = "rgb (255, 0, 0)";} cxt. fillRect (size * j, size * I, size, size); cxt. strokeRect (size * j, size * I, size, size );}}}
Note: The parameters are the same as above.
FillRect (x, y, w, h); Draw filled rectangle
StrokeRect (x, y, w, h); draw the rectangle of the line
The above two rectangles are drawn repeatedly. The fillRect and strokeRect methods are used to make the drawn squares have fill colors and borders.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111952J03-3.gif "/> using HTML5 Canvas rendering requires support for HTML5 browsers.
4. Use Java to generate N * N-level square Images
:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111952K45-4.png "title =" dis_15.png "data-pinit =" registered "/>
Key code:
BufferedImage bimage = new BufferedImage(edgePixel * n, edgePixel * n, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bimage.createGraphics(); g2d.getDeviceConfiguration().createCompatibleImage(edgePixel * n, edgePixel * n, Transparency.TRANSLUCENT); g2d.dispose(); g2d = bimage.createGraphics();
Step 1: create an image buffer object, BufferedImage.
Step 2: Two-Dimensional drawing brush to obtain Graphics interface implementation class Graphics2D object
Step 3: Set attributes such as the line color and background color of the paint brush.
Step 4: Draw a graph. The corresponding drawing method provided in the API
Step 5: Write the content cached by the image into the file through the output stream of the image file.
The following code is used:
FileImageOutputStream fios = new FileImageOutputStream(new File(imageName));ImageIO.write(bimage, IMAGE_TYPE.substring(1), fios);
Description: The IMAGE_TYPE string indicates the file type of the output image, such as png, jpeg, gif, and bmp.
The StringBuffer object and the contains method of the Array object are used in the above JavaScript and Canvas Graph Generation Process. For the source code, see the appendix: celltablegenerate image demo file celltable.html.txtto remove the suffix when using. txt].
When it comes to generating N * N-order squares, and highlighting the specified grids, the problem is due to the problem of finding patterns in the previous two articles.
Article: Bin Laden game development-distributed computing service Machine Client Program Design
Bin Laden game development-seeking for beautiful patterns in time Accumulation
Today, we have achieved a little success. We calculated N = 26.
Pick a few pictures to see the pattern:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119521446-5.png "style =" float: left; "title =" dis_10.png "data-pinit =" registered "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119524610-6.png "title =" dis_12.png "data-pinit =" registered "style =" float: left; "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11195214Z-7.png "title =" dis_13.png "data-pinit =" registered "style =" float: left; "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11195240W-8.png "title =" dis_18.png "data-pinit =" registered "style =" float: left; "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119521400-9.png "title =" dis_0000png "data-pinit =" registered "style =" float: left; "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11195260Y-10.png "title =" dis_25.png "data-pinit =" registered "style =" float: left; "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1119526439-11.png "title =" dis_26.png "data-pinit =" registered "style =" float: none; "/>
The author of this issue came from the Popeye of the Sina blogger. On the special day of August 19, 2013, I took a vacation for myself. I didn't work overtime at night. I relaxed and went home to sleep all at once. I got up around and read the book. It took a short time. After a while, I went to bed at, but I couldn't sleep, this problem suddenly turned around in my mind, my brain never stopped, and I got tired when I got up in the morning, so I solved it today, it's a final conclusion.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/111952J03-3.gif "/> is it really so hard, the programmer's life! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11195223N-13.gif "/>
This article is from the "wild horse red dust" blog and will not be reposted!