JavaFX 使 imageView 朝着它所面对的方向前进
JavaFX make imageView go in direction it is facing
我想让一个 imageView 对象朝它所面对的方向移动。
我让它在 X/Y 轴上移动,也让它旋转,但我真的不知道如何解决这个向它所面对的方向移动的问题(例如,在我的代码中我使用汽车,我只想让它朝它面对的方向移动)
我感谢任何可以让我学习的想法或教程。
public class Main extends Application {
private static final double W = 800;
private static final double H = 600;
private boolean goNorth, goSouth, goWest, goEast;
private ImageView hero;
private int toAngle = 0;
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
FileInputStream hero_image_input = new FileInputStream("C:\Users\A650262\Desktop\police-310208_1280.png");
Image hero_image = new Image(hero_image_input);
hero = new ImageView(hero_image);
hero.setFitHeight(120);
hero.setFitWidth(200);
BorderPane root = new BorderPane();
Scene scene = new Scene(root,800,800);
scene.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event)
{
switch(event.getCode())
{
case UP: goNorth = true; break;
case DOWN: goSouth = true; break;
case LEFT: goWest = true; break;
case RIGHT: goEast = true; break;
}
}
});
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event)
{
switch(event.getCode())
{
case UP: goNorth = false; break;
case DOWN: goSouth = false; break;
case LEFT: goWest = false; break;
case RIGHT: goEast = false; break;
}
}
});
root.getChildren().addAll(hero);
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
int dx = 0, dy = 0;
if (goNorth) dy -= 5;
if (goSouth) dy += 5;
if (goEast) rotate(3);
if (goWest) rotate(-3);
moveHeroBy(dx, dy);
}
};
timer.start();
}
public static void main(String[] args) {
launch(args);
}
private void moveHeroBy(int dx, int dy) {
if (dx == 0 && dy == 0) return;
double x = dx + hero.getLayoutX();
double y = dy + hero.getLayoutY();
moveHeroTo(x,y);
}
private void moveHeroTo(double x, double y) {
hero.relocate(x, y);
System.out.println(hero.getNodeOrientation() + " " + hero.getEffectiveNodeOrientation());
}
public void rotate(int angle){
toAngle = toAngle + angle;
if (toAngle == 360)
{
toAngle = 0;
}
hero.setRotate(toAngle);
}
}
使用Rotate
和Translate
转换来移动图像。这样您就可以使用 Rotate
将汽车最初朝向的方向转换为当前朝向。
private final Rotate rotate = new Rotate();
private final Translate translate = new Translate();
// upwards facing direction TODO: adjust
private final Point2D initialDirection = new Point2D(0, -1);
rotate.setPivotX(hero.getFitWidth() / 2);
rotate.setPivotY(hero.getFitHeight() / 2);
hero.getTransforms().addAll(translate, rotate);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
double delta = 0;
if (goNorth) {
delta += 5;
}
if (goSouth) {
delta -= 5;
}
if (goEast) {
rotate(3);
}
if (goWest) {
rotate(-3);
}
Point2D pt = rotate.deltaTransform(initialDirection.multiply(delta));
moveHeroBy(pt.getX(), pt.getY());
}
};
private void moveHeroBy(double dx, double dy) {
if (dx == 0 && dy == 0) {
return;
}
double x = dx + translate.getX();
double y = dy + translate.getY();
translate.setX(x);
translate.setY(y);
}
public void rotate(int angle) {
angle += rotate.getAngle();
if (angle == 360) {
angle = 0;
}
rotate.setAngle(angle);
}
我想让一个 imageView 对象朝它所面对的方向移动。
我让它在 X/Y 轴上移动,也让它旋转,但我真的不知道如何解决这个向它所面对的方向移动的问题(例如,在我的代码中我使用汽车,我只想让它朝它面对的方向移动)
我感谢任何可以让我学习的想法或教程。
public class Main extends Application {
private static final double W = 800;
private static final double H = 600;
private boolean goNorth, goSouth, goWest, goEast;
private ImageView hero;
private int toAngle = 0;
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
FileInputStream hero_image_input = new FileInputStream("C:\Users\A650262\Desktop\police-310208_1280.png");
Image hero_image = new Image(hero_image_input);
hero = new ImageView(hero_image);
hero.setFitHeight(120);
hero.setFitWidth(200);
BorderPane root = new BorderPane();
Scene scene = new Scene(root,800,800);
scene.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event)
{
switch(event.getCode())
{
case UP: goNorth = true; break;
case DOWN: goSouth = true; break;
case LEFT: goWest = true; break;
case RIGHT: goEast = true; break;
}
}
});
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event)
{
switch(event.getCode())
{
case UP: goNorth = false; break;
case DOWN: goSouth = false; break;
case LEFT: goWest = false; break;
case RIGHT: goEast = false; break;
}
}
});
root.getChildren().addAll(hero);
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
int dx = 0, dy = 0;
if (goNorth) dy -= 5;
if (goSouth) dy += 5;
if (goEast) rotate(3);
if (goWest) rotate(-3);
moveHeroBy(dx, dy);
}
};
timer.start();
}
public static void main(String[] args) {
launch(args);
}
private void moveHeroBy(int dx, int dy) {
if (dx == 0 && dy == 0) return;
double x = dx + hero.getLayoutX();
double y = dy + hero.getLayoutY();
moveHeroTo(x,y);
}
private void moveHeroTo(double x, double y) {
hero.relocate(x, y);
System.out.println(hero.getNodeOrientation() + " " + hero.getEffectiveNodeOrientation());
}
public void rotate(int angle){
toAngle = toAngle + angle;
if (toAngle == 360)
{
toAngle = 0;
}
hero.setRotate(toAngle);
}
}
使用Rotate
和Translate
转换来移动图像。这样您就可以使用 Rotate
将汽车最初朝向的方向转换为当前朝向。
private final Rotate rotate = new Rotate();
private final Translate translate = new Translate();
// upwards facing direction TODO: adjust
private final Point2D initialDirection = new Point2D(0, -1);
rotate.setPivotX(hero.getFitWidth() / 2);
rotate.setPivotY(hero.getFitHeight() / 2);
hero.getTransforms().addAll(translate, rotate);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
double delta = 0;
if (goNorth) {
delta += 5;
}
if (goSouth) {
delta -= 5;
}
if (goEast) {
rotate(3);
}
if (goWest) {
rotate(-3);
}
Point2D pt = rotate.deltaTransform(initialDirection.multiply(delta));
moveHeroBy(pt.getX(), pt.getY());
}
};
private void moveHeroBy(double dx, double dy) {
if (dx == 0 && dy == 0) {
return;
}
double x = dx + translate.getX();
double y = dy + translate.getY();
translate.setX(x);
translate.setY(y);
}
public void rotate(int angle) {
angle += rotate.getAngle();
if (angle == 360) {
angle = 0;
}
rotate.setAngle(angle);
}