JavaFX:我如何为我的舞台设置背景图片
问题描述:
我正在为弹跳球程序编写代码,并且所有内容都工作到目前为止,但我不知道如何为我的舞台设置背景图片。我是java的初学者(很明显),并试图使用ImageView类为我的舞台设置背景。但到目前为止没有运气。JavaFX:我如何为我的舞台设置背景图片
非常感谢。
package particlesystemexample;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ParticleSystemExample extends Application {
@Override
public void start(Stage primaryStage) {
ParticleAnimationPane paPane = new ParticleAnimationPane();
// mouse actions to pause and resume animation
//paPane.setOnMousePressed(e -> paPane.pause()); // Java 8.0
paPane.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
paPane.init(); }
});
// paPane.setOnMouseReleased(e -> paPane.play()); // Java 8.0
paPane.setOnMouseReleased(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
paPane.play(); }
});
// buttons to increase and decrease animation speed
paPane.setOnKeyPressed(e -> {
if(e.getCode() == KeyCode.UP){
paPane.increaseSpeed();
}
else if (e.getCode() == KeyCode.DOWN){
paPane.decreaseSpeed();
}
});
// Create a scene and place it on stage
Scene scene = new Scene(paPane, 450, 350);
primaryStage.setTitle("Particle System Example");
primaryStage.setScene(scene);
primaryStage.show();
paPane.init(); // initialize the particles
paPane.requestFocus();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
编辑:
我使用此代码来显示图像,但它不工作尝试...
StackPane sp = new StackPane();
Image img = new Image("http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg");
ImageView imgView = new ImageView(img);
sp.getChildren().add(imgView);
答
就个人而言,我使用的BufferedImage和图像类来处理我的所有形象需求。 你可以使用这个Image类。
下面是一个例子:
try {
Graphics.drawImage(ImageIO.read(new File("Picture.png")), fX, fY, fW, fH, sX, sY, sW, sH,null);
} catch (IOException e) {
e.printStackTrace();
}
开始与F中的事情是,你将画上它的矩形边界。
以s开头的东西是你画的图片部分的界限。
我不完全确定最后的零点对应于什么。我只是忽略它而且一切正常。
你在哪里尝试使用ImageView类? – Kevin 2014-12-07 01:30:09
创建场景后 – Dude 2014-12-07 01:45:45