/*
* Copyright 2006-2007 Columbia University.
*
* This file is part of MEAPsoft.
*
* MEAPsoft is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MEAPsoft is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MEAPsoft; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
* See the file "COPYING" for the text of the license.
*/
package com.meapsoft.visualizer;
import java.awt.Graphics;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JFrame;
import com.meapsoft.EDLFile;
import com.meapsoft.FeatChunk;
import com.meapsoft.FeatFile;
import com.meapsoft.disgraced.ColorMap;
/**
*
* @author douglas@music.columbia.edu
*
*/
public class SingleFeatureLineGraphPanel extends SingleFeaturePanel
{
private static final long serialVersionUID = 1L;
ColorMap colormap;
public SingleFeatureLineGraphPanel()
{
numDrawableFeatures = -1;
}
public SingleFeatureLineGraphPanel(String[] args)
{
super();
parseCommands(args);
numDrawableFeatures = -1;
setProgress(new DefaultBoundedRangeModel());
String fileName = args[args.length - 2];
FeatFile fF = null;
EDLFile eF = null;
String featName = args[args.length-1];
try
{
fF = new FeatFile(fileName);
fF.readFile();
if (edlFileName != null)
{
System.out.println("making EDLFile from " + edlFileName);
eF = new EDLFile(edlFileName);
eF.readFile();
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
if (initialize(fF, eF, featName) == -1)
{
System.out.println("hmm, something wrong, bailing.");
return;
}
repaint();
if (pngOutputFileName != null)
{
writeImageToPNG(pngOutputFileName);
System.exit(0);
}
}
public int initialize(FeatFile featFile, String featureName)
{
int error = initialize(featFile, null, featureName);
if (error < 0)
return error;
colormap = ColorMap.getJet(featureSize);
return 1;
}
@Override
public int initialize(FeatFile featFile, EDLFile edlFile, String featureName)
{
int error = initialize(featFile, edlFile, featureName);
if (error < 0)
return error;
colormap = ColorMap.getJet(featureSize);
return 1;
}
public String getDisplayType()
{
return "LineGraph";
}
// stub class, implemented from abstract parent class
public void updateData()
{
}
public void drawData(Graphics g)
{
double zoomMulti = (zoomLevel * 4.0) / 4.0;
int w = (int) (this.getWidth() * zoomMulti);
int h = this.getHeight();
double yScaler = h / featureRange;
double xScaler = w / timeRange;
g.setColor(bgColor);
g.fillRect(0, 0, w, h);
g.setColor(fGColor);
for (int j = 0; j < featureSize; j++)
{
int xPrev = 0;
int yPrev = 0;
if (featureSize > 1)
g.setColor(colormap.table[j]);
else
g.setColor(fGColor);
double localFirstEventTime = ((FeatChunk) events.get(firstChunkToDraw)).startTime;
int x = 0;
for (int i = firstChunkToDraw; i < numChunks && x < getWidth(); i++)
{
FeatChunk fC = (FeatChunk) events.get(i);
double dX = ((fC.startTime - localFirstEventTime) * xScaler);
x = (int) dX;
int xMid = (int)(dX + (fC.length * xScaler * 0.5));
// adjust to zero
//double[] features = featFile.getFeatureByName(featureName, i);
double[] features = fC.getFeatureByName(featureName);
double dataPoint = features[j] - lowestValue;
int y = h - (int) (dataPoint * yScaler);
// don't draw the first one to avoid false line
if (i == firstChunkToDraw)
{
g.drawLine(0, y, xMid, y);
}
else
{
g.drawLine(xPrev, yPrev, xMid, y);
}
//do last little segment on last chunk
if (i == numChunks - 1)
{
g.drawLine(xMid, y, w, y);
}
xPrev = xMid;
yPrev = y;
}
}
}
public static void printUsageAndExit()
{
System.out
.println("Usage: SingleFeatureLineGraphPanel [-options] featuresFile.feat FeatureName \n\n"
+ " where options include:\n"
+ getStandardUsage()
);
System.out.println();
System.exit(0);
}
public static void main(String[] args)
{
if (args.length < 2)
{
printUsageAndExit();
}
SingleFeatureLineGraphPanel sFP = new SingleFeatureLineGraphPanel(args);
if (sFP.pngOutputFileName == null)
{
JFrame frame = new JFrame("SingleFeatureLineGraphPanel");
frame.setContentPane(sFP);
frame.pack();
//frame.setBounds(100, 100, 600, 400);
frame.setVisible(true);
}
}
}