KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > blocks > stream > graph > GraphViewer


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 /*
48  * @author Amir Shevat
49  */

50
51
52  /*==================================================================================
53    For instructions on how to run this sample please refer to the file
54    sample\blocks\stream\graph\Readme.txt under the MantaRay installation directory.
55  ==================================================================================*/

56
57
58 package sample.blocks.stream.graph;
59
60 import java.awt.Color JavaDoc;
61 import java.awt.Graphics2D JavaDoc;
62 import java.awt.image.BufferedImage JavaDoc;
63 import java.io.DataInputStream JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.util.LinkedList JavaDoc;
66
67 import javax.swing.ImageIcon JavaDoc;
68 import javax.swing.JFrame JavaDoc;
69 import javax.swing.JLabel JavaDoc;
70
71 import org.mr.api.blocks.MantaInputStream;
72 import org.mr.api.blocks.MantaOutputStream;
73
74
75 public class GraphViewer {
76     private JFrame JavaDoc app ;
77
78     private BufferedImage JavaDoc onscreenImage;
79     private Graphics2D JavaDoc onscreen;
80     private Color JavaDoc background = Color.WHITE;
81     private Color JavaDoc 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 JavaDoc data;
87
88     public void init() throws IOException JavaDoc{
89         // init graph view
90
app =new JFrame JavaDoc("GraphViewer");
91         app.setBounds(10,10,appWidth,appHight);
92         onscreenImage = new BufferedImage JavaDoc(appWidth, appHight, BufferedImage.TYPE_INT_RGB);
93         onscreen = (Graphics2D JavaDoc) onscreenImage.getGraphics();
94         clear();
95         onscreen.setColor(graphColor);
96         ImageIcon JavaDoc icon = new ImageIcon JavaDoc(onscreenImage);
97         app.setContentPane(new JLabel JavaDoc(icon));
98         app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
99         app.setVisible(true);
100
101         // init graph data
102
data=new LinkedList JavaDoc();
103         for (int i = 0; i < graphwidth; i++) {
104             data.addLast(new Integer JavaDoc(0));
105         }
106
107         // draw initial data
108
drawData();
109
110         // tests
111
try {
112             Thread.sleep(2000);
113         } catch (InterruptedException JavaDoc e) {
114             // TODO Auto-generated catch block
115
e.printStackTrace();
116         }
117
118         MantaInputStream in = new MantaInputStream();
119         in.connect("graph", MantaOutputStream.TOPIC);
120         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(in);
121         int input =0;
122         boolean go = true;
123         while(go){
124             try{
125                 input=dis.readInt();
126             }catch(IOException JavaDoc e){
127                 e.printStackTrace();
128                 go =false;
129             }
130             updateGraph(input);
131
132         }
133         System.out.println("done");
134
135     }//init
136

137     public void updateGraph(int newPointValue){
138         data.addLast(new Integer JavaDoc(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 JavaDoc)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 JavaDoc[] args) throws IOException JavaDoc {
160         GraphViewer view = new GraphViewer();
161         view.init();
162     }//main
163
}
164
Popular Tags