Skip to content

Instantly share code, notes, and snippets.

@TheItachiUchiha
Created November 21, 2014 12:53
Show Gist options
  • Save TheItachiUchiha/e4739cd25ee837217227 to your computer and use it in GitHub Desktop.
Save TheItachiUchiha/e4739cd25ee837217227 to your computer and use it in GitHub Desktop.
A TimeLine showing ActionKame
package animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
class ImageViews extends ImageView {
private final Rectangle2D[][] cellClips;
private int numRows;
private int numColumns;
private final Duration FRAME_TIME = Duration.seconds(.2);
public ImageViews(Image explosionImage, int numColumns, int numRows) {
this.numColumns = numColumns;
this.numRows = numRows;
double cellWidth = explosionImage.getWidth() / numColumns;
double cellHeight = explosionImage.getHeight() / numRows;
cellClips = new Rectangle2D[numRows][numColumns];
for (int i = 0; i < numRows; i++) {
for(int j = 0; j< numColumns; j++) {
cellClips[i][j] = new Rectangle2D(
j * cellWidth, i * cellHeight,
cellWidth, cellHeight
);
}
}
setImage(explosionImage);
setViewport(cellClips[0][0]);
}
public void dropDown(EventHandler<ActionEvent> onFinished) {
final IntegerProperty frameCounter = new SimpleIntegerProperty(0);
final IntegerProperty y = new SimpleIntegerProperty(30);
Timeline kaboom = new Timeline(
new KeyFrame(FRAME_TIME, event -> {
setViewport(cellClips[0][frameCounter.get()]);
frameCounter.set((frameCounter.get() + 1) % numColumns);
setTranslateY(y.get());
y.set(y.get() + 50);
})
);
kaboom.setCycleCount(numColumns);
kaboom.setOnFinished(onFinished);
kaboom.play();
}
public void engage(EventHandler<ActionEvent> onFinished) {
final IntegerProperty frameCounter = new SimpleIntegerProperty(0);
final IntegerProperty rowCount = new SimpleIntegerProperty(1);
Timeline kaboom = new Timeline(
new KeyFrame(FRAME_TIME, event -> {
setViewport(cellClips[rowCount.get()][frameCounter.get()]);
frameCounter.set((frameCounter.get() + 1));
if(frameCounter.get() == numColumns && rowCount.get() <= numRows-1){
frameCounter.set(0);
rowCount.set(rowCount.get() + 1);
}
})
);
kaboom.setCycleCount(15);
kaboom.setOnFinished(onFinished);
kaboom.play();
}
public void repeat() {
final IntegerProperty frameCounter = new SimpleIntegerProperty(2);
Timeline kaboom = new Timeline(
new KeyFrame(FRAME_TIME, event -> {
setViewport(cellClips[numRows-1][frameCounter.get()]);
if(frameCounter.get() == numColumns-1) {
frameCounter.set(1);
}
frameCounter.set((frameCounter.get() + 1));
})
);
kaboom.setCycleCount(Timeline.INDEFINITE);
kaboom.play();
}
}
public class ActionKame extends Application {
public static void main(String[] args) {
launch(args);
}
private static final int NUM_CELLS_PER_COLUMN = 5;
private static final int NUM_CELLS_PER_ROW = 4;
@Override
public void start(Stage stage) {
Image objectImage = new Image("http://www.imagesup.net/dm-1014165743723.png");
BorderPane tiles = new BorderPane();
tiles.setCenter(new Items(objectImage, NUM_CELLS_PER_COLUMN, NUM_CELLS_PER_ROW));
stage.setTitle("Action Kame");
Scene scene = new Scene(tiles, 600, 600);
stage.setScene(scene);
stage.show();
}
}
class Items extends StackPane {
public Items(Image objectImage, int numColumns, int numRows) {
ImageViews explosionView = new ImageViews(
objectImage, numColumns, numRows
);
setMinSize(
Math.max(
objectImage.getWidth(),
explosionView.getViewport().getWidth()
),
Math.max(
objectImage.getHeight(),
explosionView.getViewport().getHeight()
)
);
getChildren().setAll(explosionView);
explosionView.dropDown(complete1 -> explosionView.engage(complete2 -> explosionView.repeat()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment