// FFTGraph.java import java.awt.*; public class FFTGraph extends Canvas { private int[] xi, yi, yffti; private double[] x, y, yfft; private double xMin, xMax, yMin, yMax; private int width, height; private boolean overlay, showFFT, showNormal; public FFTGraph() { setBackground(Color.black); } public Dimension getPreferredSize() { return new Dimension(350, 250); } private void findMinAndMax() { yMin = yMax = y[0]; xMin = x[0]; xMax = x[x.length - 1]; for(int i = 0; i < y.length; i++) { if (y[i] < yMin) yMin = y[i]; if (yfft[i] < yMin) yMin = yfft[i]; if (y[i] > yMax) yMax = y[i]; if (yfft[i] > yMax) yMax = yfft[i]; } yMin = yMin - 0.1 * Math.abs(yMin); yMax = yMax + 0.1 * Math.abs(yMax); } public void plot(double _x[], double _y[], double _yfft[], boolean _overlay, boolean _showFFT, boolean _showNormal) { x = _x; y = _y; yfft = _yfft; overlay = _overlay; showFFT = _showFFT; showNormal = _showNormal; xi = new int[x.length]; yi = new int[y.length]; yffti = new int[yfft.length]; findMinAndMax(); fitToScreen(); } public void fitToScreen() { if (x != null) { width = this.getSize().width; height = this.getSize().height; for(int i = 0; i < y.length; i++) { xi[i] = changeXCoords(x[i]); if (!overlay) { yi[i] = (changeYCoords(y[i]) + height) / 2; yffti[i] = changeYCoords(yfft[i]) / 2; } else { yi[i] = changeYCoords(y[i]); yffti[i] = changeYCoords(yfft[i]); } } repaint(); } } private int changeXCoords(double x) { return (int)Math.round(((double)width / (xMax - xMin)) * (x - xMax) + (double)width - 1); } private int changeYCoords(double y) { return (int)Math.round(((double)height / (yMin - yMax)) * (y - yMin) + (double)height - 1); } public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, this.getSize().width, this.getSize().height); g.setColor(Color.darkGray); for(int i = 0; i < 8; i++) g.drawLine((i * width) / 8, 0, (i * width) / 8, height); if (xi != null) { if ((width != this.getSize().width) || (height != this.getSize().height)) fitToScreen(); if (!overlay) { g.setColor(Color.darkGray); g.drawLine(0, height / 2, width, height / 2); } if (showNormal) { g.setColor(Color.white); g.drawPolyline(xi, yi, xi.length); } if (showFFT) { g.setColor(Color.green); g.drawPolyline(xi, yffti, xi.length); } } } }