package SesameGUI; import java.util.Vector; import java.awt.*; import java.awt.geom.*; import java.io.FileWriter; public class Spectrum { public Spectrum() { scale = 1.0; tickers = null; origOffset = 0.0; samples = new Vector(); samplesInteractive = new Vector(); currentIndex = -1; spectraVisualProperties = new SpectrumVisualProperties(); windows = null; units = new String(); } public void setOrigOffset(double d) { origOffset = d; } public double getOrigOffset() { return origOffset; } public void setTickers(Vector v) { tickers = v; } public boolean isLog() { if (tickers == null) return false; return true; } public double getTickerAt(int ndx) { if (tickers == null) return 0.0; if (ndx < 0 || ndx >= tickers.size()) return 0.0; return ((Double)(tickers.get(ndx))).doubleValue(); } public String getUnits() { return units; } public void setUnits(String un) { units = un; } public void add(double rvalue) { currentIndex++; if (currentIndex >= samples.size()) samples.add(new Double(rvalue)); else samples.set(currentIndex, new Double(rvalue)); } public void insertAt(double rvalue , int index) { samples.insertElementAt(new Double(rvalue) , index); } public void setValues(Vector v) { samples = v; } public void setIgnoreTo(int it) { ignoreTo = it; } public int getNumberOfSamples() { return samplesInteractive.size(); } public String getInteractiveSample(int ndx) { return (String)samplesInteractive.get(ndx); } public void addSample(String rvalue) { samplesInteractive.add(new String(rvalue)); } public double getSamplingRate() { return samplingRate; } public void setSamplingRate(double samplingRate) { this.samplingRate = samplingRate; } public String getName() { return name; } public String getStationName() { return stationName; } public void setName(String name) { this.name = name; } public void setStationName(String name) { this.stationName = name; } public void draw(Graphics2D g, SpectrumView view, int selectedWindow) { DoubleWrapper x; DoubleWrapper x1; DoubleWrapper y; DoubleWrapper prevX; DoubleWrapper prevY; int i; // PR: fn escala int scaleLen = decimation; int scaleStep; x = new DoubleWrapper(); x1 = new DoubleWrapper(); y = new DoubleWrapper(); prevX = new DoubleWrapper(); prevY = new DoubleWrapper(); int startingIndex; int endingIndex; startingIndex = (int)(view.getScaleX().getLowerVisibleBound() / samplingRate); endingIndex = (int)(view.getScaleX().getUpperVisibleBound() / samplingRate) + 1; if (startingIndex < 0) startingIndex = 0; if (endingIndex >= samples.size()) endingIndex = samples.size() - 1; prevX.value = 0.0; prevY.value = ((Double)samples.get(startingIndex)).doubleValue() * scale; view.getScaleX().real2Pane(prevX); view.getScaleY().real2Pane(prevY); double offset; if (view.getScaleY().toString().equals("LogarithmicVerticalScale")) offset = 0.0; else offset = view.getScaleY().getOrigin() * (-1.0); scaleStep = (endingIndex - startingIndex) / scaleLen; x.value = samplingRate; x1.value = startingIndex * samplingRate; startingIndex++; if (scaleStep < 1) scaleStep = 1; Paint oldPaint; Stroke oldStroke; BasicStroke newStroke; oldPaint = g.getPaint(); oldStroke = g.getStroke(); g.setPaint(spectraVisualProperties.getLineColor()); float dash1[] = {3.5f}; switch(spectraVisualProperties.getLinePattern()) { case 1: dash1 = null; break; case 2: dash1 = new float [] {3.0f , 1.0f , 1.0f , 1.0f}; break; case 3: dash1 = new float [] {1.0f}; break; } newStroke = new BasicStroke(spectraVisualProperties.getLineThickness() , BasicStroke.CAP_BUTT , BasicStroke.JOIN_MITER , 10f , dash1 , 0.0f); g.setStroke(newStroke); for (i = startingIndex + 1; i <= endingIndex; i += scaleStep) { y.value = (((Double)samples.get(i)).doubleValue() * scale) + offset; view.getScaleX().real2Pane(x); view.getScaleY().real2Pane(y); if (i > ignoreTo) g.draw(new Line2D.Double(prevX.value, prevY.value, x.value, y.value)); prevX.value = x.value; prevY.value = y.value; x.value = (samplingRate * ((i - startingIndex) + 1)); } g.setPaint(oldPaint); g.setStroke(oldStroke); if (windows != null) windows.draw(x, x1, view, y, g, selectedWindow); } public void drawLegend(Graphics2D g, SpectrumView view, int posX , int posY , int height) { Paint oldPaint; Stroke oldStroke; BasicStroke newStroke; oldPaint = g.getPaint(); oldStroke = g.getStroke(); g.setPaint(spectraVisualProperties.getLineColor()); float dash1[] = {3.5f}; switch(spectraVisualProperties.getLinePattern()) { case 1: dash1 = null; break; case 2: dash1 = new float [] {3.0f , 1.0f , 1.0f , 1.0f}; break; case 3: dash1 = new float [] {1.0f}; break; } newStroke = new BasicStroke(spectraVisualProperties.getLineThickness() , BasicStroke.CAP_BUTT , BasicStroke.JOIN_MITER , 10f , dash1 , 0.0f); g.setStroke(newStroke); g.draw(new Line2D.Double(posX , posY - (height / 2) , posX + 20 , posY - (height / 2))); g.setPaint(oldPaint); g.setStroke(oldStroke); if (name != null) { g.scale(1.0, -1.0); g.drawString(name , posX + 30 , ((posY + 8) - height) * (-1)); g.scale(1.0, -1.0); } } public void setSize(int newSize) { samples.setSize(newSize); } public int getSize() { return samples.size(); } public double valueAt(int ndx) { if (ndx >= samples.size()) ndx = samples.size() - 1; if (ndx < 0) ndx = 0; return ((Double)samples.get(ndx)).doubleValue() * scale; } public SpectrumVisualProperties getSpectraVisualProperties() { return spectraVisualProperties; } public void initializeScale(Scale xScale, Scale yScale, boolean all) { int i; xScale.setLowerBound(0); if (samples.size() * getSamplingRate() > xScale.getUpperBound()) xScale.setUpperBound(samples.size() * getSamplingRate()); if (all) { xScale.setLowerVisibleBound(0); xScale.setUpperVisibleBound(xScale.getUpperBound()); } xScale.calcStep(10); if (yScale.getLowerBound() == yScale.getUpperBound()) { yScale.setLowerBound(((Double)samples.get(0)).doubleValue() * scale); yScale.setUpperBound(((Double)samples.get(0)).doubleValue() * scale); } yMin = ((Double)samples.get(0)).doubleValue() * scale; yMax = ((Double)samples.get(0)).doubleValue() * scale; for (i = 1; i < samples.size(); i++) { if (yScale.getLowerBound() > ((Double)samples.get(i)).doubleValue() * scale) yScale.setLowerBound(((Double)samples.get(i)).doubleValue() * scale); if (yScale.getUpperBound() < ((Double)samples.get(i)).doubleValue() * scale) yScale.setUpperBound(((Double)samples.get(i)).doubleValue() * scale); if (yMin > ((Double)samples.get(i)).doubleValue() * scale) yMin = ((Double)samples.get(i)).doubleValue() * scale; if (yMax < ((Double)samples.get(i)).doubleValue() * scale) yMax = ((Double)samples.get(i)).doubleValue() * scale; } // acertar double low, high; low = yScale.getLowerBound(); high = yScale.getUpperBound(); if (((int)(low / 0.5)) * 0.5 != low) low = (((int)(low / 0.5)) * 0.5) - 0.5; if (((int)(high / 0.5)) * 0.5 != high) high = (((int)(high / 0.5)) * 0.5) + 0.5; if (low != -0.5) yScale.setLowerBound(low); if (high != 0.5) yScale.setUpperBound(high); if (all) { yScale.setLowerVisibleBound(yScale.getLowerBound()); yScale.setUpperVisibleBound(yScale.getUpperBound()); } if (yScale.getLength() <= 20.0) yScale.calcStep((int)(yScale.getLength() / 0.5)); else yScale.calcStep(); } public void setWindows(Windows windows) { this.windows = windows; } public Windows getWindows() { return windows; } public int selectWindow(double where) { int i; i = 0; if (windows != null) { i = windows.find(where); } return i; } public int indexOf(double value, SpectrumView view) { int ret; ret = -1; /* for (int i = 0 ; i < samples.size() - 1 ; i++) { double x = ((Double) samples.get(i)).doubleValue (); if (((Double) samples.get(i)).doubleValue () <= value && ((Double) samples.get(i + 1)).doubleValue () > value) { ret = i; break; } }*/ ret = (int)(value / samplingRate); return ret; } public double normalizeCoord(double value, SpectrumView view) { int ndx; ndx = indexOf(value, view); if (ndx == -1) ndx = -100; if (value < ndx * samplingRate) ndx--; return ndx * samplingRate; } public void saveSamples(String fileName) { try { FileWriter fw = new FileWriter(fileName, true); for (int i = 0; i < samplesInteractive.size(); i++) { fw.write(getInteractiveSample(i)); fw.write("\n"); } fw.flush(); } catch (Exception e) { } } static public void setDecimation (int dec) { decimation = dec; } public boolean isVisible(){ return visible; } public void setVisible(boolean visible){ this.visible = visible; } public void setScale (double s , int p) {this.scale = s; this.scaleT = Integer.toString(p);} public double getScale () {return scale;}; public String getScaleT () {return this.scaleT;}; public double getYMin () {return yMin;} public double getYMax () {return yMax;} /** @associates Double */ protected Vector samples; protected Vector tickers; protected Vector samplesInteractive; protected double samplingRate; private String name; private String stationName; private String units; private int currentIndex; private SpectrumVisualProperties spectraVisualProperties; private Windows windows; private boolean visible; private double yMin; private double yMax; private double origOffset; private int ignoreTo; private double scale; private String scaleT; static int decimation = 10000; }