337acf1080decddc2dac6593ff18b5c03888cd82
[jalviewjs.git] / site / j2s / swingjs / test / SeeThroughImageApplet.js
1 Clazz.declarePackage ("swingjs.test");
2 Clazz.load (["javax.swing.JApplet", "$.JPanel"], ["swingjs.test.SeeThroughImageApplet", "$.SeeThroughComponent"], ["java.awt.Color", "$.Dimension", "$.Font", "java.awt.event.WindowAdapter", "java.awt.image.BufferedImage", "$.RescaleOp", "java.io.File", "java.net.URL", "javax.imageio.ImageIO", "javax.swing.JFrame", "$.JSlider", "javax.swing.event.ChangeListener"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.bi = null;
5 this.scales = null;
6 this.offsets = null;
7 this.rop = null;
8 Clazz.instantialize (this, arguments);
9 }, swingjs.test, "SeeThroughComponent", javax.swing.JPanel);
10 Clazz.prepareFields (c$, function () {
11 this.scales =  Clazz.newFloatArray (-1, [1, 1, 1, 0.5]);
12 this.offsets =  Clazz.newFloatArray (4, 0);
13 });
14 Clazz.makeConstructor (c$, 
15 function (imageSrc) {
16 Clazz.superConstructor (this, swingjs.test.SeeThroughComponent, []);
17 try {
18 var img = javax.imageio.ImageIO.read (imageSrc);
19 var w = img.getWidth (null);
20 var h = img.getHeight (null);
21 this.bi =  new java.awt.image.BufferedImage (w, h, 2);
22 var g = this.bi.getGraphics ();
23 g.drawImage (img, 0, 0, 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.setOpacity (0.5);
32 }, "java.net.URL");
33 Clazz.overrideMethod (c$, "getPreferredSize", 
34 function () {
35 return  new java.awt.Dimension (this.bi.getWidth (null), this.bi.getHeight (null));
36 });
37 Clazz.defineMethod (c$, "setOpacity", 
38 function (opacity) {
39 this.scales[3] = opacity;
40 this.rop =  new java.awt.image.RescaleOp (this.scales, this.offsets, null);
41 }, "~N");
42 Clazz.overrideMethod (c$, "paint", 
43 function (g) {
44 var g2d = g;
45 g2d.setColor (java.awt.Color.white);
46 g2d.fillRect (0, 0, this.getWidth (), this.getHeight ());
47 g2d.setColor (java.awt.Color.black);
48 g2d.setFont ( new java.awt.Font ("Dialog", 1, 24));
49 g2d.drawString ("JavaScript is cool!", 5, 80);
50 g2d.drawImage (this.bi, this.rop, 0, 0);
51 }, "java.awt.Graphics");
52 c$ = Clazz.decorateAsClass (function () {
53 this.imageSrc = null;
54 Clazz.instantialize (this, arguments);
55 }, swingjs.test, "SeeThroughImageApplet", javax.swing.JApplet);
56 Clazz.makeConstructor (c$, 
57 function () {
58 Clazz.superConstructor (this, swingjs.test.SeeThroughImageApplet, []);
59 });
60 Clazz.makeConstructor (c$, 
61 function (imageSrc) {
62 Clazz.superConstructor (this, swingjs.test.SeeThroughImageApplet, []);
63 this.imageSrc = imageSrc;
64 }, "java.net.URL");
65 Clazz.overrideMethod (c$, "init", 
66 function () {
67 try {
68 this.imageSrc = this.pathTo (swingjs.test.SeeThroughImageApplet.imageFileName);
69 } catch (e) {
70 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {
71 } else {
72 throw e;
73 }
74 }
75 this.buildUI ();
76 });
77 Clazz.defineMethod (c$, "buildUI", 
78 function () {
79 var st =  new swingjs.test.SeeThroughComponent (this.imageSrc);
80 this.add ("Center", st);
81 try {
82 var opacitySlider =  new javax.swing.JSlider (0, 100);
83 opacitySlider.addChangeListener (((Clazz.isClassDefined ("swingjs.test.SeeThroughImageApplet$1") ? 0 : swingjs.test.SeeThroughImageApplet.$SeeThroughImageApplet$1$ ()), Clazz.innerTypeInstance (swingjs.test.SeeThroughImageApplet$1, this, Clazz.cloneFinals ("st", st))));
84 var size = st.getPreferredSize ();
85 var sliderSize = opacitySlider.getPreferredSize ();
86 this.resize (size.width, size.height + sliderSize.height);
87 this.add ("South", opacitySlider);
88 } catch (e) {
89 if (Clazz.exceptionOf (e, Exception)) {
90 } else {
91 throw e;
92 }
93 }
94 });
95 c$.main = Clazz.defineMethod (c$, "main", 
96 function (s) {
97 var f =  new javax.swing.JFrame ("See Through Image");
98 f.addWindowListener (((Clazz.isClassDefined ("swingjs.test.SeeThroughImageApplet$2") ? 0 : swingjs.test.SeeThroughImageApplet.$SeeThroughImageApplet$2$ ()), Clazz.innerTypeInstance (swingjs.test.SeeThroughImageApplet$2, this, null)));
99 var imageSrc = null;
100 try {
101 imageSrc = (( new java.io.File (swingjs.test.SeeThroughImageApplet.imageFileName)).toURI ()).toURL ();
102 } catch (e) {
103 if (Clazz.exceptionOf (e, java.net.MalformedURLException)) {
104 } else {
105 throw e;
106 }
107 }
108 var sta =  new swingjs.test.SeeThroughImageApplet (imageSrc);
109 sta.buildUI ();
110 f.add ("Center", sta);
111 f.pack ();
112 f.setVisible (true);
113 }, "~A");
114 Clazz.defineMethod (c$, "pathTo", 
115  function (file) {
116 var path = this.getDocumentBase ().toString ();
117 var pt = path.indexOf ("/bin/");
118 if (pt > 0) path = path.substring (0, pt) + "/html/" + path.substring (pt + 5);
119 path = path.substring (0, path.lastIndexOf ("/") + 1) + file;
120 if (path.startsWith ("/")) path = "file://" + path;
121 return  new java.net.URL (path);
122 }, "~S");
123 c$.$SeeThroughImageApplet$1$ = function () {
124 Clazz.pu$h(self.c$);
125 c$ = Clazz.declareAnonymous (swingjs.test, "SeeThroughImageApplet$1", null, javax.swing.event.ChangeListener);
126 Clazz.overrideMethod (c$, "stateChanged", 
127 function (e) {
128 var slider = e.getSource ();
129 this.f$.st.setOpacity (slider.getValue () / 100);
130 this.f$.st.repaint ();
131 }, "javax.swing.event.ChangeEvent");
132 c$ = Clazz.p0p ();
133 };
134 c$.$SeeThroughImageApplet$2$ = function () {
135 Clazz.pu$h(self.c$);
136 c$ = Clazz.declareAnonymous (swingjs.test, "SeeThroughImageApplet$2", java.awt.event.WindowAdapter);
137 Clazz.overrideMethod (c$, "windowClosing", 
138 function (e) {
139 System.exit (0);
140 }, "java.awt.event.WindowEvent");
141 c$ = Clazz.p0p ();
142 };
143 Clazz.defineStatics (c$,
144 "imageFileName", "examples/duke_skateboard.jpg");
145 });