English 中文(简体)
JavaFx移动图像
原标题:JavaFx moving images

Hello all so I m Creat a brief 2Dmmer, and so standard, I ve had a bit of a hamaretries to access each other. 到目前为止,我拥有一个主要的菜单、游戏网和显示网上的形象。 我现在要处理这一运动,但我似乎不能让它发挥作用。

<编码>应用。

http://www.un.org/Depts/DGACM/index_french.htm

package application;

import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.Group;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.image.Image;

public class Level {

// private static Array[] level1;
// private WarehouseKeeper warehouse;
private static ImageView image1 = ImageLoader.showWareHouseImage();
private static final int KEYBOARD_MOVEMENT_DELTA = 2;

public static void runLevel1(Stage theStage) {

    // ImageView image = new ImageView((Element)
    // ImageLoader.wareHouseImage);

    // WarehouseKeeper warehouse = new
    // WarehouseKeeper(ImageLoader.wareHouseImage);

    Group root = new Group();

    int columnAmount = 12;
    int rowAmount = 12;

    GridPane gameGrid = new GridPane();

    for (int i = 0; i < columnAmount; i++) {
        ColumnConstraints columnn = new ColumnConstraints(45);
        gameGrid.getColumnConstraints().add(columnn);
    }

    for (int i = 0; i < rowAmount; i++) {

        RowConstraints row = new RowConstraints(45);
        gameGrid.getRowConstraints().add(row);
    }

    gameGrid.setStyle("-fx-background-color: white; -fx-grid-lines-visible:true");
    Scene scene = new Scene(root, (columnAmount * 40) + 66, (rowAmount * 40) + 66, Color.WHITE);

    image(root, gameGrid);

    moveWareHouse(scene, createKeeper());

    theStage.setScene(scene);

    theStage.show();

}

private static void image(Group root, GridPane gameGrid) {

    // ImageLoader.wareHouseImage;

    /*
     * ImageView wareHouse = new ImageView(); wareHouse.setFitHeight(45);
     * wareHouse.setFitWidth(45);
     * 
     * Image image1 = ImageLoader.showWareHouseImage();
     * wareHouse.setImage(image1);
     */

    ImageView image1 = ImageLoader.showWareHouseImage();

    final HBox picture = new HBox();
    picture.getChildren().add(image1);

    gameGrid.setAlignment(Pos.CENTER);

    // gameGrid.getChildren().add(wareHouse);
    gameGrid.add(picture, 7, 9);

    root.getChildren().add(gameGrid);

}

private static WarehouseKeeper createKeeper() {

    final WarehouseKeeper keeper = new WarehouseKeeper(image1);

    return keeper;
}

private static void moveWareHouse(Scene scene, final WarehouseKeeper keeper) {

    scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {

            switch (event.getCode()) {

            case W:
                keeper.setyPosition(keeper.getyPosition() - KEYBOARD_MOVEMENT_DELTA);
                break;
            case D:
                keeper.setxPosition(keeper.getxPosition() + KEYBOARD_MOVEMENT_DELTA);
                break;
            case A:
                keeper.setyPosition(keeper.getyPosition() + KEYBOARD_MOVEMENT_DELTA);
                break;
            case S:
                keeper.setxPosition(keeper.getxPosition() - KEYBOARD_MOVEMENT_DELTA);
                break;
            }
        }
    });
   }
}

http://www.un.org/Depts/DGACM/index_french.htm 扩展图像。

package application;

import javafx.scene.image.ImageView;

import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;

public class WarehouseKeeper extends ImageLoader {

private Image playerImage;
private ImageView image;
private int speed;
private double xPosition;
private double yPosition;

public WarehouseKeeper(ImageView wareHouseImage) {

    super(wareHouseImage);

    this.speed = speed;
    this.xPosition = xPosition;
    this.yPosition = yPosition;
    this.wareHouseImage.relocate(xPosition, yPosition);

}

public void updateUI() {

    wareHouseImage.relocate(xPosition, yPosition);

}

public double getCenterX() {

    return xPosition * 0.5;

}

public double getCenterY() {

    return yPosition * 0.5;

}

public int getSpeed() {
    return speed;
}

public void setSpeed(int speed) {
    this.speed = speed;
}

public double getxPosition() {
    return xPosition;
}

// one of the ideas i had
public void setxPosition(double xPosition) {
    this.xPosition = xPosition.setTranslateX(1.0);
}

public double getyPosition() {
    return yPosition;
}

public void setyPosition(double d) {
    this.yPosition = d + 1.0;
}

public void setTranslateY(double yPosition) {
    // TODO Auto-generated method stub

    yPosition = yPosition + 2.0;

  }
}

