Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. /**
  2.  * Class to make a bird
  3.  attack movie
  4.  * @author Mark Guzdial
  5.  * @author Barb Ericson
  6.  */
  7. public class HwAnimation {
  8.  
  9.   /** The root of the scene data structure */
  10.   private Branch sceneRoot;
  11.  
  12.   /** FrameSequencer where the animation is created */
  13.   private FrameSequencer frames;
  14.  
  15.   /** The nodes we need to track between methods */
  16.   private MoveBranch birdEntry, birdRetreat, hero;
  17.  
  18.   /**
  19.    * Constructor that takes no arguments and
  20.    * sets up the movie
  21.    */
  22.   public HwAnimation() {
  23.     setUp();
  24.   }
  25.  
  26.   /**
  27.    * Set up all the pieces of the tree.
  28.    */
  29.   private void setUp(){
  30.     Picture bird = new Picture(FileChooser.getMediaPath("egg2.jpg"));
  31.     Picture house = new Picture(FileChooser.getMediaPath("house-blue.jpg"));
  32.     Picture tree = new Picture(FileChooser.getMediaPath("tree-blue.jpg"));
  33.     Picture monster = new Picture(FileChooser.getMediaPath("mscary.jpg"));
  34.  
  35.     //Make the forest
  36.     MoveBranch forest = new MoveBranch(10,400); // at bottom
  37.     HBranch trees = new HBranch(50); // 50 pixels between
  38.     BlueScreenNode treenode;
  39.     for (int i=0; i < 8; i++) { // insert 8 trees
  40.       treenode = new BlueScreenNode(tree.scale(0.5));
  41.       trees.addChild(treenode);
  42.     }
  43.     forest.addChild(trees);
  44.    
  45.     // Make the cluster of attacking "birds"
  46.     birdEntry = new MoveBranch(10,50); // starting position
  47.     VBranch birds = new VBranch(20); // space out by 20 pixels
  48.     BlueScreenNode bird1 = new BlueScreenNode(bird.scale(0.5));
  49.    
  50.     birds.addChild(bird1);
  51.    
  52.     birdEntry.addChild(birds);
  53.    
  54.     // Make the cluster of retreating "birds"
  55.     birdRetreat = new MoveBranch(400,50); // starting position
  56.     birds = new VBranch(20); // space them out by 20 pixels
  57.     bird1 = new BlueScreenNode(bird.scale(0.5).flip());
  58.    
  59.     birds.addChild(bird1);
  60.  
  61.     birdRetreat.addChild(birds);
  62.    
  63.     // Make the village
  64.     MoveBranch village = new MoveBranch(300,450); // on bottom
  65.     HBranch hhouses = new HBranch(40); // 40 pixels apart
  66.     BlueScreenNode house1 = new BlueScreenNode(house.scale(0.25));
  67.     BlueScreenNode house2 = new BlueScreenNode(house.scale(0.25));
  68.     BlueScreenNode house3 = new BlueScreenNode(house.scale(0.25));
  69.     VBranch vhouses = new VBranch(-50); // move UP 50 pixels
  70.     BlueScreenNode house4 = new BlueScreenNode(house.scale(0.25));
  71.     BlueScreenNode house5 = new BlueScreenNode(house.scale(0.25));
  72.     BlueScreenNode house6 = new BlueScreenNode(house.scale(0.25));
  73.     vhouses.addChild(house4);
  74.     vhouses.addChild(house5);
  75.     vhouses.addChild(house6);
  76.     hhouses.addChild(house1);
  77.     hhouses.addChild(house2);
  78.     hhouses.addChild(house3);
  79.     hhouses.addChild(vhouses); // a VBranch can be a child
  80.     village.addChild(hhouses);
  81.    
  82.     // Make the monster
  83.     hero = new MoveBranch(400,300);
  84.     BlueScreenNode heronode =  new BlueScreenNode(monster.scale(0.75));
  85.     hero.addChild(heronode);
  86.    
  87.     //Assemble the base scene
  88.     sceneRoot = new Branch();
  89.     sceneRoot.addChild(forest);
  90.     sceneRoot.addChild(village);
  91.     sceneRoot.addChild(birdEntry);
  92.   }
  93.  
  94.   /**
  95.    * Method to get the scene root
  96.    * @return the sceneRoot
  97.    */
  98.   public Branch getSceneRoot() { return sceneRoot;}
  99.  
  100.   /**
  101.    * Render just the first scene
  102.    */
  103.   public void renderScene() {
  104.     Picture bg = new Picture(500,500);
  105.     sceneRoot.drawOn(bg);
  106.     bg.show();
  107.   }
  108.  
  109.   /**
  110.    * Render the whole animation
  111.    */
  112.   public void renderAnimation() {
  113.     frames = new FrameSequencer("C:/Temp/");
  114.     frames.show();
  115.     Picture bg;
  116.    
  117.     // First, the nasty wolvies come closer to the poor village
  118.     // Cue the scary music
  119.     for (int i=0; i<25; i++) {
  120.  
  121.       // Render the frame
  122.       bg = new Picture(500,500);
  123.       sceneRoot.drawOn(bg);
  124.       frames.addFrame(bg);
  125.      
  126.       // Tweak the data structure
  127.       birdEntry.moveTo(birdEntry.getXPos()+5,birdEntry.getYPos()+10);
  128.     }
  129.    
  130.     // Now, our hero arrives!
  131.     this.getSceneRoot().addChild(hero);
  132.    
  133.     // Render the frame
  134.     bg = new Picture(500,500);
  135.     sceneRoot.drawOn(bg);
  136.     frames.addFrame(bg);
  137.    
  138.     // Remove the birds entering, and insert the birds
  139.     // retreating
  140.     Branch root = this.getSceneRoot();
  141.     root.getFirstChild().remove(birdEntry);
  142.     root.addChild(birdRetreat);
  143.    
  144.     // Make sure that they retreat from the same place
  145.     birdRetreat.moveTo(birdEntry.getXPos(),birdEntry.getYPos());
  146.    
  147.     // Render the frame
  148.     bg = new Picture(500,500);
  149.     root.drawOn(bg);
  150.     frames.addFrame(bg);
  151.    
  152.     // Now, the cowardly birds hightail it out of there!
  153.     // Cue the triumphant music
  154.     for (int i=0; i<10; i++) {
  155.  
  156.       // Render the frame
  157.       bg = new Picture(500,500);
  158.       root.drawOn(bg);
  159.       frames.addFrame(bg);
  160.      
  161.       // Tweak the data structure
  162.       birdRetreat.moveTo(birdRetreat.getXPos()-10, birdRetreat.getYPos()-20);
  163.     }
  164.   }
  165.  
  166.   /**
  167.    * Replay the animation
  168.    */
  169.   public void replay() {
  170.     // Probably about 5 frames per second will work
  171.     frames.replay(200);
  172.   }
  173.  
  174.   /** Main method for testing */
  175.   public static void main (String[] args) {
  176.     WolfAttackMovie movie = new WolfAttackMovie();
  177.     movie.renderAnimation();
  178.   }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement