1 46 47 50 51 52 56 57 58 package sample.blocks.stream.graph; 59 60 import java.awt.Color ; 61 import java.awt.Graphics2D ; 62 import java.awt.image.BufferedImage ; 63 import java.io.DataInputStream ; 64 import java.io.IOException ; 65 import java.util.LinkedList ; 66 67 import javax.swing.ImageIcon ; 68 import javax.swing.JFrame ; 69 import javax.swing.JLabel ; 70 71 import org.mr.api.blocks.MantaInputStream; 72 import org.mr.api.blocks.MantaOutputStream; 73 74 75 public class GraphViewer { 76 private JFrame app ; 77 78 private BufferedImage onscreenImage; 79 private Graphics2D onscreen; 80 private Color background = Color.WHITE; 81 private Color graphColor = Color.BLACK; 82 private int appHight = 200; 83 private int appWidth = 500; 84 private int graphHight = appHight/2; 85 private int graphwidth = appWidth; 86 private LinkedList data; 87 88 public void init() throws IOException { 89 app =new JFrame ("GraphViewer"); 91 app.setBounds(10,10,appWidth,appHight); 92 onscreenImage = new BufferedImage (appWidth, appHight, BufferedImage.TYPE_INT_RGB); 93 onscreen = (Graphics2D ) onscreenImage.getGraphics(); 94 clear(); 95 onscreen.setColor(graphColor); 96 ImageIcon icon = new ImageIcon (onscreenImage); 97 app.setContentPane(new JLabel (icon)); 98 app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 99 app.setVisible(true); 100 101 data=new LinkedList (); 103 for (int i = 0; i < graphwidth; i++) { 104 data.addLast(new Integer (0)); 105 } 106 107 drawData(); 109 110 try { 112 Thread.sleep(2000); 113 } catch (InterruptedException e) { 114 e.printStackTrace(); 116 } 117 118 MantaInputStream in = new MantaInputStream(); 119 in.connect("graph", MantaOutputStream.TOPIC); 120 DataInputStream dis = new DataInputStream (in); 121 int input =0; 122 boolean go = true; 123 while(go){ 124 try{ 125 input=dis.readInt(); 126 }catch(IOException e){ 127 e.printStackTrace(); 128 go =false; 129 } 130 updateGraph(input); 131 132 } 133 System.out.println("done"); 134 135 } 137 public void updateGraph(int newPointValue){ 138 data.addLast(new Integer (newPointValue)); 139 data.removeFirst(); 140 drawData(); 141 } 142 143 public void drawData(){ 144 clear(); 145 onscreen.setColor(graphColor); 146 for (int i = 0; i < data.size(); i++) { 147 int dataVal =((Integer )data.get(i)).intValue(); 148 onscreen.drawRect(i,graphHight-dataVal,1,1); 149 } 150 app.repaint(); 151 } 152 153 public void clear(){ 154 onscreen.setColor(background); 155 onscreen.fillRect(0,0,500,500); 156 app.repaint(); 157 } 158 159 public static void main(String [] args) throws IOException { 160 GraphViewer view = new GraphViewer(); 161 view.init(); 162 }} 164 | Popular Tags |