http://www.un.org/Depts/DGACM/index_french.htm

package application;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class ImageLoader {

protected ImageView wareHouseImage;
private Object diamondImage;
private Object wallImage;
private Object mapImage;
private Object crateImage;

public ImageLoader(ImageView wareHouseImage) {

    this.wareHouseImage = showWareHouseImage();

}

public ImageView getWareHouseImage() {
    return wareHouseImage;
}

public void setWareHouseImage(Image wareHouseImage) {
    this.wareHouseImage = showWareHouseImage();
}

public static ImageView showWareHouseImage() {

    ImageView wareHouse = new ImageView();
    wareHouse.setFitHeight(45);
    wareHouse.setFitWidth(45);

    Image wareHouseImage = new Image("application/warehouse.png");
    wareHouse.setImage(wareHouseImage);

    return wareHouse;

}

public Object getDiamondImage() {

    return diamondImage;

}

public void setDiamondImage(Object diamondImage) {

    this.diamondImage = diamondImage;

}

public Object getWallImage() {

    return wallImage;

}

public void setWallImage(Object wallImage) {

    this.wallImage = wallImage;

}

public Object getMapImage() {

    return mapImage;

}

public void setMapImage(Object mapImage) {

    this.mapImage = mapImage;

}

public Object getCrateImage() {

    return crateImage;

}

public void setCrateImage(Object crateImage) {

    this.crateImage = crateImage;

    }
}
问题回答

问题可能在于您对 object客如何工作的理解,该物体可能是由WareHouseKeeper和AlexLoader等编造的,但该物体是在级别上播放的,也就是说,如果你将目标放在必须移到级别上的话。 比如,你是

    private static void moveWareHouse(Scene scene, final WarehouseKeeper keeper) {

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

    @Override
    public void handle(KeyEvent event) {

        switch (event.getCode()) {

        case W:
            keeper.setyPosition(keeper.getyPosition() - KEYBOARD_MOVEMENT_DELTA);
            break;
        case D:
            keeper.setxPosition(keeper.getxPosition() + KEYBOARD_MOVEMENT_DELTA);
            break;
        case A:
            keeper.setyPosition(keeper.getyPosition() + KEYBOARD_MOVEMENT_DELTA);
            break;
        case S:
            keeper.setxPosition(keeper.getxPosition() - KEYBOARD_MOVEMENT_DELTA);
            break;
        }
    }
});
}

As the object has been passed to the function you would only need to move it directly from within the level class and not from the other Classes, so this would be changed to

    private static void moveWareHouse(Stage theStage, Group wHK) {

        theStage.addEventFilter(KeyEvent.KEY_RELEASED, e -> {
        e.consume();

        switch (e.getCharacter()) {

        case W:
            keeper.setLayoutY(keeper.getLayoutY() - KEYBOARD_MOVEMENT_DELTA);
            break;
        case D:
            keeper.setLayoutX(keeper.getLayoutX() + KEYBOARD_MOVEMENT_DELTA);
            break;
        case A:
            keeper.setLayoutY(keeper.getLayoutY() + KEYBOARD_MOVEMENT_DELTA);
            break;
        case S:
            keeper.setLayoutX(keeper.getLayoutX() - KEYBOARD_MOVEMENT_DELTA);
            break;
        }
    });
}

EDIT I also noticed you have the Scene parsed as the key listener this would require you to click on the scene (to give it focus) before it will listen to the keys, if you set the stage as the key listener no matter where you click it will always be the root focus and always listening for keys.

EDITED FURTHER As I mentioned in our conversation up the top, your better off creating the entirety of the WarehouseKeeper in your WarehouseKeeper class and instantizing it into your main class (level1) this way the control of the object is within the level1 but level1 does not have all the code to create it (so if you need to modify the creation of it later or use it again you can call more of them by just creating a new instance of it and adding it to the scene) for example.

public final Group warehouseKeeper(ImageView image) {
    Group tempGroup;
    /* insert necessary code here to add image to group and anything else you want in the group */
    return tempGroup;
}

然后,你可以简单地呼吁,将它提高到1级。

final Group wHK = new WarehouseKeeper.warehouseKeeper();
wHK.setLayoutX(/*some init val */);
wHK.setLayoutY(/*some init val */);
root.getChildren().add(wHK);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签