00001 package edu.tum.cs.vis; 00002 00003 import java.util.Vector; 00004 00005 public class AnimatedCanvas extends Canvas { 00006 00007 private static final long serialVersionUID = 1L; 00008 00009 protected int animationStep = 0; 00010 //maxAnimationStep = 0; 00011 protected Vector<DrawableAnimated> animatedItems = new Vector<DrawableAnimated>(); 00012 00013 @Override 00014 public void keyPressed() { 00015 super.keyPressed(); 00016 switch(keyCode) { 00017 case RIGHT: 00018 animationStepForward(); 00019 break; 00020 case LEFT: 00021 animationStepBackward(); 00022 break; 00023 case java.awt.event.KeyEvent.VK_HOME: 00024 animationReset(); 00025 break; 00026 case java.awt.event.KeyEvent.VK_END: 00027 animationEnd(); 00028 break; 00029 case java.awt.event.KeyEvent.VK_PAGE_UP: 00030 animationSkip(10); 00031 break; 00032 case java.awt.event.KeyEvent.VK_PAGE_DOWN: 00033 animationSkip(-10); 00034 break; 00035 } 00036 } 00037 00038 public synchronized int getMaxAnimationStep() { 00039 int max = 0; 00040 for(DrawableAnimated item : animatedItems) { 00041 max = Math.max(max, item.getMaxStep()); 00042 } 00043 return max; 00044 } 00045 00046 public void animationStepForward() { 00047 if(animationStep < getMaxAnimationStep()) 00048 animationStep++; 00049 redraw(); 00050 } 00051 00052 public void animationStepBackward() { 00053 if(animationStep > 0) 00054 animationStep--; 00055 redraw(); 00056 } 00057 00058 public void animationReset() { 00059 animationStep = 0; 00060 redraw(); 00061 } 00062 00063 public void animationEnd() { 00064 animationStep = getMaxAnimationStep(); 00065 redraw(); 00066 } 00067 00072 public void animationSkip(int steps) { 00073 animationStep += steps; 00074 int maxStep = getMaxAnimationStep(); 00075 if(animationStep > maxStep) 00076 animationStep = maxStep; 00077 if(animationStep < 0) 00078 animationStep = 0; 00079 redraw(); 00080 } 00081 00082 @Override 00083 public synchronized void drawItems() { 00084 super.drawItems(); 00085 00086 for(DrawableAnimated d : animatedItems) { 00087 d.draw(this, animationStep); 00088 } 00089 } 00090 00091 public synchronized void addAnimated(DrawableAnimated item) { 00092 //maxAnimationStep = Math.max(maxAnimationStep, item.getMaxStep()); 00093 animatedItems.add(item); 00094 } 00095 00096 public int getAnimationStep() { 00097 return this.animationStep; 00098 } 00099 00100 public Vector<DrawableAnimated> getAnimatedItems() { 00101 return animatedItems; 00102 } 00103 }