Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

LineGraphRenderer.java

00001 /*
00002  *  Copyright 2006-2007 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 /*
00024  * Created on Nov 15, 2006
00025  *
00026  * various ways of viewing EDL files
00027  * 
00028  */
00029 package com.meapsoft.visualizer;
00030 
00031 import java.awt.Color;
00032 import java.awt.Graphics;
00033 import java.awt.Point;
00034 import java.awt.Rectangle;
00035 import java.awt.image.BufferedImage;
00036 import java.util.Vector;
00037 
00038 import javax.swing.BoxLayout;
00039 import javax.swing.JCheckBox;
00040 import javax.swing.JComboBox;
00041 import javax.swing.JLabel;
00042 import javax.swing.JPanel;
00043 
00044 import com.meapsoft.EDLFile;
00045 import com.meapsoft.FeatFile;
00046 
00047 
00053 public class LineGraphRenderer extends Renderer
00054 {               
00055         JComboBox sOptions;
00056         JCheckBox connectOption;
00057         boolean connect;
00058         
00059         double currXMulti = 0.0;
00060         double currHeight = 0.0;
00061         
00062         public LineGraphRenderer(FeatFile featFile, EDLFile eDLFile)
00063         {
00064                 super(featFile, eDLFile, "LineGraph");
00065         }
00066 
00067         public LineGraphRenderer(Renderer r)
00068         {
00069                 super(r);
00070                 //have to do this so we're not stuck with invalid
00071                 //multipliers from another renderer
00072                 updateColorMultipliers();
00073         }
00074 
00075         public void draw(BufferedImage image, int width, int height)
00076         {       
00077                 //System.out.println("width: " + width + " height: " + height);
00078                 //height *= .5;
00079                 int numChunks = events.size();
00080                 
00081                 //set up scaling factors
00082                 ChunkVisInfo lastChunk = (ChunkVisInfo) events.elementAt(numChunks - 1);
00083                 double xMulti = width/(lastStartTime + lastChunk.length);
00084                 
00085                 Graphics graphics = image.getGraphics();
00086                 graphics.setColor(Color.white);
00087                 graphics.fillRect(0,0,width,height);
00088                 
00089                 Point[][] previousPoints = new Point[numChunks][numFeatures];
00090                 
00091                 int colorMulti = numColors/numFeatures;
00092                 
00093                 for (int i = 0; i < numChunks; i++)
00094                 {
00095                         ChunkVisInfo cVI = (ChunkVisInfo) events.elementAt(i);
00096                         
00097                         int[] featNum = {0};
00098                         
00099                         for (int j = 0; j < numFeatures; j++)
00100                         {
00101                                 double features[] = cVI.getFeatures(featNum);
00102                                 double featureValue = features[0] - lowestFeatureValue[j];
00103 
00104                                 featNum[0] += elementsPerFeature[j];
00105 
00106                                 //double colorIndex = featureValue * colorMultipliers[j];
00107                                 
00108                                 int colorIndex = j * colorMulti;
00109                                 
00110                                 
00111                                 Color featureColor = colormap.table[(int)colorIndex];
00112                                 
00113                                 if (!cVI.selected)
00114                                         featureColor = featureColor.darker().darker().darker();
00115                                 
00116                                 graphics.setColor(featureColor);
00117                                 
00118                                 int blobSize = (int)Math.round(width/numChunks) - 2;
00119                                 if (blobSize <= 1)
00120                                         blobSize = 2;
00121                                         
00122                                 double yMulti = (height - blobSize)/(highestFeatureValue[j] - lowestFeatureValue[j]);
00123 
00124                                 //draw feats
00125                                 int x = (int)(Math.round(cVI.startTime * xMulti));
00126                                 //or draw EDL
00127                                 if (sOptions.getSelectedIndex() == 1)
00128                                         x = (int)(Math.round(cVI.dstTime * xMulti));
00129                                 int y = (int)(Math.round(featureValue * yMulti));
00130                                 
00131                                 graphics.fillOval(x, y, blobSize, blobSize);
00132                                 
00133                                 if (i > 0 && connectOption.isSelected())
00134                                 {
00135                                         Point pP = previousPoints[i-1][j];
00136                                         if (pP != null)
00137                                         {
00138                                                 int pX = pP.x + (blobSize/2);
00139                                                 int pY = pP.y + (blobSize/2);
00140                                                 graphics.setColor(featureColor.brighter());
00141                                                 graphics.drawLine(x + (blobSize/2), y + (blobSize/2), pX, pY);
00142                                         }
00143                                 }
00144                                 previousPoints[i][j] = new Point(x, y);
00145                                 
00146                                 //record our position
00147                                 if (j == 0)
00148                                 {
00149                                         if (sOptions.getSelectedIndex() == 0)
00150                                         {
00151                                                 cVI.xFeat = x;
00152                                                 cVI.yFeat = y;
00153                                         }
00154                                         else
00155                                         {
00156                                                 cVI.xEDL = x;
00157                                                 cVI.yEDL = y;
00158                                         }
00159                                         cVI.width = blobSize;
00160                                         cVI.height = blobSize;
00161                                 }
00162                         }
00163                 }
00164                 
00165                 currHeight = height;
00166                 
00167                 if (dragRect != null)
00168                 {
00169                         graphics.setColor(Color.black);
00170                         graphics.drawRect(dragRect.x, dragRect.y, dragRect.width, dragRect.height);
00171                 }
00172         }
00173 
00174                 
00175         public Vector getChunkVisInfosForPoint(Point p)
00176         {       
00177                 Vector chunks = new Vector();
00178                 
00179                 for (int i = 0; i < events.size(); i++)
00180                 {
00181                         ChunkVisInfo c = (ChunkVisInfo) events.elementAt(i);
00182 
00183                         if (sOptions.getSelectedIndex() == 0)
00184                         {
00185                                 if (p.x >= c.xFeat && p.x <= (c.xFeat + c.width))
00186                                         chunks.add(c);
00187                         }
00188                         else
00189                         {
00190                                 if (p.x >= c.xEDL && p.x <= (c.xEDL + c.width))
00191                                         chunks.add(c);
00192                         }
00193                 
00194                 }
00195 
00196                 return chunks;
00197         }
00198                 
00199         public int getFeatureNumberForPoint(Point p)
00200         {
00201                 int featureNumber = -1;
00202                 
00203                 if (featFile != null)
00204                 {
00205                         for (int i = 0; i < events.size(); i++)
00206                         {
00207                                 ChunkVisInfo cVI = (ChunkVisInfo) events.elementAt(i);
00208                                 
00209                                 if (sOptions.getSelectedIndex() == 0)
00210                                 {
00211                                         if (p.x >= cVI.xFeat && p.x <= (cVI.xFeat + cVI.width))
00212                                         {
00213                                                 int[] featNum = {0};
00214                         
00215                                                 for (int j = 0; j < numFeatures; j++)
00216                                                 {
00217                                                         double features[] = cVI.getFeatures(featNum);
00218                                                         double featureValue = features[0] - lowestFeatureValue[j];
00219         
00220                                                         featNum[0] += elementsPerFeature[j];
00221         
00222                                                         double yMulti = currHeight/(highestFeatureValue[j] - lowestFeatureValue[j]);
00223                                                         int y = (int)(Math.round(featureValue * yMulti));
00224                                                         
00225                                                         if (p.y >= y && p.y <= y + cVI.height)
00226                                                                 featureNumber = j;
00227                                                 }
00228                                         }
00229                                 }
00230                                 else
00231                                 {
00232                                         if (p.x >= cVI.xEDL && p.x <= (cVI.xEDL + cVI.width))
00233                                         {
00234                                                 int[] featNum = {0};
00235                         
00236                                                 for (int j = 0; j < numFeatures; j++)
00237                                                 {
00238                                                         double features[] = cVI.getFeatures(featNum);
00239                                                         double featureValue = features[0] - lowestFeatureValue[j];
00240         
00241                                                         featNum[0] += elementsPerFeature[j];
00242         
00243                                                         double yMulti = currHeight/(highestFeatureValue[j] - lowestFeatureValue[j]);
00244                                                         int y = (int)(Math.round(featureValue * yMulti));
00245                                                         
00246                                                         if (p.y >= y && p.y <= y + cVI.height)
00247                                                                 featureNumber = j;
00248                                                 }
00249                                         }
00250                                 }
00251                         }
00252                 }
00253                 
00254                 return featureNumber;
00255         }
00256         
00257         public String getFeatureNameForPoint(Point p)
00258         {
00259                 String featureName = "i don't know!";
00260                 
00261                 int whichFeature = getFeatureNumberForPoint(p);
00262                 
00263                 if (whichFeature != -1)
00264                 {
00265                         String fullFeatureName = (String) featFile.featureDescriptions.elementAt(whichFeature);
00266                         //we're splitting on "." but have to use an escape sequence!
00267                         String[] chunks = fullFeatureName.split("\\.");
00268                         featureName = chunks[chunks.length-1];
00269                 }
00270                         
00271                 return featureName;     
00272         }
00273 
00274         public double getFeatureValueForPoint(Point p)
00275         {
00276                 double value = 0.0;
00277                 
00278                 int whichFeature = getFeatureNumberForPoint(p);
00279                 
00280                 if (whichFeature != -1)
00281                 {
00282                         for (int i = 0; i < events.size(); i++)
00283                         {
00284                                 ChunkVisInfo cVI = (ChunkVisInfo) events.elementAt(i);
00285                                 
00286                                 if (sOptions.getSelectedIndex() == 0)
00287                                 {
00288                                         if (p.x >= cVI.xFeat && p.x <= (cVI.xFeat + cVI.width))
00289                                         {
00290                                                 if (p.y >= cVI.yFeat && p.y <= (cVI.yFeat + cVI.height))
00291                                                 {
00292                                                         int[] featNum = {0};
00293                                                 
00294                                                         for (int j = 0; j < whichFeature; j++)
00295                                                                 featNum[0] += elementsPerFeature[j];
00296                                                         value = cVI.getFeatures(featNum)[0];
00297                                                         return value;
00298                                                 }
00299                                         }
00300                                 }
00301                                 else
00302                                 {
00303                                         if (p.x >= cVI.xEDL && p.x <= (cVI.xEDL + cVI.width))
00304                                         {
00305                                                 if (p.y >= cVI.yEDL && p.y <= (cVI.yEDL + cVI.height))
00306                                                 {
00307                                                         int[] featNum = {0};
00308                                                 
00309                                                         for (int j = 0; j < whichFeature; j++)
00310                                                                 featNum[0] += elementsPerFeature[j];
00311                                                         value = cVI.getFeatures(featNum)[0];
00312                                                         return value;
00313                                                 }
00314                                         }
00315                                 }
00316                         }
00317                 }
00318                 
00319                 return value;
00320         }
00321         
00322         public void rangeFilterSelectionChanged()
00323         {
00324         }
00325         
00326         public void setDragRect(Rectangle r, boolean dS)
00327         {
00328                 dragRect = r;
00329                 dragShift = dS;
00330                 
00331                 if (!dragShift)
00332                 {
00333                         selectNone();
00334                 }
00335                 
00336                 for (int i = 0; i < events.size(); i++)
00337                 {
00338                         ChunkVisInfo cVI = (ChunkVisInfo)events.elementAt(i);
00339                         Rectangle chunkRect = null;
00340                         
00341                         if (sOptions.getSelectedIndex() == 0)
00342                         {
00343                                 chunkRect = new Rectangle(cVI.xFeat, cVI.yFeat, cVI.width, cVI.height);
00344                         }
00345                         else
00346                         {
00347                                 chunkRect = new Rectangle(cVI.xEDL, cVI.yEDL, cVI.width, cVI.height);
00348                         }
00349                         
00350                         if ((dragRect.x >= chunkRect.x && 
00351                                 dragRect.x <= chunkRect.x + chunkRect.width) ||
00352                                 (dragRect.x + dragRect.width >= chunkRect.x && 
00353                                 dragRect.x + dragRect.width <= chunkRect.x + chunkRect.width) ||
00354                                 (dragRect.x <= chunkRect.x && 
00355                                 dragRect.x + dragRect.width >= chunkRect.x + chunkRect.width))
00356                                 cVI.selected = true;
00357                 }
00358                 
00359                 dragRect = null;
00360                 dragShift = false;
00361                 
00362                 drawingPanel.repaint(); 
00363         }
00364         /*
00365         public void actionPerformed(ActionEvent arg0)
00366         {               
00367                 Object source = arg0.getSource();
00368                 String command = arg0.getActionCommand();
00369                 
00370                 super.actionPerformed(arg0);
00371         }
00372         */
00373         public JPanel buildGUI(Color bgColor)
00374         {
00375                 
00376                 JPanel panel = super.buildGUI(bgColor);
00377                 
00378                 JPanel cSPanel = new JPanel();
00379                 cSPanel.setBackground(bgColor);
00380                 cSPanel.setLayout(new BoxLayout(cSPanel, BoxLayout.Y_AXIS));
00381                 
00382                 connectOption = new JCheckBox("connect dots");
00383                 connectOption.setBackground(bgColor);
00384                 connectOption.setActionCommand("connect");
00385                 connectOption.addActionListener(this);
00386                 
00387                 cSPanel.add(connectOption);
00388 
00389                 JLabel sCL = new JLabel("show: ");
00390                 sCL.setBackground(bgColor);
00391                 cSPanel.add(sCL);
00392                 
00393                 String[] showOptions = {"feat file", "EDL file"};
00394                 
00395                 sOptions = new JComboBox(showOptions);
00396                 sOptions.setBackground(bgColor);
00397                 sOptions.setActionCommand("sOptions");
00398                 sOptions.addActionListener(this);
00399                 cSPanel.add(sOptions);
00400                 
00401                 controlsPanel.add(cSPanel);
00402                 
00403                 return panel;
00404         }
00405 
00406 
00407 }
00408 
00409 
00410 

Generated on Tue Feb 6 19:02:26 2007 for MEAPsoft by doxygen1.2.18