2017-04-05 46 views
1

我實際上學習JavaFX並試圖做出一個簡單的應用 用箭頭正確地移動一個字符的圖片。JavaFX刷新重新繪製一個新的位置的圖片

我做了onKeyPressed部分這是工作,我可以修改對象的變量locationX和locationY,但圖像不是mooving,有沒有 有點像「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; 
    } 
} 

我不知道RLY如果我在正確的部分我張貼,這是我的第一篇文章。

謝謝大家!

+0

按下按鈕後,您不會更新HBox的位置。 – Steven

+0

相關,也許是重複:[如何編寫JavaFX的KeyListener](http://stackoverflow.com/questions/29962395/how-to-write-a-keylistener-for-javafx),它圍繞屏幕上的按鍵。 – jewelsea

回答

2

下面的代碼已經過測試,雖然使用不同的圖形。 (我必須重做加載才能使用我的Eclipse文件結構。)

可以使用「根」節點作爲鍵盤事件處理程序。主要的是要記住,使它FocusTraversable並給它的焦點。

我剝離了一些不需要的Charac中的一些代碼,並將圖形預加載到一個數組中,因此結構將更像一個精靈,因此您不會加載新圖形(相對昂貴)每次你改變方向。

有一些冗餘代碼(三行)初始化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 node root來進行鍵盤處理。

1

按下鍵後,您需要更新HBox box的位置。

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; 
      } 
      box.setTranslateY((double)c1.getLocationY()); 
      box.setTranslateX((double)c1.getLocationX()); 
     } 
    }); 

也可能最好,如果您將圖像文件加載到數組。