a47bb63f94f995eb3fa68f7529bc5b70c540de54
[jalviewjs.git] / site / j2s / org / uwi / BoltzCanvas.js
1 Clazz.declarePackage ("org.uwi");\r
2 Clazz.load (["javax.swing.JPanel"], "org.uwi.BoltzCanvas", ["java.awt.Color", "$.Point", "$.Rectangle"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.maxEnergy = 0;\r
5 this.energyLevels = null;\r
6 this.xFactor = 0;\r
7 this.boltzmann = null;\r
8 Clazz.instantialize (this, arguments);\r
9 }, org.uwi, "BoltzCanvas", javax.swing.JPanel);\r
10 Clazz.makeConstructor (c$, \r
11 function (boltzmann) {\r
12 Clazz.superConstructor (this, org.uwi.BoltzCanvas);\r
13 this.boltzmann = boltzmann;\r
14 }, "org.uwi.Boltzmann");\r
15 Clazz.overrideMethod (c$, "paintComponent", \r
16 function (g) {\r
17 g.setColor (this.getForeground ());\r
18 g.setPaintMode ();\r
19 this.displayBoltz (g);\r
20 }, "java.awt.Graphics");\r
21 Clazz.defineMethod (c$, "displayBoltz", \r
22 function (g) {\r
23 var i;\r
24 org.uwi.BoltzCanvas.nPaint++;\r
25 var r = this.getBounds ();\r
26 g.setColor (java.awt.Color.black);\r
27 g.drawString ("A Boltzmann Simulation (" + this.boltzmann.numOfCollisions + ")", r.x + 110, r.y + 10);\r
28 g.drawString ("E", r.x + 5, r.y + 145);\r
29 g.drawString ("No. of Particles", r.x + 110, r.y + 286);\r
30 var graphRec =  new java.awt.Rectangle (r.x + 20, r.y + 20, r.width - 40, r.height - 40);\r
31 g.clearRect (graphRec.x - 1, graphRec.y - 1, graphRec.width + 3, graphRec.height + 2);\r
32 g.drawRect (graphRec.x - 2, graphRec.y - 2, graphRec.width + 4, graphRec.height + 3);\r
33 var bottomRight =  new java.awt.Point (graphRec.x + graphRec.width, graphRec.y + graphRec.height);\r
34 for (i = this.maxEnergy; i > 0; i--) {\r
35 if (this.energyLevels[i] >= 1) break;\r
36 this.maxEnergy--;\r
37 }\r
38 this.xFactor = 0;\r
39 for (i = 1; i <= (this.maxEnergy); i++) {\r
40 if (this.energyLevels[i] > this.xFactor) {\r
41 this.xFactor = this.energyLevels[i];\r
42 if (i == this.maxEnergy) this.maxEnergy = 2 * this.maxEnergy;\r
43 }}\r
44 var xScale = (graphRec.width) / this.xFactor;\r
45 var yScale = (graphRec.height) / (this.maxEnergy + 1);\r
46 g.setColor (java.awt.Color.red);\r
47 var curY = 0;\r
48 for (i = 0; i <= this.maxEnergy; i++) {\r
49 curY = (bottomRight.y - 1) - (Clazz.doubleToInt (yScale * (i + 1)));\r
50 if (this.energyLevels[i] > 0) {\r
51 var isMax = (this.energyLevels[i] == this.xFactor);\r
52 if (isMax) g.setColor (java.awt.Color.BLUE);\r
53 g.drawLine (graphRec.x, curY, graphRec.x + (Clazz.doubleToInt (xScale * this.energyLevels[i])), curY);\r
54 if (isMax) g.setColor (java.awt.Color.RED);\r
55 }}\r
56 }, "java.awt.Graphics");\r
57 Clazz.defineStatics (c$,\r
58 "nPaint", 0);\r
59 });\r