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
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
17 Clazz.makeConstructor (c$,
\r
18 function (imageSrc) {
\r
19 Clazz.superConstructor (this, swingjs.test.JumbledImage, []);
\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
25 if (Clazz.exceptionOf (e, java.io.IOException)) {
\r
26 System.out.println ("Image could not be read");
\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
38 Clazz.defineMethod (c$, "jumble",
\r
40 var rand = new java.util.Random ();
\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
49 Clazz.overrideMethod (c$, "getPreferredSize",
\r
51 return new java.awt.Dimension (this.w, this.h);
\r
53 Clazz.overrideMethod (c$, "paint",
\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
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
75 Clazz.superConstructor (this, swingjs.test.JumbledImageApplet, []);
\r
77 Clazz.makeConstructor (c$,
\r
78 function (imageSrc) {
\r
79 Clazz.superConstructor (this, swingjs.test.JumbledImageApplet, []);
\r
80 this.imageSrc = imageSrc;
\r
82 Clazz.overrideMethod (c$, "init",
\r
85 this.imageSrc = this.pathTo (swingjs.test.JumbledImageApplet.imageFileName);
\r
87 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {
\r
94 Clazz.defineMethod (c$, "buildUI",
\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
104 c$.main = Clazz.defineMethod (c$, "main",
\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
110 imageSrc = (( new java.io.File (swingjs.test.JumbledImageApplet.imageFileName)).toURI ()).toURL ();
\r
112 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {
\r
117 var jumbler = new swingjs.test.JumbledImageApplet (imageSrc);
\r
118 jumbler.buildUI ();
\r
119 f.add ("Center", jumbler);
\r
121 f.setVisible (true);
\r
123 Clazz.defineMethod (c$, "pathTo",
\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
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
137 var b = e.getSource ();
\r
138 this.f$.ji.jumble ();
\r
139 this.f$.ji.repaint ();
\r
140 }, "java.awt.event.ActionEvent");
\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
149 }, "java.awt.event.WindowEvent");
\r
152 Clazz.defineStatics (c$,
\r
153 "imageFileName", "examples/duke_skateboard.jpg");
\r