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 () {
12 Clazz.instantialize (this, arguments);
13 }, swingjs.test, "JumbledImage", javax.swing.JPanel);
14 Clazz.prepareFields (c$, function () {
15 this.numcells = this.numlocs * this.numlocs;
17 Clazz.makeConstructor (c$,
19 Clazz.superConstructor (this, swingjs.test.JumbledImage, []);
21 this.bi = javax.imageio.ImageIO.read (imageSrc);
22 this.w = this.bi.getWidth (null);
23 this.h = this.bi.getHeight (null);
25 if (Clazz.exceptionOf (e, java.io.IOException)) {
26 System.out.println ("Image could not be read");
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++) {
38 Clazz.defineMethod (c$, "jumble",
40 var rand = new java.util.Random ();
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];
49 Clazz.overrideMethod (c$, "getPreferredSize",
51 return new java.awt.Dimension (this.w, this.h);
53 Clazz.overrideMethod (c$, "paint",
57 for (var x = 0; x < this.numlocs; x++) {
59 for (var y = 0; y < this.numlocs; y++) {
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);
67 }, "java.awt.Graphics");
68 c$ = Clazz.decorateAsClass (function () {
70 this.jumbledImage = null;
71 Clazz.instantialize (this, arguments);
72 }, swingjs.test, "JumbledImageApplet", javax.swing.JApplet);
73 Clazz.makeConstructor (c$,
75 Clazz.superConstructor (this, swingjs.test.JumbledImageApplet, []);
77 Clazz.makeConstructor (c$,
79 Clazz.superConstructor (this, swingjs.test.JumbledImageApplet, []);
80 this.imageSrc = imageSrc;
82 Clazz.overrideMethod (c$, "init",
85 this.imageSrc = this.pathTo (swingjs.test.JumbledImageApplet.imageFileName);
87 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {
94 Clazz.defineMethod (c$, "buildUI",
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);
104 c$.main = Clazz.defineMethod (c$, "main",
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)));
110 imageSrc = (( new java.io.File (swingjs.test.JumbledImageApplet.imageFileName)).toURI ()).toURL ();
112 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {
117 var jumbler = new swingjs.test.JumbledImageApplet (imageSrc);
119 f.add ("Center", jumbler);
123 Clazz.defineMethod (c$, "pathTo",
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);
132 c$.$JumbledImageApplet$1$ = function () {
134 c$ = Clazz.declareAnonymous (swingjs.test, "JumbledImageApplet$1", null, java.awt.event.ActionListener);
135 Clazz.overrideMethod (c$, "actionPerformed",
137 var b = e.getSource ();
138 this.f$.ji.jumble ();
139 this.f$.ji.repaint ();
140 }, "java.awt.event.ActionEvent");
143 c$.$JumbledImageApplet$2$ = function () {
145 c$ = Clazz.declareAnonymous (swingjs.test, "JumbledImageApplet$2", java.awt.event.WindowAdapter);
146 Clazz.overrideMethod (c$, "windowClosing",
149 }, "java.awt.event.WindowEvent");
152 Clazz.defineStatics (c$,
153 "imageFileName", "examples/duke_skateboard.jpg");