package org.openjfx.javafx_maven_plugin; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.util.Duration; import java.io.IOException; /** * JavaFX App */ public class App extends Application { private final static double height = 800; private final static double width = 1200; int ufoposx=0; int ufoposy=0; private static Scene scene; @Override public void start(Stage stage) throws IOException { Group g = new Group(); Canvas canvas = new Canvas(width, height); canvas.setFocusTraversable(true); g.getChildren().add(canvas); GraphicsContext gc = canvas.getGraphicsContext2D(); Timeline tl = new Timeline(new KeyFrame(Duration.millis(10), e -> run(gc))); tl.setCycleCount(Timeline.INDEFINITE); tl.play(); Scene scene = new Scene(g,width, height); stage.setTitle("MaleUFO"); stage.setScene(scene); stage.show(); canvas.addEventHandler(KeyEvent.KEY_PRESSED, event -> { switch (event.getCode()){ case D: ufoposx+=5; break; case A: ufoposx-=5; break; case S: ufoposy+=5; break; case W: ufoposy-=5; break; default: break; } } ); } private void run(GraphicsContext gc) { gc.clearRect(0, 0, width, height); gc.setStroke(Color.BLACK); maleufo (ufoposx, ufoposy, gc); } private void maleufo (double xUfo, double yUfo, GraphicsContext gc) { gc.setStroke(Color.BLACK); gc.setFill(Color.BLACK); gc.fillRect(xUfo,yUfo,30,15); gc.strokeLine(xUfo+15,yUfo-10,xUfo+15,yUfo); gc.strokeLine(xUfo+10,yUfo+15,xUfo+5,yUfo+25); gc.strokeLine(xUfo+20,yUfo+15,xUfo+25,yUfo+25); } public static void main(String[] args) { launch(); } }