MOA 12.03
Real Time Analytics for Data Streams
ClusterPanel.java
Go to the documentation of this file.
00001 /*
00002  *    ClusterPanel.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 java.awt.Graphics2D;
00027 import javax.swing.JPanel;
00028 import moa.cluster.SphereCluster;
00029 
00030 public class ClusterPanel extends JPanel {
00031     private SphereCluster cluster;
00032 
00033     private double[] center;
00034     private final static int MIN_SIZE = 5;
00035     protected double decay_rate;
00036 
00037     protected int x_dim = 0;
00038     protected int y_dim = 1;
00039     protected Color col;
00040     protected Color default_color = Color.BLACK;
00041     protected double[] direction = null;
00042 
00043     protected StreamPanel streamPanel;
00044 
00045     protected int panel_size;
00046     protected int window_size;
00047     protected boolean highligted = false;
00048     private double r;
00049 
00050 
00051 
00054     public ClusterPanel(SphereCluster cluster, Color color, StreamPanel sp) {
00055         this.cluster = cluster;
00056         center = cluster.getCenter();
00057         r = cluster.getRadius();
00058         streamPanel = sp;
00059 
00060         default_color = col = color;
00061 
00062         setVisible(true);
00063         setOpaque(false);
00064         setSize(new Dimension(1,1));
00065         setLocation(0,0);
00066 
00067         initComponents();
00068     }
00069 
00070     public void setDirection(double[] direction){
00071         this.direction = direction;
00072     }
00073 
00074     public void updateLocation(){
00075         x_dim = streamPanel.getActiveXDim();
00076         y_dim = streamPanel.getActiveYDim();
00077 
00078         if(cluster!=null && center==null)
00079             getParent().remove(this);
00080         else{
00081             //size of the parent
00082             window_size = Math.min(streamPanel.getWidth(),streamPanel.getHeight());
00083 
00084             //scale down to diameter
00085             panel_size = (int) (2* r * window_size);
00086             if(panel_size < MIN_SIZE)
00087                     panel_size = MIN_SIZE;
00088 
00089             setSize(new Dimension(panel_size+1,panel_size+1));
00090             setLocation((int)(center[x_dim]*window_size-(panel_size/2)),(int)(center[y_dim]*window_size-(panel_size/2)));
00091             
00092         }
00093     }
00094 
00095     public void updateTooltip(){
00096         setToolTipText(cluster.getInfo());
00097     }
00098 
00099     @Override
00100     public boolean contains(int x, int y) {
00101         //only react on the hull of the cluster
00102         double dist = Math.sqrt(Math.pow(x-panel_size/2,2)+Math.pow(y-panel_size/2,2));
00103         if(panel_size/2 - 5 < dist && dist < panel_size/2 + 5)
00104             return true;
00105         else
00106             return false;
00107     }
00108 
00109 
00110 
00111 
00117     @SuppressWarnings("unchecked")
00118     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
00119     private void initComponents() {
00120 
00121         addMouseListener(new java.awt.event.MouseAdapter() {
00122             public void mouseClicked(java.awt.event.MouseEvent evt) {
00123                 formMouseClicked(evt);
00124             }
00125         });
00126 
00127         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
00128         this.setLayout(layout);
00129         layout.setHorizontalGroup(
00130             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00131             .addGap(0, 296, Short.MAX_VALUE)
00132         );
00133         layout.setVerticalGroup(
00134             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
00135             .addGap(0, 266, Short.MAX_VALUE)
00136         );
00137     }// </editor-fold>//GEN-END:initComponents
00138 
00139     private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
00140         streamPanel.setHighlightedClusterPanel(this);
00141     }//GEN-LAST:event_formMouseClicked
00142 
00143     @Override
00144     protected void paintComponent(Graphics g) {
00145         updateLocation();
00146         if(highligted){
00147             g.setColor(Color.BLUE);
00148         }
00149         else{
00150             g.setColor(default_color);
00151         }
00152         int c = (int)(panel_size/2);
00153 
00154         if(cluster.getId()>=0)
00155             g.drawString("C"+(int)cluster.getId(),c,c);
00156         
00157         g.drawOval(0, 0, panel_size, panel_size);
00158 
00159         if(direction!=null){
00160             double length = Math.sqrt(Math.pow(direction[0], 2) + Math.pow(direction[1], 2));
00161             g.drawLine(c, c, c+(int)((direction[0]/length)*panel_size), c+(int)((direction[1]/length)*panel_size));
00162         }
00163 
00164         updateTooltip();
00165         
00166 
00167     }
00168 
00169     public void highlight(boolean enabled){
00170         highligted = enabled;
00171         repaint();
00172     }
00173 
00174     public boolean isValidCluster(){
00175         return (center!=null);
00176     }
00177 
00178     public int getClusterID(){
00179         return (int)cluster.getId();
00180     }
00181 
00182     public int getClusterLabel(){
00183         return (int)cluster.getGroundTruth();
00184     }
00185 
00186 
00187     public String getSVGString(int width){
00188         StringBuffer out = new StringBuffer();
00189 
00190         int x = (int)(center[x_dim]*window_size);
00191         int y = (int)(center[y_dim]*window_size);
00192         int radius = panel_size/2;
00193         
00194         out.append("<circle ");
00195         out.append("cx='"+x+"' cy='"+y+"' r='"+radius+"'");
00196         out.append(" stroke='green' stroke-width='1' fill='white' fill-opacity='0' />");
00197         out.append("\n");
00198         return out.toString();
00199     }
00200 
00201     public void drawOnCanvas(Graphics2D imageGraphics){
00202         int x = (int)(center[x_dim]*window_size-(panel_size/2));
00203         int y = (int)(center[y_dim]*window_size-(panel_size/2));
00204         int radius = panel_size;
00205         imageGraphics.drawOval(x, y, radius, radius);
00206     }
00207 
00208 
00209     // Variables declaration - do not modify//GEN-BEGIN:variables
00210     // End of variables declaration//GEN-END:variables
00211 
00212 
00213 }
 All Classes Namespaces Files Functions Variables Enumerations