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