Topic: Jython and JavaBean properties
As I have not seen any topic of the Jython access properties to JavaBean here, I'd like to give a few example of how they are very efficient with the jhplot libraries to make a code very compact and easy to write and read.
Maybe what I will describe is obvious, but I guess that not all are aware of those properties.
First, I don't want to describe in details what is a JavaBean (you can find tens of websites that will explain it better than me), but just explain basically that they are Java class that respect some writing conventions.
One of this convention, in particular, has a very important interest in Jython: The fields of a Java Class are declared private (possibly protected or friendly) and are accessed via “acessor” methods (getters/setters methods) as following.
For instance a field:
private double dou1=1;
will be accessed with two public methods:
public double getDou1()
public void setDou1(double d)
used for reading and writing this double value.
The things is that most of the jHPlot code is written respecting this convention. Let's see now how it could be used to simplify the code.
Thus a small piece of Jython code used in the standard "Java way" would be written:
from java.awt import Color from java.util import Random from jhplot import HPlotJa, H1D from jhplot.jadraw import JaText rand = Random() val =  for i in range(500): val.append(rand.nextGaussian()) h1 = H1D("H1D-1",20, -2.0, 2.0) h1.setColor(Color.green) h1.setErrX(False) h1.setErrY(True) h1.setPenWidthErr(2) h1.fill(val) c1 = HPlotJa("Canvas-1", 600, 600, 1, 1, True) c1.visible() c1.setAntiAlias(False) c1.setGTitle("Title-1") c1.setNameX("Xaxis") c1.setNameY("Yaxis") c1.setName("Canvas Title") c1.draw(h1) t1 = JaText("Text-1", 0.3, 0.3, "NDC") t1.setColor(h1.getColor()) c1.draw(t1) c1.update()
Of course, this works very well but as the Jython interpreter is smart with these accessor methods, it is possible to get a direct access to the private field and the interpreter will by itself use the getter/setter methods. Now if I write the same example as above with the properties, one would have:
from java.awt import Color from java.util import Random from jhplot import HPlotJa, H1D from jhplot.jadraw import JaText rand = Random() val =  for i in range(500): val.append(rand.nextGaussian()) h2 = H1D("H1D-2",20, -2.0, 2.0) h2.color = Color.blue h2.errX = False h2.errY = True h2.penWidthErr = 2 h2.fill(val) c2 = HPlotJa("Canvas-2", 600, 600, 1, 1, True) c2.visible() c2.antiAlias = False c2.GTitle = "Title-2" c2.nameX = "Xaxis" c2.nameY = "Yaxis" c2.name = "Canvas Title" c2.draw(h2) t2 = JaText("Text-2", 0.3, 0.3, "NDC") t2.color = h2.color c2.draw(t2) c2.update()
One can see that we don't really save typing code , however it is (in my opinion) more clear to read, thanks to the “=” symbols.
Finally there another way to use those JavaBean properties, with once again, the smart Jython interpreter who can read or write the class fields directly in the object instance. Thus the same code can now be written:
from java.awt import Color from java.util import Random from jhplot import HPlotJa, H1D from jhplot.jadraw import JaText rand = Random() val =  for i in range(500): val.append(rand.nextGaussian()) h3 = H1D("H1D-3", 20, -2.0, 2.0, color=Color.red, errX=False, errY=True, penWidthErr=2) h3.fill(val) c3 = HPlotJa("Canvas-3", 600, 600, 1, 1, True, antiAlias=False, GTitle="Title-3", nameX="Xaxis", nameY="Yaxis", name="Canvas Title") c3.visible() c3.draw(h3) t3 = JaText("Text-3", 0.3, 0.3, "NDC", color=h3.color) c3.draw(t3) c3.update()
One can see now that the code is more compact and each object is easy to identify.
Of course these three methods produce exactly the same result.
Although this feature allows to save a lot a time while coding one must, however, be careful with some methods which are sometime named get/setSomething() but which are actually not providing any access to a private field of the class but a random methods doing some stuff. Of course this will not work in that case.
I hope this small tutorial will be helpful and will convince all septic people about how powerful is the combo Java/Jython.