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