[英]JavaFX Refreshing To repaint a picture in new Location
我實際上正在學習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;
}
}
我不知道我是否在正確的部分發帖,這是我的第一篇文章。
謝謝你們 !
以下代碼經過測試和工作,但具有不同的圖形。 (我不得不重做加載以使用我的Eclipse文件結構。)
可以使用“root”節點作為鍵盤事件處理程序。 最重要的是要記住要使它成為FocusTraversable並賦予它Focus。
我在Charac中刪除了一些不需要的代碼,並將圖形預加載到一個數組中,因此結構更像是一個精靈,因此每次更改時都不會加載新圖形(相對昂貴)方向。
有一些冗余代碼(三行)初始化ImageView,然后在KeyEvent Handling例程中更新它。 可能可以而且應該被淘汰。
主要是您可以直接設置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。 你可以在這里查看 。 但那時我仍然沒有想到我可以簡單地使用Group節點root進行鍵盤處理。
按下按鍵后,您需要更新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());
}
});
如果將圖像文件加載到數組中,也可能是最好的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.