JavaFX刷新在新位置重新绘制图片

标签 java javafx

我实际上正在学习 JavaFX 并尝试制作一个简单的应用程序 只需用箭头移动人物图片。

我做了 onKeyPressed 部分,它正在工作,我可以修改对象的变量 locationX 和 locationY,但图像没有移动,是吗? 像“repaint()”这样的方法可以简单地做到这一点?或者我必须使用动画?我暂时不想使用动画。

Fenetre.java

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Fenetre extends Application{

    public Charac c1;

    public void init(){
        c1 = new Charac(110,110);
    }

    public void start(Stage stage){
        ImageView ivChar = new ImageView();
        ivChar.setImage(c1.getImg());
        ivChar.relocate((double)c1.getLocationX(),(double)c1.getLocationY());

        HBox box = new HBox();
        box.setTranslateX((double)c1.getLocationX());
        box.setTranslateY((double)c1.getLocationY());
        box.getChildren().add(ivChar);
        box.setFocusTraversable(true);
        box.setOnKeyPressed(new EventHandler<KeyEvent>(){
            @Override
            public void handle(KeyEvent e) {
                switch(e.getCode()){
                    case UP:
                        c1.goUp();
                        break;
                    case DOWN:
                        c1.goDown();
                        break;
                    case LEFT:
                        c1.goLeft();
                        break;
                    case RIGHT:
                        c1.goRight();
                        break;
                default:
                    break;
                }   
            }
        });

        Group root = new Group();
        root.getChildren().add(box);
        Scene scene = new Scene(root);
        scene.setFill(Color.BLACK);


        stage.setWidth(600);
        stage.setHeight(600);   
        stage.setTitle("BBMAN");
        stage.setScene(scene);
        stage.show();


    }
}

Charac.java

import javafx.scene.Parent;
import javafx.scene.image.Image;

public class Charac extends Parent{
    private int locationX=0;
    private int locationY=0;
    private Image img = new Image("/Images/bbfront.png");

    public Charac(int locationX, int locationY){
        this.locationY=locationY;
        this.locationX=locationX;


    }


    public void setLocationX(int locationX){
        this.locationX=locationX;
    }

    public int getLocationX(){
        return this.locationX;
    }

    public int getLocationY(){
        return this.locationY;
    }
    public void setLocationY(int locationY){
        this.locationY=locationY;
    }

    public void goRight(){
        this.locationX+=1;
        this.img = new Image("/Images/bbright.png");
    }

    public void goLeft() {
        this.locationX-=1;
        this.img = new Image("/Images/bbleft.png");
    }

    public void goUp(){
        this.locationY-=1;
        this.img = new Image("/Images/bbup.png");
    }

    public void goDown(){
        this.locationY+=1;
        this.img = new Image("/Images/bbfront.png");

    }

    public Image getImg(){
        return this.img;
    }
}

我不知道我是否在正确的部分发帖,这是我的第一篇文章。

谢谢大家!

最佳答案

以下代码经过测试并且可以工作,尽管图形不同。 (我必须重新加载才能使用我的 Eclipse 文件结构。)

可以使用“根”节点作为键盘事件处理程序。最重要的是要记住使其成为 FocusTraversable 并为其提供 Focus。

我在你的Charac中删除了一些不需要的代码,并将图形预加载到一个数组中,这样结构就更像是一个 Sprite ,这样你就不会每次都加载一个新的图形(相对昂贵)是时候改变方向了。

有一些冗余代码(三行)用于初始化 ImageView,然后在 KeyEvent 处理例程中更新它。或许可以而且应该被消除。

最主要的是你可以直接设置ImageView的X和Y,以及更新ImageView显示的图像。我发现 ImageView 是一个非常方便的类,因此我更频繁地使用它。改变不透明度和缩放比例的能力也非常好。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CharacMove extends Application
{
    public static void main(String[] args) 
    {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception 
    {
        Group root = new Group();
        Scene scene = new Scene(root);

        scene.setFill(Color.BLACK);
        stage.setWidth(600);
        stage.setHeight(600);   
        stage.setTitle("BBMAN");
        stage.setScene(scene);

        Charac c1 = new Charac();
        ImageView ivChar = new ImageView();
        ivChar.setImage(c1.getImage());
        ivChar.setX(c1.getX());
        ivChar.setY(c1.getY());

        root.setFocusTraversable(true);
        root.requestFocus();
        root.setOnKeyPressed(new EventHandler<KeyEvent>()
        {
            @Override
            public void handle(KeyEvent e) 
            {
                switch(e.getCode())
                {
                    case UP:
                        c1.goUp();
                        break;
                    case DOWN:
                        c1.goDown();
                        break;
                    case LEFT:
                        c1.goLeft();
                        break;
                    case RIGHT:
                        c1.goRight();
                        break;
                    default:
                        break;
                }
                ivChar.setImage(c1.getImage());
                ivChar.setX(c1.getX());
                ivChar.setY(c1.getY());
            }
        });

        root.getChildren().add(ivChar);
        stage.show();
    }

    class Charac
    {
        private Image[] img;
        private double xLoc, yLoc;
        private int currentImg;    

        public Image getImage() { return img[currentImg]; }
        public double getX() { return xLoc; }
        public double getY() { return yLoc; }

        public Charac()
        {
            img = new Image[4];
            img[0] = new Image(getClass().getResource("Images/bbright.png").toString());
            img[1] = new Image(getClass().getResource("Images/bbleft.png").toString());
            img[2] = new Image(getClass().getResource("Images/bbup.png").toString());
            img[3] = new Image(getClass().getResource("Images/bbfront.png").toString());
        }

        public void goRight()
        {
            xLoc += 1;
            currentImg = 0;
        }

        public void goLeft() 
        {
            xLoc -= 1;
            currentImg = 1;
        }

        public void goUp()
        {
            yLoc -= 1;
            currentImg = 2;
        }

        public void goDown()
        {
            yLoc += 1;
            currentImg = 3;
        }
    }
}

当我开始使用 JavaFX 时,我在 java-gaming.org 上编写并发布了一个简单的游戏动画教程,该教程又需要几个步骤,例如,如果/当您准备好探索它时,使用 AnimationTimer。您可以查看here 。但当时我仍然没有想到我可以简单地使用 Group 节点根来进行键盘处理。

关于JavaFX刷新在新位置重新绘制图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222017/

相关文章:

java - 将jsp表单中的整数插入数据库表中?

java - 与 ObservableMap 绑定(bind)的组合

java - 如何在 JavaFX 桌面应用程序中使用 Google Maps API?

java - 在图表上添加文本字段

java - Weka 过滤器导致数据丢失

java - 使用 lambda 交换流中的值

java - 如何在 Apache Tomcat Tribes 集群中启用成员身份验证

fullscreen - 全屏模式下有多个JavaFX阶段

javafx:将属性绑定(bind)在一起并在 TableView 中显示它们

java - 使用 CriteriaUpdate 查询时如何避免硬编码 attributeName?