/** SpectrumCanvas.java Canvas that plots Vector of SpectrumDatum's * @author Phil Burk and Nick Didkovsky */ /* * (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved * JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom. */ package jmsltutorial; import java.awt.*; import java.util.Enumeration; import java.util.Vector; public class SpectrumCanvas extends Canvas { Vector spectrum; int width = 600; int height = 200; Image offscreenImage; Graphics bg; // background graphics public SpectrumCanvas() { setSize(width, height); } private void buildOffscreenImage() { offscreenImage = createImage(width, height); } public void setSpectrum(Vector v) { spectrum = v; } /* flicker killer */ public void update(Graphics g) { paint(g); } void plotLine(SpectrumDatum sd, Graphics g) { double f = sd.getFrequency(); double a = sd.getAmplitude(); int xpos = (int) (width * f / (20000.0 - 20.0)); int ypos = (int) (height / 2.0 - (height / 2.0 * a)); g.drawLine(xpos, height / 2, xpos, ypos); } void clear(Graphics g) { g.setColor(Color.lightGray); g.fillRect(0, 0, width, height); } public void paint(Graphics g) { if (offscreenImage == null) buildOffscreenImage(); if (offscreenImage != null) { bg = offscreenImage.getGraphics(); clear(bg); bg.setColor(Color.blue); if (spectrum == null) { bg.drawString("No spectrum to plot", 20, 20); } else { for (Enumeration e = spectrum.elements(); e.hasMoreElements();) { SpectrumDatum sd = (SpectrumDatum) e.nextElement(); plotLine(sd, bg); } } g.drawImage(offscreenImage, 0, 0, this); bg.dispose(); } } }