|
package net.dbyrne.natenberg;
import static java.awt.Color.*; import static java.lang.Long.toHexString; import static org.json.simple.JSONValue.parse;
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics;
import org.json.simple.JSONArray; import org.json.simple.JSONObject;
/** * Graphs a JSON message via Swing. * @author Dennis Byrne */ |
package net.dbyrne.natenberg
import java.awt.Color._ import java.lang.Long.toHexString import org.json.simple.JSONValue._
import java.applet.Applet import java.awt.Color import java.awt.Graphics
import org.json.simple.JSONArray import org.json.simple.JSONObject
/** * Graphs a JSON message via Swing. * @author Dennis Byrne */ |
|
public class Client extends Applet { private static final Integer MARGIN = 5; private JSONArray lines = new JSONArray(); private JSONArray labels = new JSONArray();
public void init(){}
public void stop(){}
public void paint(Graphics g){ paintLabels(g); paintLines(g); } |
class Client extends Applet { private final val MARGIN = 5 private final var lines = new JSONArray private final var labels = new JSONArray
override def init {}
override def stop {}
override def paint(g:Graphics) { paintLabels(g) paintLines(g) } |
|
public void draw(String state){ /* called by JavaScript */ JSONObject root = (JSONObject) parse(state); lines = (JSONArray) root.get("lines"); labels = (JSONArray) root.get("labels"); repaint(); repaint(); repaint(); }
private void paintLines(Graphics g){ for(int i = 0; i < this.lines.size(); i++){ JSONObject line = (JSONObject) lines.get(i); JSONObject from = (JSONObject) line.get("from"); JSONObject to = (JSONObject) line.get("to"); Color color = line.containsKey("color") ? decode("0x" + toHexString(((Long)line.get("color")))) g.setColor(color); g.drawLine(((Long)from.get("x")).intValue() + MARGIN, ((Long)from.get("y")).intValue() + MARGIN, ((Long)to.get("x")).intValue() + MARGIN, ((Long)to.get("y")).intValue() + MARGIN); } }
private void paintLabels(Graphics g){ for(int i = 0; i < labels.size(); i++){ JSONObject label = (JSONObject) labels.get(i); JSONObject pt = (JSONObject) label.get("pt"); String text = label.get("text").toString(); g.drawString(text, ((Long)pt.get("y")).intValue() + MARGIN + 15); } }
} |
def draw(state:String){ /* called by JavaScript */ val root = parse(state).asInstanceOf[JSONObject] lines = root.get("lines").asInstanceOf[JSONArray] labels = root.get("labels").asInstanceOf[JSONArray] repaint; repaint; repaint; }
private def paintLines(g:Graphics){ 0.until(lines.size - 1).foreach(i => { val line = lines.get(i).asInstanceOf[JSONObject] val from = line.get("from").asInstanceOf[JSONObject] val to = line.get("to").asInstanceOf[JSONObject] val color = if(line.containsKey("color")) decode("0x" + toHexString(line.get("color").asInstanceOf[Long])) g.setColor(color) g.drawLine(from.get("x").asInstanceOf[Long].intValue + MARGIN, from.get("y").asInstanceOf[Long].intValue + MARGIN, to.get("x").asInstanceOf[Long].intValue + MARGIN, to.get("y").asInstanceOf[Long].intValue + MARGIN) }) }
private def paintLabels(g:Graphics){ 0.until(labels.size - 1).foreach(i => { val label = labels.get(i).asInstanceOf[JSONObject] val pt = label.get("pt").asInstanceOf[JSONObject] val text = label.get("text").toString g.drawString(text, pt.get("y").asInstanceOf[Long].intValue + MARGIN + 15) }) } }
|