import java.applet.*; import java.awt.*; import java.awt.event.*; public class welle1 extends Applet { int Start=1; Button f1 = new Button("Stop"); Scrollbar Frequenz = new Scrollbar(Scrollbar.HORIZONTAL,6,1,1,31); Scrollbar Länge = new Scrollbar(Scrollbar.HORIZONTAL,13,1,4,31); double omega=0.6; //Gibt Frequenz an double laenge=13; //Gibt Wellenlaenge an boolean anhalten; painter canvas; //initialisiert Zeichenflaeche wellen wellen1; wellen wellen2; wellen wellen3; wellen wellen4; wellen wellen5; wellen wellen6; Zeiger zeiger1; Panel raster = new Panel(); Label s1 = new Label("Frequenz",Label.RIGHT); Label s2 = new Label("0.6"); Label s3 = new Label("Wellenlänge",Label.RIGHT); Label s4 = new Label("13"); double sb1value=0; //Scrollbarvariable double sb2value=0; //Scrollbarvariable int s4value=13; //Anzeigevariable fuer Scrollbar2 double t=0.0; double t_neu=0.0; int[] x = new int[31]; int[] y = new int[31]; final double pi=3.14; public welle1() { canvas = new painter(); canvas.setForeground(Color.black); canvas.setBackground(Color.white); canvas.setSize(630,300); setLayout(new FlowLayout()); add(canvas); Label info1 = new Label(" ",Label.LEFT); Label info2 = new Label(" ",Label.RIGHT); info1.setForeground(Color.blue); info2.setForeground(Color.blue); raster.setLayout(new GridLayout(4,3,10,5)); raster.setForeground(Color.black); raster.setBackground(Color.white); raster.add(s1); raster.add(Frequenz); raster.add(s2); raster.add(s3); raster.add(Länge); raster.add(s4); raster.add(info1); raster.add(f1); raster.add(info2); add(raster); Frequenz.addAdjustmentListener(new Scrolling()); Länge.addAdjustmentListener(new Scrolling()); f1.addActionListener(new Akt()); } public static void main(String[] args) { Frame fenster = new Frame("Zur Wellenfunktion");; class adapt extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } fenster.addWindowListener(new adapt()); welle1 test = new welle1(); test.init(); test.start(); fenster.add(test); fenster.setBackground(Color.white); fenster.setForeground(Color.black); fenster.setBounds(100,80,670,450); fenster.setResizable(false); fenster.show(); } //Ende von main public void init() { wellen1 = new wellen(canvas,0); wellen2 = new wellen(canvas,5); wellen3 = new wellen(canvas,10); wellen4 = new wellen(canvas,15); wellen5 = new wellen(canvas,20); wellen6 = new wellen(canvas,25); zeiger1 = new Zeiger(canvas,0); wellen1.beenden = false; wellen2.beenden = false; wellen3.beenden = false; wellen4.beenden = false; wellen5.beenden = false; wellen6.beenden = false; zeiger1.beenden = false; wellen1.anhalten = true; wellen2.anhalten = true; wellen3.anhalten = true; wellen4.anhalten = true; wellen5.anhalten = true; wellen6.anhalten = true; zeiger1.anhalten = true; wellen1.start(); wellen2.start(); wellen3.start(); wellen4.start(); wellen5.start(); wellen6.start(); zeiger1.start(); } // Ende von init public void start() { synchronized(wellen1) { wellen1.anhalten = false; try { wellen1.notify(); } catch(Exception e){} } synchronized(wellen2) { wellen2.anhalten = false; try { wellen2.notify(); } catch(Exception e){} } synchronized(wellen3) { wellen3.anhalten = false; try { wellen3.notify(); } catch(Exception e){} } synchronized(wellen4) { wellen4.anhalten = false; try { wellen4.notify(); } catch(Exception e){} } synchronized(wellen5) { wellen5.anhalten = false; try { wellen5.notify(); } catch(Exception e){} } synchronized(wellen6) { wellen6.anhalten = false; try { wellen6.notify(); } catch(Exception e){} } synchronized(zeiger1) { zeiger1.anhalten = false; try { zeiger1.notify(); } catch(Exception e){} } } // Ende von Start public void stop() { if(wellen1 != null && wellen2 != null && wellen3 != null && wellen4 != null && wellen5 != null && wellen6 != null && zeiger1 != null) { wellen1.anhalten = true; wellen2.anhalten = true; wellen3.anhalten = true; wellen4.anhalten = true; wellen5.anhalten = true; wellen6.anhalten = true; zeiger1.anhalten = true; } } public void destroy() { if(wellen1 != null && wellen2 != null && wellen3 != null && wellen4 != null && wellen5 != null && wellen6 != null && zeiger1 != null) { wellen1.anhalten = true; wellen2.anhalten = true; wellen3.anhalten = true; wellen4.anhalten = true; wellen5.anhalten = true; wellen6.anhalten = true; zeiger1.anhalten = true; } } class Akt implements ActionListener { public void actionPerformed(ActionEvent e) { String Label; Label = e.getActionCommand(); if(Label.equals("Start")) { Start=1; f1.setLabel("Stop"); omega=Frequenz.getValue()/10.0; wellen1.resume(); wellen2.resume(); wellen3.resume(); wellen4.resume(); wellen5.resume(); wellen6.resume(); zeiger1.resume(); } if(Label.equals("Stop")) { Start=0; f1.setLabel("Start"); wellen1.suspend(); wellen2.suspend(); wellen3.suspend(); wellen4.suspend(); wellen5.suspend(); wellen6.suspend(); zeiger1.suspend(); } } } //Ende von Actionlistener class Scrolling implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e) { sb1value=Frequenz.getValue(); sb2value=Länge.getValue(); if(Start==1) { omega=sb1value/10.0; } laenge=sb2value; double s2value = omega; s4value = (int) laenge; s2.setText("" + sb1value/10.0); s4.setText("" + s4value); zeiger1.resume(); wellen1.resume(); wellen2.resume(); wellen3.resume(); wellen4.resume(); wellen5.resume(); wellen6.resume(); Start=1; f1.setLabel("Stop"); canvas.repaint(); } } class wellen extends Thread { boolean anhalten,beenden; //Statusvariablen painter bild; double dt = 0.002; int i=0; wellen(painter c, int k) //Konstruktor anlegen { bild=c; i=k; } public void run() //run Methode fuer die Threads { while(beenden==false) { Bewegen(); //Ein Stueckchen drehen synchronized(this) { try { sleep(20); if(anhalten==true) { wait(); } } catch(InterruptedException e) {return;} } } } void Bewegen() { //double t_neu; Graphics g = bild.getGraphics(); g.setColor(Color.white); g.drawLine(22+i*20,202,22+i*20+x[i],202+y[i]); g.drawLine(22+(i*20),152,22+(i*20),152+y[i]); g.drawLine(22+(i+1)*20,202,22+(i+1)*20+x[i+1],202+y[i+1]); g.drawLine(22+(i+1)*20,152,22+(i+1)*20,152+y[i+1]); g.drawLine(22+(i+2)*20,202,22+(i+2)*20+x[i+2],202+y[i+2]); g.drawLine(22+(i+2)*20,152,22+(i+2)*20,152+y[i+2]); g.drawLine(22+(i+3)*20,202,22+(i+3)*20+x[i+3],202+y[i+3]); g.drawLine(22+(i+3)*20,152,22+(i+3)*20,152+y[i+3]); g.drawLine(22+(i+4)*20,202,22+(i+4)*20+x[i+4],202+y[i+4]); g.drawLine(22+(i+4)*20,152,22+(i+4)*20,152+y[i+4]); g.setColor(Color.black); g.fillOval((i+1)*20,200,5,5); g.fillOval((i+1)*20,150,5,5); g.fillOval((i+2)*20,200,5,5); g.fillOval((i+2)*20,150,5,5); g.fillOval((i+3)*20,200,5,5); g.fillOval((i+3)*20,150,5,5); g.fillOval((i+4)*20,200,5,5); g.fillOval((i+4)*20,150,5,5); g.fillOval((i+5)*20,200,5,5); g.fillOval((i+5)*20,150,5,5); t_neu=t+dt; x[i]=(int) (15*(Math.cos((t_neu*2*pi*omega)+(30-i)/laenge*2*pi))); y[i]=(int) (-15*(Math.sin((t_neu*2*pi*omega)+(30-i)/laenge*2*pi))); x[i+1]=(int) (15*(Math.cos((t_neu*2*pi*omega)+(30-(i+1))/laenge*2*pi))); y[i+1]=(int) (-15*(Math.sin((t_neu*2*pi*omega)+(30-(i+1))/laenge*2*pi))); x[i+2]=(int) (15*(Math.cos((t_neu*2*pi*omega)+(30-(i+2))/laenge*2*pi))); y[i+2]=(int) (-15*(Math.sin((t_neu*2*pi*omega)+(30-(i+2))/laenge*2*pi))); x[i+3]=(int) (15*(Math.cos((t_neu*2*pi*omega)+(30-(i+3))/laenge*2*pi))); y[i+3]=(int) (-15*(Math.sin((t_neu*2*pi*omega)+(30-(i+3))/laenge*2*pi))); x[i+4]=(int) (15*(Math.cos((t_neu*2*pi*omega)+(30-(i+4))/laenge*2*pi))); y[i+4]=(int) (-15*(Math.sin((t_neu*2*pi*omega)+(30-(i+4))/laenge*2*pi))); g.fillOval((i+1)*20,200,5,5); g.fillOval((i+2)*20,200,5,5); g.fillOval((i+3)*20,200,5,5); g.fillOval((i+4)*20,200,5,5); g.fillOval((i+5)*20,200,5,5); g.drawLine(22+i*20,202,22+i*20+x[i],202+y[i]); g.drawLine(22+(i*20),152,22+(i*20),152+y[i]); g.drawLine(22+(i+1)*20,202,22+(i+1)*20+x[i+1],202+y[i+1]); g.drawLine(22+(i+1)*20,152,22+(i+1)*20,152+y[i+1]); g.drawLine(22+(i+2)*20,202,22+(i+2)*20+x[i+2],202+y[i+2]); g.drawLine(22+(i+2)*20,152,22+(i+2)*20,152+y[i+2]); g.drawLine(22+(i+3)*20,202,22+(i+3)*20+x[i+3],202+y[i+3]); g.drawLine(22+(i+3)*20,152,22+(i+3)*20,152+y[i+3]); g.drawLine(22+(i+4)*20,202,22+(i+4)*20+x[i+4],202+y[i+4]); g.drawLine(22+(i+4)*20,152,22+(i+4)*20,152+y[i+4]); t=t_neu; g.dispose(); } //Ende von draw } //Ende der Klasse wellen class painter extends Canvas { public void paint(Graphics g) { g.setColor(Color.black); g.drawLine(1,1,629,1); g.drawLine(629,1,629,299); g.drawLine(629,299,1,299); g.drawLine(1,299,1,1); g.drawRect(310,20,70,70); g.drawString(" Vergleich zwischen Elongation und Phasenzeiger",20,30); g.drawString("des ersten Wellenpunktes (vergrößert):",20,45); g.setColor(Color.red); g.drawString("rot:",20,65); g.setColor(Color.blue); g.drawString("blau:",20,80); g.setColor(Color.black); g.drawString("Elongation",50,65); g.drawString("Phasenzeiger",50,80); //g.drawString("Maßstab 2:1",290,30); g.setColor(Color.red); g.drawString("Wellenlänge",25,280); g.drawLine(22,240,22,260); g.drawLine(22,250,22+((s4value)*20),250); g.drawLine(22+((s4value)*20),240,22+((s4value)*20),260); g.setColor(Color.blue); Font eins = new Font("Times",0,9); g.setFont(eins); g.drawString("\u00a9 1999 by Jörg Bogendörfer; Didaktik der Physik, Uni Erlangen",350,297); } //Ende von Paint } // Ende von painter class Zeiger extends Thread { boolean anhalten,beenden; //Statusvariablen painter bild; double dt = 0.002; int i=0; Zeiger(painter c, int k) //Konstruktor anlegen { bild=c; i=k; } public void run() //run Methode fuer die Threads { while(beenden==false) { Bewegen(); //Ein Stueckchen drehen synchronized(this) { try { sleep(20); if(anhalten==true) { wait(); } } catch(InterruptedException e) {return;} } } } void Bewegen() { int x,y; Graphics g = bild.getGraphics(); g.setColor(Color.white); g.fillRect(315,25,65,65); x=(int) (30*(Math.cos((t_neu*2*pi*omega)+(30)/laenge*2*pi))); y=(int) (-30*(Math.sin((t_neu*2*pi*omega)+(30)/laenge*2*pi))); g.setColor(Color.black); g.fillOval(343,53,5,5); g.setColor(Color.blue); g.drawLine(345,55,345+x,55+y); g.setColor(Color.red); g.drawLine(345,55,345,55+y); g.setColor(Color.darkGray); g.drawLine(345,55+y,345+x,55+y); g.dispose(); } //Ende von draw } //Ende der Klasse Zeiger } //Ende von welle1