Youss
Posts: 3
Nickname: mounir
Registered: Oct, 2002
stop an Animation
Posted: Oct 21, 2002 4:36 AM
Advertisement
How can i stop an Animation, when i start it. Can i directly stop it , or i must wait untill my LOOP in Programm stops just See this Example:(TRY IT) import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.applet.*; import java.util.Random; import java.util.Hashtable; public class FigureTe extends java.applet.Applet { public Scrollbar scrollbar; public Scrollbar scrollbar2; public Button button, button_reset; public Label label_T, label_Z, label1,label2,label3,label4,label5; int delay=10; int e=0; int x; public void init() { Panel pan = new Panel(); NullPanel ana = new NullPanel(); Steuerung is = new Steuerung(ana) ; ana.setBackground(Color.white); pan.setBackground(Color.white); label1= new Label("Schwellwert nach"); label2= new Label("AD Wandlung"); label3= new Label("additives Rauschen"); label4= new Label("Niedrig"); label5= new Label("Hoch"); //add(is); Panel Panelwael = new Panel(); /*setLayout(new BorderLayout()); Panelwael.setLayout(new GridLayout(1,2)); ana.setBackground(Color.white); is.setBackground(Color.white); */ setLayout(new BorderLayout()); Panelwael.setLayout(new BorderLayout()); Panelwael.add(ana); add(Panelwael) ; //pan.setLayout(new GridLayout(6,1,100,10)); pan.setLayout(new BulletinLayout()); //add (button1) //pan.add(label_T);; pan.add (scrollbar); pan.add (scrollbar2); pan.add (button); pan.add (button_reset); label_T.setForeground(Color.blue); pan.add(label_T); pan.add(label1); pan.add(label2); label3.setForeground(Color.red); pan.add(label3); label4.setForeground(Color.red); pan.add(label4); label5.setForeground(Color.red); pan.add(label5); //pan.add(label_Z); scrollbar.setLocation(0, 45); scrollbar.setSize(30,140); scrollbar2.setLocation(0, 200); scrollbar2.setSize(30,140); button.setLocation(0, 360); button.setSize(70,20); button_reset.setLocation(0, 390); button_reset.setSize(70,20); label_T.setLocation(35, 90); label_T.setSize(40,140); label1.setLocation(30, 40); label1.setSize(40,160); label2.setLocation(30, 60); label2.setSize(40,100); label3.setLocation(35,250); label3.setSize(120,70); label4.setLocation(35,180); label4.setSize(120,70); label5.setLocation(35,300); label5.setSize(120,70); add(Panelwael) ; add(pan,"East"); } class NullPanel extends Canvas { private boolean running=false,clear=true; //private int delay=4000; Thread animatorThread; int x1,y1,y2,x2; int i; int h; int x=0; //Listener List=new Listener(); Random random = new Random(); public int FeldSize = 60; //int t = ((int) (Math.random()*10)); public double[] t= new double[FeldSize]; public double[] f= new double[60]; int puls; double[] l= new double[8]; double[] yala= new double[60]; int s; int s2; boolean filly = false; int ma?stab = 20; // 1 == 20 Pixel; int y= -15*ma?stab; int c; private Color[] colors = { Color.black}; NullPanel sidi; Steuerung is = new Steuerung(sidi); public int setvar(int var) { s=var; return s; } public int setvarY(int vary) { s2=vary; return s2; } /* public boolean handleEvent( Event e ) { boolean result = super.handleEvent(e); if (e.target == is.scrollbar ){ //painto( getGraphics() ); repaint(); } return result; } */ public void paint(Graphics g) { // Dimension d = size(); Font font = g.getFont(); //int puls; // int FeldSize = 15; //int t = ((int) (Math.random()*10)); // int[] t= new int[FeldSize]; g.setColor(Color.black); // ma?stab px = 1 LE! // Zentrum setzen g.translate(1*ma?stab,20*ma?stab); // Koordinatensystem zeichnen g.drawLine(-20*ma?stab, 0, 30 * ma?stab, 0); // X-Achse g.drawLine(0, -20*ma?stab, 0, 20*ma?stab); // Y-Achse // Achsen beschriften g.drawString("x", -ma?stab,- ma?stab); g.drawString("y", -ma?stab, -19 * ma?stab); // und Einheiten zeigen for(int i = -20; i <= 30; i++) { g.drawLine(i * ma?stab, - 4, i * ma?stab, 4); g.drawLine(- 4, i * ma?stab, 4, i * ma?stab); } //painto(); g.setColor(Color.blue); g.drawLine(0*ma?stab, -15*ma?stab+3*setvar(s),820,-15*ma?stab+3*setvar(s)); g.setColor(Color.black); g.drawRect(0*ma?stab, -2*ma?stab,30*ma?stab+8,4*ma?stab); g.setColor(Color.black); g.setFont(font = new Font("Timesroman", Font.PLAIN, 14)); g.drawString ("zeitlicher Verlauf des Signals am Empf?nger", 1*ma?stab-1, -19*ma?stab-10); g.setFont(font = new Font("Timesroman", Font.PLAIN, 16)); g.drawString(" zuf?llige Pulsfolge am Eingang der ?bertragungsstrecke", 1*ma?stab -5, -3*ma?stab); g.drawString(" detektierte Pulsfolge am Empf?nger nach AD-Wandlung", 1*ma?stab -5, 4*ma?stab); g.setColor(Color.red); g.drawRect(0*ma?stab,5*ma?stab,30*ma?stab+8,4*ma?stab); g.setColor(Color.black); g.drawLine(0,7*ma?stab, 30*ma?stab, 7*ma?stab); /*g.setColor(Color.black); g.drawLine(0,70+(d.height/4),240,70+(d.height/4)); */ //g.drawLine(0,70,240,70); if (filly) { // hier wird die Start Button bet?tigt for (i=0; i<t.length; i++) { c=i; t =((random.nextGaussian())) ; //f[c] = t; // System.out.println(b); // int b = ((int) (Math.random()*15)) ;; //System.out.println(t); if (t<=0.0 ) { puls = -1; } else puls = 1; g.setColor(colors[(int)(Math.random()*1)]); //int t1=100; if (puls ==-1 ) { for (int e=0 ; e< 10000;e++){ g.fillRect(i*10,0*ma?stab,10,10); g.fillRect(c*10,7*ma?stab,10,10); // g.fillOval(i*5, -5*ma?stab+10, 2, 2); } for (int j=0; j<=19;j++) { if (setvar(s)> yala[j] & t[3*j]<0 & setvar(s)!=0 & setvar(s2)!=0 ) { t[3*j]=2.0321; t[3*j+2]=2; g.setColor(Color.gray) ; g.fillRect((i)*10,6*ma?stab+ma?stab/2,10,10); g.setColor(Color.white) ; g.fillRect((i)*10,7*ma?stab,10,10); } } } if (puls ==1 ) { for (e=0 ; e< 10000;++e){ g.fillRect(i*10,-ma?stab/2,10,10); g.setColor(Color.black) ; g.fillRect(c*10,6*ma?stab+ma?stab/2,10,10); //g.fillOval(i*5, -6*ma?stab, 2, 2); for (int j=0; j<=19;j++) { if (setvar(s)<= yala[3*j] & t[3*j]>0 & setvar(s)!=0 & setvar(s2)!=0) { t[3*j]=0;t[3*j+2]=0; g.setColor(Color.gray) ; g.fillRect(i*10,7*ma?stab,10,10); g.setColor(Color.white) ; g.fillRect(i*10,6*ma?stab+ma?stab/2,10,10); } if (setvar(s)<= yala[3*j+2] & t[3*j+2]>0 & setvar(s)!=0 & setvar(s2)!=0) { t[3*j+1]=0;t[3*j+2]=0; g.setColor(Color.gray) ; g.fillRect(i*10,7*ma?stab,10,10); g.setColor(Color.white) ; g.fillRect(i*10,6*ma?stab+ma?stab/2,10,10); } } } } //////////Erg?nzung///////////////// for (int j=0; j<=19;j++) { yala = t ; System.arraycopy(t, 0, yala, 0, t.length); //System.out.println(t[j]); l[j/3] =((random.nextGaussian())) ; /* if (setvar(s)<= yala[3*j] & t[3*j]>0 & setvar(s)!=0 & setvar(s2)!=0) { t[3*j]=0;t[3*j+2]=0; g.setColor(Color.gray) ; g.fillRect(i*10,7*ma?stab,10,10); g.setColor(Color.white) ; g.fillRect(i*10,6*ma?stab+ma?stab/2,10,10); } if (setvar(s)<= yala[3*j+2] & t[3*j+2]>0 & setvar(s)!=0 & setvar(s2)!=0) { t[3*j+1]=0;t[3*j+2]=0; g.setColor(Color.gray) ; g.fillRect(i*10,7*ma?stab,10,10); g.setColor(Color.white) ; g.fillRect(i*10,6*ma?stab+ma?stab/2,10,10); } */ // System.out.println(l) ; if (t[3*j]>0 && t[3*j+1]<= 0 && t[3*j+2] >0 ) { for (int k=10; k<=2*ma?stab ;k+=5){ g.fillOval((int)(4*ma?stab+k),(int)(-18*ma?stab+7*k/2), 2,2); g.fillOval((int)(10*ma?stab+k),(int)(-11*ma?stab-7*k/2), 2,2); if (setvarY(s2)>5){ g.fillOval((int)(4*ma?stab+0.5*k),(int)(-18*ma?stab+7*k/2), 2,2); g.fillOval((int)(11*ma?stab+0.5*k),(int)(-11*ma?stab-7*k/2), 2,2); } } for (int k=10; k<=4*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k),(int)(-18*ma?stab), 2,2); g.fillOval((int)(6*ma?stab+k),(int)(-11*ma?stab), 2,2); g.fillOval((int)(12*ma?stab+k),(int)(-18*ma?stab), 2,2); } } if (t[3*j]<=0 && t[3*j+1]> 0 && t[3*j+2] <=0 ) { for (int k=10; k<=2*ma?stab ;k+=5){ g.setColor(Color.black); g.fillOval((int)(4*ma?stab+k),(int)(-11*ma?stab-7*k/2), 2,2); g.fillOval((int)(10*ma?stab+k),(int)(-18*ma?stab+7*k/2), 2,2); if (setvarY(s2)>=5){ g.fillOval((int)(4*ma?stab+0.5*k),(int)(-11*ma?stab-7*k/2), 2,2); g.fillOval((int)(10*ma?stab+0.5*k),(int)(-18*ma?stab+7*k/2), 2,2); } } for (int k=10; k<=4*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k*l[j/8]),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(6*ma?stab+k*l[j/8]),(int)(-18*ma?stab+2*setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(12*ma?stab+k*l[j/8]),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2); } } // 0 1 1 // if (t[3*j]<=0 && t[3*j+1]> 0 && t[3*j+2] >0 ) { for (int k=10; k<=2*ma?stab ;k+=5){ g.fillOval((int)(4*ma?stab+k),(int)(-11*ma?stab-7*k/2), 2,2); if (setvarY(s2)>=7){ g.fillOval((int)(4*ma?stab+k*0.6),(int)(-11*ma?stab-7*k/2), 2,2); } } for (int k=10; k<=4*ma?stab ;k+=10){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(6*ma?stab+k),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(10*ma?stab+k),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(12*ma?stab+k),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); } } //////// // 1 0 0//// if (t[3*j]>0 && t[3*j+1]<= 0 && t[3*j+2] <=0 ) { for (int k=10; k<=2*ma?stab ;k+=5){ g.fillOval((int)(4*ma?stab+k),(int)(-18*ma?stab+7*k/2), 2,2); } for (int k=10; k<=4*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k*l[j/8]),(int)(-18*ma?stab+setvar(s2)*l[j/8]), 2,2); g.fillOval((int)(6*ma?stab-k*l[j/8]),(int)(-11*ma?stab+setvar(s2)*l[j/8]), 2,2); g.fillOval((int)(10*ma?stab+k*l[j/8]),(int)(-11*ma?stab+setvar(s2)*l[j/8]), 2,2); } } ////1 1 0/// if (t[3*j]>0 && t[3*j+1]> 0 && t[3*j+2] <=0 ) { for (int k=10; k<=2*ma?stab ;k+=5){ g.fillOval((int)(10*ma?stab+k),(int)(-18*ma?stab+7*k/2), 2,2); } for (int k=10; k<=4*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k*l[j/8]),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(6*ma?stab+k*l[j/8]),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(10*ma?stab+k*l[j/8]),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2); } } /// 0 0 1 //// if (t[3*j]<=0 && t[3*j+1]<= 0 && t[3*j+2] >0 ) { for (int k=10; k<=2*ma?stab ;k+=10){ g.fillOval((int)(10*ma?stab+k),(int)(-11*ma?stab-7*k/2), 2,2); } for (int k=10; k<=4*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k*l[j/8]),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(6*ma?stab-k*l[j/8]),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(10*ma?stab+k*l[j/8]),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); g.fillOval((int)(12*ma?stab+k*l[j/8]),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2); } } if (t[3*j]>0 && t[3*j+1]> 0 && t[3*j+2] >0 ) { for (int k=10; k<=14*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k*l[j/8]),(int)(-18*ma?stab+setvarY(s2)*l[j/8]), 2,2);} } if (t[3*j]<=0 && t[3*j+1]<= 0 && t[3*j+2] <=0 ) { for (int k=10; k<=14*ma?stab ;k+=30){ // erste Signal g.setColor(Color.black); g.fillOval((int)(0*ma?stab+k*l[j/8]),(int)(-11*ma?stab+setvarY(s2)*l[j/8]), 2,2);} } } // } } } } ////////////////////////////////////////// /* public void startAnimation(){ //Start animating! if (animatorThread == null) { animatorThread = new Thread(this); } animatorThread.start(); } public void stopAnimation(){ //Stop the animating thread. animatorThread = null; running=false; } public void clear(){ stopAnimation(); clear=true; repaint(); } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); long startTime = System.currentTimeMillis(); Thread currentThread = Thread.currentThread(); while (currentThread == animatorThread) { try { long numMillisecondsToSleep = 5000; // 5 seconds Thread.sleep(numMillisecondsToSleep); } catch (InterruptedException e) { } repaint(); try { Thread.sleep(Math.max(0, startTime-System.currentTimeMillis())); } catch (InterruptedException e) { break; } } } */ } class Steuerung extends Panel implements AdjustmentListener, ActionListener { NullPanel ana; Figure1 fig; int endstate; //Panel pan = new Panel(); public int varT=0,varB=0; // Label label_T, label_Z; // public Scrollbar scrollbar; //public Scrollbar scrollbar2; int scry; double scrolly; public int h=7; int varZ; // Ausgang freq=new Ausgang(); //public Button button; public Steuerung (NullPanel ana) { //setLayout(null); create(); GridBagLayout gridbag = new GridBagLayout(); setLayout(gridbag); GridBagConstraints c = new GridBagConstraints(); this.ana=ana; c.fill = GridBagConstraints.BOTH; c.weightx = 2; c.weighty = 2; c.gridwidth = 1; c.gridheight =1; gridbag.setConstraints(scrollbar,c); //44444444444444444444444444444444444444 44444444444444444444444444444444444444444444444444444444 label_T = new Label ("= " + ( (double) (scrollbar.getValue()))); gridbag.setConstraints(label_T, c); add(label_T); c.weightx = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; //end of row ////////////////////////// c.fill = GridBagConstraints.BOTH; c.weightx = 4; c.weighty = 4; c.gridwidth = 3; c.gridheight =3; gridbag.setConstraints(scrollbar2, c); //////////////////// c.weightx = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; //end of row label_Z = new Label (" "); gridbag.setConstraints(label_Z, c); add(label_Z); //////////////// this.ana = ana; //setLayout(null); //setLayout(new BorderLayout()); /* setLayout(new BulletinLayout()); //add (button1) add(label_T);; add (scrollbar); add (scrollbar2); add (button); //add(label_T); add(label_Z); scrollbar.addAdjustmentListener (this); scrollbar2.addAdjustmentListener (this); scrollbar.setLocation(30, 260); scrollbar.setSize(100,100); scrollbar2.setLocation(30, 360); scrollbar2.setSize(100,100); button.setLocation(0, 10); button.setSize(0,10); */ scrollbar.addAdjustmentListener (this); scrollbar2.addAdjustmentListener (this); button.addActionListener(this); button_reset.addActionListener(this); } //// /* public boolean handleEvent(Event e){ if (e.target instanceof Button ){ //System.out.println(varB ) ; if(e.target.equals(button)) { button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ana.filly= true ; ana.repaint(); } }); } if(e.target.equals(bu)) { button_reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ana.filly= false ; ana.repaint(); } });} //varZ= (scrollbar2.getValue()) ; //varY= (double) (0.1*varZ); //label_T.setText(" <--- Zeitraum t = "+((float) varT)/1000+" s"); //System.out.println(varB) ; return true; } return false; } */ public void create(){ ///16 mitte 33 100 scrollbar= new Scrollbar(Scrollbar.VERTICAL, 0,1,-14,24); scrollbar2= new Scrollbar(Scrollbar.VERTICAL, 10,1,0,10); //label_T = new Label ("Schwellwert f?r AD-Wandlung = " + 0.1* (double) scrollbar.getValue()); button = new Button ("start"); button_reset = new Button ("reset"); /*button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ana.filly=true; ana.startAnimation(); } }); button_reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ana.clear() ; } });*/ } public void actionPerformed(ActionEvent event) { //String arg = e.getActionCommand(); if(event.getSource() == button) { // button_reset.setEnabled (true); //button.setEnabled (false); ana.filly=true; ana.repaint(); } if(event.getSource() == button_reset ) { // button_reset.setEnabled (false); // button.setEnabled (true); ana.filly=false; ana.repaint(); } } /* public boolean mouseDrag(Event e,int xd,int yd){ int i; if(e.target == button_reset){ repaint(); } return true; } */ public void adjustmentValueChanged (AdjustmentEvent event) { int doit; scry= (scrollbar.getValue()); doit=(scrollbar2.getValue()); ; //label_T.setForeground(Color.blue); //label_T1.setText("Schwellwert f?r "); // label_T1.setText("Schwellwert f?r "); //label_T2.setText("AD-Wandlung "); label_T.setText("= " + (float) (0.1* scry)); //label_Z.setText("additives Rauschen "); //label_Z.setText("= "+ (scrollbar2.getValue())); ana.setvar(scry); ana.repaint(); ana.setvarY(doit); } //////////////////////////////// /* public void paint(Graphics g) { // Dimension d = size(); Font font = g.getFont(); g.drawString("Schwellwert ", 160, 70) ; g.drawString("f?r AD-Wandlung ",160, 90) ; g.setColor(Color.red); g.drawString("Additives ", 160, 240) ; g.drawString("Rauschen ", 160, 260) ; } */ } } class BulletinLayout implements LayoutManager { private Hashtable hash = new Hashtable(); public void addLayoutComponent(String s, Component comp) { } public void removeLayoutComponent(Component comp) { } public Dimension preferredLayoutSize(Container target) { Insets insets = target.getInsets(); Dimension dim = new Dimension(0,0); int ncomponents = target.getComponentCount(); Component comp; Dimension d; Rectangle size = new Rectangle(0,0); Rectangle compSize; for (int i = 0 ; i < ncomponents ; i++) { comp = target.getComponent(i); if(comp.isVisible()) { d = comp.getSize(); compSize = new Rectangle(comp.getLocation()); compSize.width = d.width; compSize.height = d.height; size = size.union(compSize); } } dim.width += size.width + insets.right; dim.height += size.height + insets.bottom; return dim; } public Dimension minimumLayoutSize(Container target) { Insets insets = target.getInsets(); Dimension dim = new Dimension(0,0); int ncomponents = target.getComponentCount(); Component comp; Dimension d; Rectangle minBounds = new Rectangle(0,0); Rectangle compMinBounds; for (int i = 0 ; i < ncomponents ; i++) { comp = target.getComponent(i); if(comp.isVisible()) { d = comp.getMinimumSize(); compMinBounds = new Rectangle(comp.getLocation()); compMinBounds.setSize(d.width, d.height); minBounds = minBounds.union(compMinBounds); } } dim.width += minBounds.width + insets.right; dim.height += minBounds.height + insets.bottom; return dim; } public void layoutContainer(Container target) { Insets insets = target.getInsets(); int ncomponents = target.getComponentCount(); Component comp; Dimension sz, ps; Point loc; for (int i = 0 ; i < ncomponents ; i++) { comp = target.getComponent(i); if(comp.isVisible()) { sz = comp.getSize(); ps = comp.getPreferredSize(); loc = getComponentLocation(comp); if(sz.width < ps.width || sz.height < ps.height) sz = ps; comp.setBounds(loc.x, loc.y, sz.width, sz.height); } } } private Point getComponentLocation(Component comp) { Insets insets = comp.getParent().getInsets(); Point loc = comp.getLocation(); if( ! hash.containsKey(comp)) { addComponentToHashtable(comp); } else { Point oldLocation = (Point)hash.get(comp); if(oldLocation.x != loc.x || oldLocation.y != loc.y) { addComponentToHashtable(comp); } } return comp.getLocation(); } private void addComponentToHashtable(Component comp) { Insets insets = comp.getParent().getInsets(); Point loc = comp.getLocation(); comp.setLocation(loc.x + insets.left, loc.y + insets.top); hash.put(comp, comp.getLocation()); } }