BarGraph.java
import javax.swing.*;
import java.io.IOException;
import java.util.Vector;
import java.awt.*;
import fr.esrf.tangoatk.widget.util.jdraw.JDrawEditor;
import fr.esrf.tangoatk.widget.util.jdraw.JDObject;
import fr.esrf.tangoatk.widget.util.jdraw.JDAxis;
import fr.esrf.tangoatk.widget.util.ATKGraphicsUtils;
public class BarGraph extends JFrame {
JDrawEditor theGraph;
JDObject graphArea;
JDAxis graphAxis;
JDObject[] allBars = new JDObject[8];
String[] barList = {"Cell1","Cell3","Cell4","Cell8","Cell9","Cell10","Cell15","Cell19"};
public BarGraph() {
theGraph = new JDrawEditor(JDrawEditor.MODE_PLAY);
try {
theGraph.loadFile("bargraph.jdw");
} catch (IOException e) {
JOptionPane.showMessageDialog(this,e.getMessage(),"Error loading file",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
graphArea = getObject("masterView");
graphAxis = (JDAxis)getObject("Axis");
for(int i=0;i<barList.length;i++)
allBars[i] = getObject(barList[i]);
new Thread() {
public void run() {
while (true) {
for(int i=0;i<barList.length;i++) {
double h = Math.random() / 5.0 - 0.1;
updateBar(i,h);
}
graphArea.refresh();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}
}
}.start();
theGraph.setAutoZoom(true);
setContentPane(theGraph);
setTitle("BarGraph");
}
public JDObject getObject(String name) {
Vector objs = theGraph.getObjectsByName(name,false);
if(objs.size()==0) {
System.out.print("Error , no object named '" + name + "' found." );
System.exit(0);
} else if(objs.size()>1) {
System.out.print("Warning , more than one object having the name : " + name +" found ,getting first..." );
}
return (JDObject)objs.get(0);
}
public void updateBar(int idx,double x) {
Rectangle gRect = graphArea.getBoundRect();
double y = (double)gRect.y;
double h = (double)gRect.height;
double max = graphAxis.getMax();
double min = graphAxis.getMin();
double nPos = (min-x)*h / (max-min) + (y+h);
allBars[idx].moveSummitV(5,nPos);
}
public static void main(String[] args) {
final BarGraph bg = new BarGraph();
ATKGraphicsUtils.centerFrameOnScreen(bg);
bg.setVisible(true);
}
}