Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package spritetest;
- import java.util.ArrayList;
- import java.util.Random;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Rectangle2D;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Button;
- import javafx.scene.image.Image;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Stage;
- /**
- *
- * @author Compsci
- */
- public class SpriteTest extends Application {
- @Override
- public void start(Stage primaryStage) {
- Random random = new Random();
- Sprite briefcase = new Sprite();
- briefcase.setImage("file:/C:/Users/Compsci/Desktop/briefcase.png");
- Group root = new Group();
- Scene theScene = new Scene(root);
- primaryStage.setScene(theScene);
- primaryStage.setTitle("First Sprite");
- Canvas canvas = new Canvas(512,512);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- briefcase.drawSprite(gc);
- ArrayList<Sprite> imageList = new ArrayList<Sprite>();
- Sprite moneybag;
- for(int i = 0;i<10;i++){
- moneybag = new Sprite();
- moneybag.setImage("file:/C:/Users/Compsci/Desktop/money_bag.png");
- moneybag.setPosition(random.nextInt(300), random.nextInt(300));
- imageList.add(moneybag);
- moneybag.drawSprite(gc);
- }
- root.getChildren().add(canvas);
- primaryStage.show();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- class Sprite {
- private double velocityX;
- private double velocityY;
- private double positionX;
- private double positionY;
- private double width;
- private double height;
- private Image img;
- private void setImage(String path){
- img = new Image(path);
- }
- private void setPosition(double x, double y){
- positionX = x;
- positionY = y;
- }
- private void setVelocity(double x, double y){
- velocityX = x;
- velocityY = y;
- }
- private void addVelocity(double x, double y){
- velocityX += x;
- velocityY += y;
- }
- private void update(double time){
- positionX += velocityX * time;
- positionY += velocityY * time;
- }
- private Rectangle2D getBoundry(){
- return new Rectangle2D(positionX,positionY,width,height);
- }
- private boolean intersect(Sprite s){
- return s.getBoundry().intersects(this.getBoundry());
- }
- private void drawSprite(GraphicsContext gc){
- gc.drawImage(img,positionX,positionY);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement