Advertisement
Guest User

Untitled

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