Advertisement
Guest User

Sprite

a guest
Oct 9th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package spritetest;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import javafx.application.Application;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.geometry.Rectangle2D;
  9. import javafx.scene.Group;
  10. import javafx.scene.Scene;
  11. import javafx.scene.canvas.Canvas;
  12. import javafx.scene.canvas.GraphicsContext;
  13. import javafx.scene.control.Button;
  14. import javafx.scene.image.Image;
  15. import javafx.scene.layout.StackPane;
  16. import javafx.stage.Stage;
  17.  
  18. /**
  19.  *
  20.  * @author Compsci
  21.  */
  22. public class SpriteTest extends Application {
  23.    
  24.     @Override
  25.     public void start(Stage primaryStage) {
  26.         Random random = new Random();
  27.         Sprite briefcase = new Sprite();
  28.         briefcase.setImage("file:/C:/Users/Compsci/Desktop/briefcase.png");
  29.         Group root = new Group();
  30.         Scene theScene = new Scene(root);
  31.         primaryStage.setScene(theScene);
  32.         primaryStage.setTitle("First Sprite");
  33.         Canvas canvas = new Canvas(512,512);
  34.         GraphicsContext gc = canvas.getGraphicsContext2D();
  35.         briefcase.drawSprite(gc);
  36.         ArrayList<Sprite> imageList = new ArrayList<Sprite>();
  37.         Sprite moneybag;
  38.         for(int i = 0;i<10;i++){
  39.             moneybag = new Sprite();
  40.             moneybag.setImage("file:/C:/Users/Compsci/Desktop/money_bag.png");
  41.             moneybag.setPosition(random.nextInt(300), random.nextInt(300));
  42.             imageList.add(moneybag);
  43.             moneybag.drawSprite(gc);
  44.         }
  45.         root.getChildren().add(canvas);
  46.         primaryStage.show();
  47.     }
  48.  
  49.     /**
  50.      * @param args the command line arguments
  51.      */
  52.     public static void main(String[] args) {
  53.         launch(args);
  54.     }
  55.    
  56.     class Sprite {
  57.         private double velocityX;
  58.         private double velocityY;
  59.         private double positionX;
  60.         private double positionY;
  61.         private double width;
  62.         private double height;
  63.         private Image img;
  64.  
  65.         private void setImage(String path){
  66.             img = new Image(path);
  67.         }
  68.         private void setPosition(double x, double y){
  69.             positionX = x;
  70.             positionY = y;
  71.         }
  72.         private void setVelocity(double x, double y){
  73.             velocityX = x;
  74.             velocityY = y;
  75.         }
  76.         private void addVelocity(double x, double y){
  77.             velocityX += x;
  78.             velocityY += y;
  79.         }
  80.         private void update(double time){
  81.             positionX += velocityX * time;
  82.             positionY += velocityY * time;
  83.         }
  84.         private Rectangle2D getBoundry(){
  85.             return new Rectangle2D(positionX,positionY,width,height);
  86.         }
  87.         private boolean intersect(Sprite s){
  88.             return s.getBoundry().intersects(this.getBoundry());
  89.         }
  90.         private void drawSprite(GraphicsContext gc){
  91.             gc.drawImage(img,positionX,positionY);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement