MOA 12.03
Real Time Analytics for Data Streams
StreamPanel.java
Go to the documentation of this file.
00001 /*
00002  *    StreamPanel.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 
00021 package moa.gui.visualization;
00022 
00023 import java.awt.Color;
00024 import java.awt.Component;
00025 import java.awt.Dimension;
00026 import java.awt.Graphics;
00027 import java.awt.Graphics2D;
00028 import java.awt.Point;
00029 import java.awt.RenderingHints;
00030 import java.awt.event.ComponentEvent;
00031 import java.awt.event.ComponentListener;
00032 import java.awt.image.BufferedImage;
00033 import java.awt.image.RescaleOp;
00034 import java.io.BufferedWriter;
00035 import java.io.File;
00036 import java.io.FileWriter;
00037 import java.io.IOException;
00038 import java.io.PrintWriter;
00039 import java.util.ArrayList;
00040 import java.util.logging.Level;
00041 import java.util.logging.Logger;
00042 import javax.imageio.ImageIO;
00043 import javax.swing.JPanel;
00044 import javax.swing.JScrollPane;
00045 import moa.cluster.Clustering;
00046 import moa.cluster.SphereCluster;
00047 
00048 public class StreamPanel extends JPanel implements ComponentListener{
00049 
00050     private ClusterPanel highlighted_cluster = null;
00051     private double zoom_factor = 0.2;
00052     private int zoom = 1;
00053     private int width_org;
00054     private int height_org;
00055     private int activeXDim = 0;
00056     private int activeYDim = 1;
00057 
00058     
00059     private JPanel layerPoints;
00060     private JPanel layerMicro;
00061     private JPanel layerMacro;
00062     private JPanel layerGroundTruth;
00063 
00064     //Buffered Image stuff
00065     private BufferedImage pointCanvas;
00066     private pointCanvasPanel layerPointCanvas;
00067     private boolean pointsVisible = true;
00068     private boolean ANTIALIAS = false;
00069 
00070 
00071     class pointCanvasPanel extends JPanel{
00072         BufferedImage image = null;
00073         public void setImage(BufferedImage image){
00074             setSize(image.getWidth(), image.getWidth());
00075             this.image = image;
00076         }
00077         @Override
00078         protected void paintComponent(Graphics g) {
00079             Graphics2D g2 = (Graphics2D)g;
00080             if(image!=null)
00081                 g2.drawImage(image, null, 0, 0);
00082         }
00083     }
00084 
00085 
00087     public StreamPanel() {
00088         initComponents();
00089 
00090         layerPoints = getNewLayer();
00091         layerPoints.setOpaque(true);
00092         layerPoints.setBackground(Color.white);
00093         
00094         layerMicro = getNewLayer();
00095         layerMacro = getNewLayer();
00096         layerGroundTruth = getNewLayer();
00097 
00098         add(layerMacro);
00099         add(layerMicro);
00100         add(layerGroundTruth);
00101         add(layerPoints);
00102 
00103         layerPointCanvas = new pointCanvasPanel();
00104         add(layerPointCanvas);
00105         
00106         addComponentListener(this);
00107         
00108     }
00109 
00110 
00111     private JPanel getNewLayer(){
00112         JPanel layer = new JPanel();
00113         layer.setOpaque(false);
00114         layer.setLayout(null);
00115         return layer;
00116     }
00117 
00118 
00119     public void drawMicroClustering(Clustering clustering, Color color){
00120         drawClustering(layerMicro, clustering, color);
00121     }
00122     
00123     public void drawMacroClustering(Clustering clustering, Color color){
00124         drawClustering(layerMacro, clustering, color);
00125     }
00126     public void drawGTClustering(Clustering clustering, Color color){
00127         drawClustering(layerGroundTruth, clustering, color);
00128     }
00129 
00130     public void setMicroLayerVisibility(boolean visibility){
00131         layerMicro.setVisible(visibility);
00132     }
00133     public void setMacroLayerVisibility(boolean visibility){
00134         layerMacro.setVisible(visibility);
00135     }
00136     public void setGroundTruthLayerVisibility(boolean visibility){
00137         layerGroundTruth.setVisible(visibility);
00138     }
00139     public void setPointVisibility(boolean visibility){
00140         pointsVisible = visibility;
00141         layerPoints.setVisible(visibility);
00142         if(!visibility)
00143             layerPointCanvas.setVisible(false);
00144     }
00145 
00146     void drawPointPanels(ArrayList<DataPoint> points, int timestamp, double decay_rate, double decay_threshold) {
00147         for(int p = 0; p < points.size(); p++){
00148             PointPanel pointPanel = new PointPanel(points.get(p),decay_rate,decay_threshold);
00149             layerPoints.add(pointPanel);
00150             pointPanel.updateLocation();
00151         }
00152         layerPointCanvas.setVisible(false);
00153         layerPoints.setVisible(pointsVisible);
00154     }
00155 
00156     public void drawPoint(DataPoint point){
00157         layerPointCanvas.setVisible(pointsVisible);
00158         layerPoints.setVisible(false);
00159         if(!pointsVisible)
00160             return;
00161 
00162         Graphics2D imageGraphics = (Graphics2D) pointCanvas.createGraphics();
00163 
00164         if (ANTIALIAS) {
00165             imageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
00166                     RenderingHints.VALUE_ANTIALIAS_ON);
00167         }
00168 
00169 
00170         int size = Math.min(getWidth(), getHeight());
00171         int x = (int) Math.round(point.value(getActiveXDim()) * size);
00172         int y = (int) Math.round(point.value(getActiveYDim()) * size);
00173 
00174         Color c = PointPanel.getPointColorbyClass((int)point.classValue(), 10);
00175         imageGraphics.setColor(c);
00176         int psize = PointPanel.POINTSIZE;
00177         int poffset = 2;
00178         imageGraphics.drawOval(x - poffset, y - poffset, psize, psize);
00179         imageGraphics.fillOval(x - poffset, y - poffset, psize, psize);
00180 
00181         layerPointCanvas.repaint();
00182     }
00183 
00184 
00185     private void drawCanvansClustering(JPanel layer, Graphics2D imageGraphics){
00186         for(Component comp :layer.getComponents()){
00187             if(comp instanceof ClusterPanel){
00188                 ClusterPanel cp = (ClusterPanel)comp;
00189                 cp.drawOnCanvas(imageGraphics);
00190             }
00191         }
00192     }
00193 
00194 
00195     public void applyDrawDecay(float factor){
00196 
00197         RescaleOp brightenOp = new RescaleOp(1f, 150f/factor, null);
00198         pointCanvas = brightenOp.filter(pointCanvas, null);
00199 
00200         layerPointCanvas.setImage(pointCanvas);
00201         layerPointCanvas.repaint();
00202     }
00203 
00204     private void drawClustering(JPanel layer, Clustering clustering, Color color){
00205         layer.removeAll();
00206         for (int c = 0; c < clustering.size(); c++) {
00207             SphereCluster cluster = (SphereCluster)clustering.get(c);
00208 
00209             ClusterPanel clusterpanel = new ClusterPanel(cluster, color, this);
00210             
00211             layer.add(clusterpanel);
00212             clusterpanel.updateLocation();
00213         }
00214 
00215         if(layer.isVisible() && pointsVisible){
00216             Graphics2D imageGraphics = (Graphics2D) pointCanvas.createGraphics();
00217             imageGraphics.setColor(color);
00218             drawCanvansClustering(layer, imageGraphics);
00219             layerPointCanvas.repaint();
00220         }
00221 
00222         layer.repaint();
00223     }
00224 
00225     public void screenshot(String filename, boolean svg, boolean png){
00226         if(layerPoints.getComponentCount()==0 && layerMacro.getComponentCount()==0 && layerMicro.getComponentCount()==0)
00227                 return;
00228         
00229         BufferedImage image = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
00230         if(png){
00231             synchronized(getTreeLock()){
00232                 Graphics g = image.getGraphics();
00233                 paintAll(g);
00234                 try {
00235                     ImageIO.write(image, "png", new File(filename+".png"));
00236                 } catch (Exception e) {
00237                     e.printStackTrace();
00238                 }
00239             }
00240         }
00241         if(svg){
00242             try {
00243                 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename+".svg")));
00244                 int width = 500;
00245                 out.write("<?xml version=\"1.0\"?>\n");
00246                 out.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
00247                 out.write("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\""+width+"\" height=\""+width+"\">\n");
00248 
00249                 if(layerMicro.isVisible()){
00250                     for(Component comp :layerMicro.getComponents()){
00251                         if(comp instanceof ClusterPanel)
00252                             out.write(((ClusterPanel)comp).getSVGString(width));
00253                     }
00254                 }
00255 
00256                 if(layerMacro.isVisible()){
00257                     for(Component comp :layerMacro.getComponents()){
00258                         if(comp instanceof ClusterPanel)
00259                             out.write(((ClusterPanel)comp).getSVGString(width));
00260                     }
00261                 }
00262 
00263                 if(layerGroundTruth.isVisible()){
00264                     for(Component comp :layerGroundTruth.getComponents()){
00265                         if(comp instanceof ClusterPanel)
00266                             out.write(((ClusterPanel)comp).getSVGString(width));
00267                     }
00268                 }
00269 
00270                 if(layerPoints.isVisible()){
00271                     for(Component comp :layerPoints.getComponents()){
00272                         if(comp instanceof PointPanel){
00273                             PointPanel pp = (PointPanel) comp;
00274                             out.write(pp.getSVGString(width));
00275                         }
00276                     }
00277                 }
00278                 
00279                 out.write("</svg>");
00280                 out.close();
00281             } catch (IOException ex) {
00282                 Logger.getLogger(StreamPanel.class.getName()).log(Level.SEVERE, null, ex);
00283             }
00284         }
00285     }
00286 
00287     public ClusterPanel getHighlightedClusterPanel(){
00288         return highlighted_cluster;
00289     }
00290 
00291     public void setHighlightedClusterPanel(ClusterPanel clusterpanel){
00292         highlighted_cluster = clusterpanel;
00293         repaint();
00294     }
00295 
00296     public void setZoom(int x, int y, int zoom_delta, JScrollPane scrollPane){
00297         
00298         if(zoom ==1){
00299             width_org = getWidth();
00300             height_org = getHeight();
00301         }
00302         zoom+=zoom_delta;
00303         
00304         if(zoom<1) zoom = 1;
00305         else{
00306             int size = (int)(Math.min(width_org, height_org)*zoom_factor*zoom);
00307 
00308             setSize(new Dimension(size*zoom, size*zoom));
00309             setPreferredSize(new Dimension(size*zoom, size*zoom));
00310 
00311             scrollPane.getViewport().setViewPosition(new Point((int)(x*zoom_factor*zoom+x),(int)( y*zoom_factor*zoom+y)));
00312         }
00313     }
00314 
00315     public int getActiveXDim() {
00316         return activeXDim;
00317     }
00318 
00319     public void setActiveXDim(int activeXDim) {
00320         this.activeXDim = activeXDim;
00321     }
00322 
00323     public int getActiveYDim() {
00324         return activeYDim;
00325     }
00326 
00327     public void setActiveYDim(int activeYDim) {
00328         this.activeYDim = activeYDim;
00329     }
00330 
00336     @SuppressWarnings("unchecked")
00337     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00338     private void initComponents() {
00339 
00340         setBackground(new java.awt.Color(255, 255, 255));
00341         addMouseListener(new java.awt.event.MouseAdapter() {
00342             public void mouseClicked(java.awt.event.MouseEvent evt) {
00343                 formMouseClicked(evt);
00344             }
00345         });
00346 
00347         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
00348         this.setLayout(layout);
00349         layout.setHorizontalGroup(
00350             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00351             .addGap(0, 400, Short.MAX_VALUE)
00352         );
00353         layout.setVerticalGroup(
00354             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00355             .addGap(0, 300, Short.MAX_VALUE)
00356         );
00357     }// </editor-fold>//GEN-END:initComponents
00358 
00359     private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
00360         if(highlighted_cluster!=null){
00361             highlighted_cluster.highlight(false);
00362             highlighted_cluster=null;
00363         }
00364     }//GEN-LAST:event_formMouseClicked
00365 
00366 
00367     // Variables declaration - do not modify//GEN-BEGIN:variables
00368     // End of variables declaration//GEN-END:variables
00369 
00370     
00371     
00372     public void componentHidden(ComponentEvent e) {
00373     }
00374 
00375     public void componentMoved(ComponentEvent e) {
00376     }
00377 
00378     public void componentResized(ComponentEvent e) {
00379         //System.out.println(e.getComponent().getClass().getName() + " --- Resized ");
00380 
00381         int size = Math.min(getWidth(), getHeight());
00382         layerMicro.setSize(new Dimension(size,size));
00383         layerMacro.setSize(new Dimension(size,size));
00384         layerGroundTruth.setSize(new Dimension(size,size));
00385         layerPoints.setSize(new Dimension(size,size));
00386 
00387 
00388         pointCanvas = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
00389         layerPointCanvas.setSize(new Dimension(size,size));
00390         layerPointCanvas.setImage(pointCanvas);
00391 
00392         Graphics2D imageGraphics = (Graphics2D) pointCanvas.getGraphics();
00393         imageGraphics.setColor(Color.white);
00394         imageGraphics.fillRect(0, 0, getWidth(), getHeight());
00395         imageGraphics.dispose();
00396 
00397     }
00398 
00399     public void componentShown(ComponentEvent e) {
00400     }
00401 
00402 
00403 }
 All Classes Namespaces Files Functions Variables Enumerations