import java.awt.*; import java.awt.event.*; /* */ public class Lissajous extends java.applet.Applet { LissajousCanvas MyCanvas; ControlPanel MyPanel; public void init() { setLayout(new GridLayout(1,2,5,10)); MyCanvas = new LissajousCanvas(this); MyPanel = new ControlPanel(this); add(MyCanvas); add(MyPanel); } public void paint(Graphics g) { MyPanel.setBackground(Color.lightGray); MyCanvas.setBackground(Color.lightGray); MyPanel.paint(g); } public Insets getInsets() { return new Insets (5, 5, 5, 5); } public void update(Graphics g) { MyCanvas.paint(g); } } // **************************** MYCANVAS CLASS ******************** class LissajousCanvas extends Canvas { Lissajous outerparent; public int W, H; double fx=1.0, fy=1.0, phase=0.0; LissajousCanvas (Lissajous outerparent) { this.outerparent = outerparent; Rectangle r = outerparent.getBounds(); W = (r.width) / 2; H = r.height; } int gcd (int one, int two) { if (one < two) return gcd(two, one); else if (two == 0) return one; else return gcd (two, one % two); } public void paint (Graphics g) { int x, y, lastx=0, lasty=0, w = this.getSize().width, h = this.getSize().height; double t, max, ww=0.5*(double)w, hh=0.5*(double)h; // g.drawRect(0, 0, w-1, h-1); max = 2.0 * Math.PI * Math.max(fx, fy) / (double)gcd((int)fx,(int)fy); for (t = 0.0; t <= max ; t += 0.03125) { x = (int) Math.round(ww * Math.sin(t * (fy>fx ? fx/fy : 1.0))); y = (int) Math.round(hh * Math.sin(t * (fy>fx ? 1.0 : fy/fx) + phase)); if (t == 0.0) { lastx=x; lasty=y; } g.drawLine(lastx+w/2, lasty+h/2, x+w/2, y+h/2); lastx = x; lasty = y; } } public void setValue (int which, int what) { switch (which) { case 1: fx = (double) what; break; case 2: fy = (double) what; break; case 3: phase = (double) what; break; default: } repaint(); } } // ******************* MYPANEL CLASS ************************* class ControlPanel extends Panel { Scrollbar freq1, freq2, phase; Label lfreq1, lfreq2, lphase; Lissajous parent; ControlPanel(Lissajous parent) { this.parent = parent; freq1 = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,100); freq2 = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,100); phase = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,180); freq1.setBlockIncrement(5); freq2.setBlockIncrement(5); phase.setBlockIncrement(15); lfreq1 = new Label("1", Label.CENTER); lfreq2 = new Label("1", Label.CENTER); lphase = new Label("0", Label.CENTER); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints (); setLayout(gridbag); setFont(new Font("Helvetica", Font.BOLD, 14)); make_static_label("Lissajous Lab",gridbag, c); c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.gridwidth = 1; make_static_label(" ",gridbag, c); make_static_label("Frequency 1", gridbag, c); make_scroll(freq1, gridbag, c); make_dynamic_label(lfreq1, gridbag, c); lfreq1.setText(String.valueOf(1)); make_static_label("Frequency 2", gridbag, c); make_scroll(freq2, gridbag, c); make_dynamic_label(lfreq2, gridbag, c); lfreq2.setText(String.valueOf(1)); make_static_label("Phase",gridbag, c); make_scroll(phase, gridbag, c); make_dynamic_label(lphase, gridbag, c); lphase.setText(String.valueOf(0)); freq1.addAdjustmentListener(new ScrollbarAL(1, lfreq1)); freq2.addAdjustmentListener(new ScrollbarAL(2, lfreq2)); phase.addAdjustmentListener(new ScrollbarAL(3, lphase)); } void setValue (int which, int what) { parent.MyCanvas.setValue(which, what); } public void init() { freq1.setValue(1); freq2.setValue(1); phase.setValue(0); lfreq1.setText("1"); lfreq2.setText("1"); lphase.setText("0"); } public void paint(Graphics g) { // Dimension d = getSize(); // g.setColor(new Color(0, 0, 0)); // g.draw3DRect(0, 7, d.width-1, d.height-10, false); } protected void make_scroll(Scrollbar tempsc, GridBagLayout gridbag, GridBagConstraints c) { c.weightx = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(tempsc, c); add(tempsc); } protected void make_static_label(String name, GridBagLayout gridbag, GridBagConstraints c) { Label label = new Label(name, Label.CENTER); c.weightx = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(label, c); add(label); } protected void make_dynamic_label(Label temptex, GridBagLayout gridbag, GridBagConstraints c) { c.weightx = 1.0; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints(temptex, c); add(temptex); } class ScrollbarAL implements AdjustmentListener { int scrollbar; Label theLabel; ScrollbarAL (int _scrollbar, Label _theLabel) { scrollbar = _scrollbar; theLabel = _theLabel; } public void adjustmentValueChanged(AdjustmentEvent e) { int temp = e.getValue(); theLabel.setText(String.valueOf(temp)); setValue(scrollbar, temp); } } }