JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / swingjs / j2s / swingjs / test / JumbledImageApplet.js
1 Clazz.declarePackage ("swingjs.test");\r
2 Clazz.load (["javax.swing.JApplet", "$.JPanel"], ["swingjs.test.JumbledImageApplet", "$.JumbledImage"], ["java.awt.Dimension", "java.awt.event.ActionListener", "$.WindowAdapter", "java.io.File", "java.net.URL", "java.util.Random", "javax.imageio.ImageIO", "javax.swing.JButton", "$.JFrame"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.numlocs = 2;\r
5 this.numcells = 0;\r
6 this.cells = null;\r
7 this.bi = null;\r
8 this.w = 0;\r
9 this.h = 0;\r
10 this.cw = 0;\r
11 this.ch = 0;\r
12 Clazz.instantialize (this, arguments);\r
13 }, swingjs.test, "JumbledImage", javax.swing.JPanel);\r
14 Clazz.prepareFields (c$, function () {\r
15 this.numcells = this.numlocs * this.numlocs;\r
16 });\r
17 Clazz.makeConstructor (c$, \r
18 function (imageSrc) {\r
19 Clazz.superConstructor (this, swingjs.test.JumbledImage, []);\r
20 try {\r
21 this.bi = javax.imageio.ImageIO.read (imageSrc);\r
22 this.w = this.bi.getWidth (null);\r
23 this.h = this.bi.getHeight (null);\r
24 } catch (e) {\r
25 if (Clazz.exceptionOf (e, java.io.IOException)) {\r
26 System.out.println ("Image could not be read");\r
27 } else {\r
28 throw e;\r
29 }\r
30 }\r
31 this.cw = Clazz.doubleToInt (this.w / this.numlocs);\r
32 this.ch = Clazz.doubleToInt (this.h / this.numlocs);\r
33 this.cells =  Clazz.newIntArray (this.numcells, 0);\r
34 for (var i = 0; i < this.numcells; i++) {\r
35 this.cells[i] = i;\r
36 }\r
37 }, "java.net.URL");\r
38 Clazz.defineMethod (c$, "jumble", \r
39 function () {\r
40 var rand =  new java.util.Random ();\r
41 var ri;\r
42 for (var i = 0; i < this.numcells; i++) {\r
43 while ((ri = rand.nextInt (this.numlocs)) == i) ;\r
44 var tmp = this.cells[i];\r
45 this.cells[i] = this.cells[ri];\r
46 this.cells[ri] = tmp;\r
47 }\r
48 });\r
49 Clazz.overrideMethod (c$, "getPreferredSize", \r
50 function () {\r
51 return  new java.awt.Dimension (this.w, this.h);\r
52 });\r
53 Clazz.overrideMethod (c$, "paint", \r
54 function (g) {\r
55 var dx;\r
56 var dy;\r
57 for (var x = 0; x < this.numlocs; x++) {\r
58 var sx = x * this.cw;\r
59 for (var y = 0; y < this.numlocs; y++) {\r
60 var sy = y * this.ch;\r
61 var cell = this.cells[x * this.numlocs + y];\r
62 dx = (Clazz.doubleToInt (cell / this.numlocs)) * this.cw;\r
63 dy = (cell % this.numlocs) * this.ch;\r
64 g.drawImage (this.bi, dx, dy, dx + this.cw, dy + this.ch, sx, sy, sx + this.cw, sy + this.ch, null);\r
65 }\r
66 }\r
67 }, "java.awt.Graphics");\r
68 c$ = Clazz.decorateAsClass (function () {\r
69 this.imageSrc = null;\r
70 this.jumbledImage = null;\r
71 Clazz.instantialize (this, arguments);\r
72 }, swingjs.test, "JumbledImageApplet", javax.swing.JApplet);\r
73 Clazz.makeConstructor (c$, \r
74 function () {\r
75 Clazz.superConstructor (this, swingjs.test.JumbledImageApplet, []);\r
76 });\r
77 Clazz.makeConstructor (c$, \r
78 function (imageSrc) {\r
79 Clazz.superConstructor (this, swingjs.test.JumbledImageApplet, []);\r
80 this.imageSrc = imageSrc;\r
81 }, "java.net.URL");\r
82 Clazz.overrideMethod (c$, "init", \r
83 function () {\r
84 try {\r
85 this.imageSrc = this.pathTo (swingjs.test.JumbledImageApplet.imageFileName);\r
86 } catch (e) {\r
87 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {\r
88 } else {\r
89 throw e;\r
90 }\r
91 }\r
92 this.buildUI ();\r
93 });\r
94 Clazz.defineMethod (c$, "buildUI", \r
95 function () {\r
96 var ji =  new swingjs.test.JumbledImage (this.imageSrc);\r
97 this.add ("Center", ji);\r
98 var jumbleButton =  new javax.swing.JButton ("Jumble");\r
99 jumbleButton.addActionListener (((Clazz.isClassDefined ("swingjs.test.JumbledImageApplet$1") ? 0 : swingjs.test.JumbledImageApplet.$JumbledImageApplet$1$ ()), Clazz.innerTypeInstance (swingjs.test.JumbledImageApplet$1, this, Clazz.cloneFinals ("ji", ji))));\r
100 var jumbleSize = ji.getPreferredSize ();\r
101 this.resize (jumbleSize.width, jumbleSize.height + 40);\r
102 this.add ("South", jumbleButton);\r
103 });\r
104 c$.main = Clazz.defineMethod (c$, "main", \r
105 function (s) {\r
106 var f =  new javax.swing.JFrame ("Jumbled Image");\r
107 f.addWindowListener (((Clazz.isClassDefined ("swingjs.test.JumbledImageApplet$2") ? 0 : swingjs.test.JumbledImageApplet.$JumbledImageApplet$2$ ()), Clazz.innerTypeInstance (swingjs.test.JumbledImageApplet$2, this, null)));\r
108 var imageSrc = null;\r
109 try {\r
110 imageSrc = (( new java.io.File (swingjs.test.JumbledImageApplet.imageFileName)).toURI ()).toURL ();\r
111 } catch (e) {\r
112 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {\r
113 } else {\r
114 throw e;\r
115 }\r
116 }\r
117 var jumbler =  new swingjs.test.JumbledImageApplet (imageSrc);\r
118 jumbler.buildUI ();\r
119 f.add ("Center", jumbler);\r
120 f.pack ();\r
121 f.setVisible (true);\r
122 }, "~A");\r
123 Clazz.defineMethod (c$, "pathTo", \r
124  function (file) {\r
125 var path = this.getDocumentBase ().toString ();\r
126 var pt = path.indexOf ("/bin/");\r
127 if (pt > 0) path = path.substring (0, pt) + "/html/" + path.substring (pt + 5);\r
128 path = path.substring (0, path.lastIndexOf ("/") + 1) + file;\r
129 if (path.startsWith ("/")) path = "file://" + path;\r
130 return  new java.net.URL (path);\r
131 }, "~S");\r
132 c$.$JumbledImageApplet$1$ = function () {\r
133 Clazz.pu$h(self.c$);\r
134 c$ = Clazz.declareAnonymous (swingjs.test, "JumbledImageApplet$1", null, java.awt.event.ActionListener);\r
135 Clazz.overrideMethod (c$, "actionPerformed", \r
136 function (e) {\r
137 var b = e.getSource ();\r
138 this.f$.ji.jumble ();\r
139 this.f$.ji.repaint ();\r
140 }, "java.awt.event.ActionEvent");\r
141 c$ = Clazz.p0p ();\r
142 };\r
143 c$.$JumbledImageApplet$2$ = function () {\r
144 Clazz.pu$h(self.c$);\r
145 c$ = Clazz.declareAnonymous (swingjs.test, "JumbledImageApplet$2", java.awt.event.WindowAdapter);\r
146 Clazz.overrideMethod (c$, "windowClosing", \r
147 function (e) {\r
148 System.exit (0);\r
149 }, "java.awt.event.WindowEvent");\r
150 c$ = Clazz.p0p ();\r
151 };\r
152 Clazz.defineStatics (c$,\r
153 "imageFileName", "examples/duke_skateboard.jpg");\r
154 });\r