# DMelt:Programming/4 Java

Limitted access. First login to DataMelt if you are a full DataMelt member. Then login to HandWiki as a user.

# Using Java

DataMelt is written in Java. Thus you can write your code in Java calling DataMelt libraries, and creating jar files that hold your program. You can use DataMelt IDE to write Java code, compile and execute it (simply press "run"). Or use any IDE, like NetBeans, to write Java programs. See the section Using Netbeans.

Let us give an example how to write a simple Java code that plots a histogram:

import java.awt.Color;
import java.util.Random;
import jhplot.*;

class Histo1 {
public static void main(String[] args) {

HPlot c1 = new HPlot("Canvas",600,400,1,1);
c1.setGTitle("Global title for F_{2} and x_{&gamma;} ");
c1.visible(true);
c1.setAutoRange();

H1D h1 = new H1D("Simple1",20, -2.0, 2.0);
Random rand = new Random();
for (int i=0; i<100; i++)  h1.fill(rand.nextGaussian());

c1.draw(h1);
h1.setColor(Color.blue);
h1.setPenWidthErr(2);
c1.setNameX("Xaxis");
c1.setNameY("Yaxis");
c1.setName("Canvas title");
c1.drawStatBox(h1);
c1.update();
c1.export("test.pdf"); '' make PDF output
}
}

When compiling this code, make sure that all jar files are included to CLASSPATH. Since there are many DataMelt jar files located in "lib" directory, consider single jar that holds all DataMelt libraries. It is accessible in from the section using Netbeans.

The result of the Java code above is identical to that given by this Jython script:

from java.awt import Color
from java.util import Random
from jhplot  import *

c1 = HPlot("Canvas",600,400,1, 1)
c1.setGTitle("Global labels: F_{2},  x_{&gamma;}  #bar{p}p F_{2}^{c#bar{c}}"); #put title
c1.visible(1)
c1.setAutoRange()
h1 = H1D("Simple1",100, -2, 2.0)
rand = Random()
# fill histogram
for i in range(100):
h1.fill(rand.nextGaussian())
c1.draw(h1)
c1.setAutoRange()
h1.setPenWidthErr(2)
c1.setNameX("Xaxis")
c1.setNameY("Yaxis")
c1.setName("Canvas title")
c1.drawStatBox(h1)
c1.export("test.pdf"); '' make PDF output

As already mentioned, you may also try to run this code in Groovy, which typically over-perform Jython for loops.