package rice2604hw; /* ==================================================== 2604 MandelApplet J.Rice ---------------------------------------------------- Allows testing of JWRMandelInst and the FilteredFM circuit, intended for use in Didkovsky's MandelMusic applet. ---------------------------------------------------- Due: 11/15/99 HW 6 ==================================================== */ import com.softsynth.jsyn.*; import com.softsynth.jmsl.*; import java.awt.*; import java.awt.event.*; import java.lang.*; import java.applet.Applet; public class MandelApplet extends Applet implements XYListener, ActionListener { MouseMotionPanel mmp; JWRMandelInst inst; Button noteOnButton, noteOffButton; TextField azField, bzField, magField, distField, kField; int lastX, lastY, panelSize = 100; double aZ, bZ, mag, dist, kount; double[] dar; Panel p0, p, p2; //-------------------------------------------------------------- public void init() { //ALL GUI dar = new double[5]; p0 = new Panel(); p0.setLayout(new GridLayout(2, 1)); p = new Panel(); p.setLayout(new GridLayout(1, 2)); p.setBackground(Color.green); p.add(mmp = new MouseMotionPanel(panelSize)); p2 = new Panel(); p2.setLayout(new GridLayout(2, 1)); p2.add(noteOnButton = new Button("NoteOn()")); p2.add(noteOffButton = new Button("NoteOff()")); p.add(p2); p0.add(p); noteOnButton.addActionListener(this); noteOffButton.addActionListener(this); mmp.addXYListener(this); p = new Panel(); p.setLayout(new GridLayout(3, 1)); p.setBackground(Color.orange); p.add(new Label("Please hit NoteOn() and drag in the canvas", Label.CENTER)); p2 = new Panel(); p2.setLayout(new GridLayout(1, 5)); p2.add(new Label("AZ", Label.CENTER)); p2.add(new Label("BZ", Label.CENTER)); p2.add(new Label("Mag", Label.CENTER)); p2.add(new Label("Dist", Label.CENTER)); p2.add(new Label("Kount", Label.CENTER)); p.add(p2); p2 = new Panel(); p2.setLayout(new GridLayout(1, 5)); p2.add(azField = new TextField("0.0")); p2.add(bzField = new TextField("0.0")); p2.add(magField = new TextField("0.0")); p2.add(distField = new TextField("0.0")); p2.add(kField = new TextField("0")); p.add(p2); azField.addActionListener(this); bzField.addActionListener(this); magField.addActionListener(this); distField.addActionListener(this); kField.addActionListener(this); p0.add(p); add(p0); } //-------------------------------------------------------------- public void locationChanged(int x, int y) { aZ = x / 100.0; azField.setText(aZ + ""); bZ = y / 100.0; bzField.setText(bZ + ""); mag = (int)((Math.sqrt((double)((x * x) + (y * y))))) / 141.0; magField.setText(mag + ""); dist = (Math.sqrt(Math.pow((double)(x - lastX), 2.0) + Math.pow((double)(y - lastY), 2.0)))/141.0; distField.setText(dist + ""); kount++; kField.setText(kount + ""); dar[0] = aZ; dar[1] = bZ; dar[2] = mag; dar[3] = dist; dar[4] = convertKount(kount); // inst.play(JMSL.now(), 1.0, dar); //inst.setAll(dar); lastX = x; lastY = y; } //-------------------------------------------------------------- public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == noteOnButton) handleNoteOn(); else if (source == noteOffButton) handleNoteOff(); else if (source == azField) handleAZField(); else if (source == bzField) handleBZField(); else if (source == magField) handleMagField(); else if (source == distField) handleDistField(); else if (source == kField) handleKField(); } //-------------------------------------------------------------- void handleNoteOn() { dar[0] = aZ; dar[1] = bZ; dar[2] = mag; dar[3] = dist; dar[4] = convertKount(kount); inst.play(JMSL.now(), 1.0, dar); } //-------------------------------------------------------------- double convertKount(double cK) { double sK; sK = cK * .03; //the constant here determines PERIOD of filter openg/closg sK = Math.sin(sK); sK = sK * sK; //JMSL.out.println(getName() + " Kount sin/sqr = " + sK); return sK; //((kount % 50.0) / 50.0); } //-------------------------------------------------------------- void handleNoteOff() { inst.pleaseStopNow(); //inst.modamplitude.set(0.0); //inst.caramplitude.set(0.0); } //-------------------------------------------------------------- void handleAZField() { try { aZ = clip((new Double(azField.getText())).doubleValue()); } catch (NumberFormatException e) { azField.setText("NFE..."); } } //-------------------------------------------------------------- void handleBZField() { try { bZ = clip((new Double(bzField.getText())).doubleValue()); } catch (NumberFormatException e) { bzField.setText("NFE..."); } } //-------------------------------------------------------------- void handleMagField() { try { mag = clip((new Double(magField.getText())).doubleValue()); } catch (NumberFormatException e) { magField.setText("NFE..."); } } //-------------------------------------------------------------- void handleDistField() { try { dist = clip((new Double(distField.getText())).doubleValue()); } catch (NumberFormatException e) { distField.setText("NFE..."); } } //-------------------------------------------------------------- void handleKField() { try { kount = ((new Double(kField.getText())).doubleValue()); } catch (NumberFormatException e) { kField.setText("NFE..."); } } //-------------------------------------------------------------- double clip(double x) { if (x > 1.0) return 1.0; else if (x < 0.0) return 0.0; else return x; } //-------------------------------------------------------------- public void start() { try { Synth.startEngine(0); //SynthObject.enableTracking(true); JMSL.clock = new com.softsynth.jmsl.jsyn.SynthClock(); inst = new JWRMandelInst(); inst.open(JMSL.now()); } catch (SynthException e) { SynthAlert.showError(this, e); } } //-------------------------------------------------------------- public void stop() { try { inst.close(JMSL.now()); Synth.stopEngine(); } catch (SynthException e) { JMSL.out.println("Error " + e); } } }//==========================END MANDELAPPLET===================================