Advertisement
Nick-O-Rama

SpriteAnimation

Oct 20th, 2015
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package spriteanimation;
  7.  
  8. import javafx.animation.KeyFrame;
  9. import javafx.animation.Timeline;
  10. import javafx.application.Application;
  11. import javafx.scene.Group;
  12. import javafx.scene.Scene;
  13. import javafx.scene.canvas.Canvas;
  14. import javafx.scene.canvas.GraphicsContext;
  15. import javafx.scene.image.Image;
  16. import javafx.scene.paint.Color;
  17. import javafx.stage.Stage;
  18. import javafx.util.Duration;
  19.  
  20. /**
  21.  *
  22.  * @author Compsci
  23.  */
  24. public class SpriteAnimation extends Application {
  25.    
  26.     private String[] spriteList = {"file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/Right01.png",
  27.                                    "file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/Right02.png",
  28.                                    "file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/Right03.png",
  29.                                    "file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/Right04.png",
  30.                                    "file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/Right05.png",
  31.                                    "file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/Right06.png"};
  32.     private Image img = new Image("file:C:/Users/Compsci/Documents/NetBeansProjects/SpriteAnimation/spritewalk/walkSpriteSheet.png");
  33.     private int currentFrame = 0;
  34.     private int cols = 6;
  35.     private int frameHeight = 150;
  36.     private int frameWidth = 104;
  37.     @Override
  38.     public void start(Stage primaryStage) {
  39.        
  40.         Group root = new Group();
  41.         Canvas canvas = new Canvas(512, 512);
  42.         Scene scene = new Scene(root);
  43.         root.getChildren().add(canvas);
  44.         GraphicsContext gc = canvas.getGraphicsContext2D();
  45.         gc.setFill(Color.WHITE);
  46.         Timeline animation = new Timeline();
  47.         KeyFrame kf = new KeyFrame(Duration.millis(100), e->{
  48.             gc.clearRect(0, 0, 512, 512);
  49.             gc.fillRect(0, 0, 512, 512);
  50.             int fx = currentFrame % cols * frameWidth;
  51.             int fy = currentFrame / cols * frameHeight;
  52.             gc.drawImage(img, fx, fy, frameWidth, frameHeight, 200, 200, frameWidth, frameHeight);
  53.             //gc.drawImage(new Image(spriteList[currentFrame]), 100, 100);
  54.             currentFrame++;
  55.             if (currentFrame > 5)
  56.                 currentFrame = 0;
  57.            
  58.         });
  59.         animation.getKeyFrames().add(kf);
  60.         animation.setCycleCount(Timeline.INDEFINITE);
  61.         animation.play();
  62.         primaryStage.setTitle("Sprite Animation");
  63.         primaryStage.setScene(scene);
  64.         primaryStage.show();
  65.     }
  66.  
  67.     /**
  68.      * @param args the command line arguments
  69.      */
  70.     public static void main(String[] args) {
  71.         launch(args);
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement