User Tools

Site Tools


sample_code:image_array

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:image_array [2015/09/28 15:06]
stevegarman created
sample_code:image_array [2015/09/29 17:27]
Line 1: Line 1:
-====== Image array ====== 
-Click part of the image to store a number in a 2d array representing the rectangles of the image. 
  
-Click a rectangle multiple times and it will toggle between 1 and 2. 
- 
-A colour-coded rectangle appears on the image to show what value was in the array before it was updated. 
- 
-This is intended as a demo of one way to treat an image as a grid. 
- 
-<code JavaScript imageArray.js> 
- 
-var cellData=[]; 
-//Called when application is started. 
-function OnStart() 
-{ 
-    //Create a layout with objects vertically centered. 
-    var lay = app.CreateLayout( "linear", "VCenter,FillXY" );     
- 
-    //Create a text label and add it to layout. 
-    var img = app.CreateImage( null, 0.9, 0.5 ); 
-    img.SetColor("#ff999999") 
-    img.SetOnTouch(imgOnTouch); 
-     
-    lay.AddChild( img ); 
-    var btnSave=app.CreateButton("Save"); 
-    btnSave.SetOnTouch(btnSaveOnTouch); 
-    lay.AddChild(btnSave); 
-    //Add layout to app.     
-    app.AddLayout( lay ); 
-} 
- 
-function imgOnTouch(ev) 
-{ 
-     var row,col,val,x1,y1,x2,y2 
-     var cellWidth=0.1,cellHeight=0.1; 
-     if(ev.action==="Down") 
-     { 
-         row = Math.floor(ev.Y/cellHeight); 
-         col = Math.floor(ev.X/cellWidth); 
-         x1 = col*cellWidth; 
-         y1 = row*cellHeight; 
-         x2 = x1 + cellWidth; 
-         y2 = y1 + cellHeight; 
-         val = getData(row,col); 
-         this.SetLineWidth(1); 
-         this.SetPaintStyle("Fill") 
-         setData(row,col,(val+1)%2); 
-         if(val < 0) 
-         { 
-            this.SetPaintColor("#ffff0000"); 
-            this.SetPaintStyle("Line") 
-            this.DrawRectangle(x1,y1,x2,y2); 
-         } 
-         else if(val === 0) 
-         { 
-            this.SetPaintColor("#ff00ff00"); 
-            this.DrawRectangle(x1,y1,x2,y2); 
-         } 
-         else if(val === 1) 
-         { 
-            this.SetPaintColor("#ff0000ff"); 
-            this.DrawRectangle(x1,y1,x2,y2); 
-         } 
-         app.ShowPopup(val); 
-     } 
-} 
- 
-function getData(row,col) 
-{ 
-   var ret; 
-   if (cellData.length < row +1) 
-      return -1;  // represents unset 
-   ret = cellData[row][col]; 
-   if(ret===undefined) 
-      return -1; 
-   return ret; 
-} 
-function setData(row,col,value) 
-{ 
-   while (cellData.length < row +1) 
-   { 
-       cellData.push([]); 
-   } 
-   cellData[row][col]=value; 
-} 
- 
-function btnSaveOnTouch() 
-{ 
-      
-    var data = JSON.stringify(cellData); 
-    app.WriteFile("/sdcard/picData",data)      
-} 
- 
-</code> 
sample_code/image_array.txt · Last modified: 2015/09/29 17:27 (external edit)