MOA 12.03
Real Time Analytics for Data Streams
PointPanel.java
Go to the documentation of this file.
00001 /*
00002  *    PointPanel.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.Dimension;
00025 import java.awt.Graphics;
00026 import javax.swing.JPanel;
00027 
00028 public class PointPanel extends JPanel{
00029     static final int POINTSIZE = 4;
00030     DataPoint point;
00031 
00032     protected int x_dim = 0;
00033     protected int y_dim = 1;
00034     protected Color col;
00035     protected Color default_color = Color.BLACK;
00036 
00037     protected int panel_size;
00038     protected int window_size;
00039     protected boolean highligted = false;
00040 
00041     protected double decayRate;
00042     protected double decayThreshold;
00043 
00044 
00046     public PointPanel(DataPoint point, double decayRate, double decayThreshold) {
00047         this.point = point;
00048         this.col = Color.BLACK;
00049         this.panel_size = POINTSIZE;
00050         this.decayRate = decayRate;
00051         this.decayThreshold = decayThreshold;
00052         this.col = default_color;
00053 
00054         setVisible(true);
00055         setOpaque(false);
00056         setSize(new Dimension(1,1));
00057         setLocation(0,0);
00058         initComponents();
00059 
00060     }
00061 
00062 
00063 
00064     public void updateLocation(){
00065         window_size = Math.min(this.getParent().getWidth(),this.getParent().getHeight());
00066 
00067         StreamPanel sp = (StreamPanel)getParent().getParent();
00068         x_dim = sp.getActiveXDim();
00069         y_dim = sp.getActiveYDim();
00070 
00071         setSize(new Dimension(panel_size+1,panel_size+1));
00072         setLocation((int)(point.value(x_dim)*window_size-(panel_size/2)),(int)(point.value(y_dim)*window_size-(panel_size/2)));
00073     }
00074 
00080     @SuppressWarnings("unchecked")
00081     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00082     private void initComponents() {
00083 
00084         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
00085         this.setLayout(layout);
00086         layout.setHorizontalGroup(
00087             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00088             .addGap(0, 296, Short.MAX_VALUE)
00089         );
00090         layout.setVerticalGroup(
00091             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00092             .addGap(0, 266, Short.MAX_VALUE)
00093         );
00094     }// </editor-fold>//GEN-END:initComponents
00095 
00096     @Override
00097     protected void paintComponent(Graphics g) {
00098         point.updateWeight(RunVisualizer.getCurrentTimestamp(), decayRate);
00099         if(point.weight() < decayThreshold){
00100             getParent().remove(this);
00101             return;
00102         }
00103 
00104         Color color = getColor();
00105         Color errcolor = getErrorColor();
00106         if(errcolor == null){
00107             errcolor = color;
00108             panel_size = POINTSIZE;
00109         }
00110         else{
00111             panel_size = POINTSIZE+2;
00112         }
00113         
00114         updateLocation();
00115         g.setColor(errcolor);
00116         g.drawOval(0, 0, panel_size, panel_size);
00117         g.setColor(color);
00118         g.fillOval(0, 0, panel_size, panel_size);
00119 
00120         setToolTipText(point.getInfo(x_dim, y_dim));
00121 
00122     }
00123 
00124             
00125 
00126     private Color getErrorColor(){
00127         String cmdvalue = point.getMeasureValue("CMM");
00128         Color color = null;
00129         if(!cmdvalue.equals("")){
00130             double err = Double.parseDouble(cmdvalue);
00131             if(err > 0.00001){
00132                 if(err > 0.7) err = 1;
00133                 int alpha = (int)(100+155*err);
00134                 color = new Color(255, 0, 0, alpha);
00135             }
00136             if(err == 0.00001){
00137                 color = new Color(255, 0, 0,100);
00138             }
00139         }
00140         return color;
00141     }
00142 
00143     private Color getColor(){
00144         Color color = null;
00145 
00146         if(getParent() instanceof StreamPanel){
00147             StreamPanel sp = (StreamPanel) getParent();
00148             ClusterPanel cp = sp.getHighlightedClusterPanel();
00149             if(cp !=null){
00150                 if(cp.getClusterLabel()==point.classValue()){
00151                     color = Color.BLUE;
00152                 }
00153             }
00154         }
00155 
00156         if(color == null){
00157             int alpha = (int)(point.weight()*200+55);
00158             float numCl = 10;
00159             Color c = getPointColorbyClass((int)point.classValue(), numCl);
00160 
00161             color = new Color(c.getRed(),c.getGreen(),c.getBlue(),alpha);
00162         }
00163         return color;
00164     }
00165 
00166     public static Color getPointColorbyClass(int classValue, float numClasses){
00167         Color c;
00168         if(classValue != -1)
00169             c = new Color(Color.HSBtoRGB((float)((classValue+1)/numClasses), 1f, 240f/240));
00170         else
00171             c = Color.GRAY;
00172         return c;
00173     }
00174 
00175 
00176     public void highlight(boolean enabled){
00177         highligted = enabled;
00178         repaint();
00179     }
00180 
00181 
00182     public String getObjectInfo(){
00183         return point.getInfo(x_dim, y_dim);
00184     }
00185 
00186     @Override
00187     public String getToolTipText() {
00188         return super.getToolTipText();
00189     }
00190 
00191 
00192     // Variables declaration - do not modify//GEN-BEGIN:variables
00193     // End of variables declaration//GEN-END:variables
00194 
00195 
00196     public String getSVGString(int width){
00197         StringBuffer out = new StringBuffer();
00198 
00199         int x = (int)(point.value(x_dim)*window_size);
00200         int y = (int)(point.value(y_dim)*window_size);
00201         int radius = panel_size/2;
00202         //radius = 1;
00203 
00204         Color c = getColor();
00205 
00206         String color = "rgb("+c.getRed()+","+c.getGreen()+","+c.getBlue()+")";
00207         double trans = c.getAlpha()/255.0;
00208 
00209         out.append("<circle ");
00210         out.append("cx='"+x+"' cy='"+y+"' r='"+radius+"'");
00211         out.append(" stroke='"+color+"' stroke-width='0' fill='"+color+"' fill-opacity='"+trans+"' stroke-opacity='"+trans+"'/>");
00212         out.append("\n");
00213         return out.toString();
00214     }
00215 
00216 }
 All Classes Namespaces Files Functions Variables Enumerations