我是Java新手,我正在使用Netbeans程序。我对如何导入图像甚至使用图形绘制图像非常困惑。我已经设置了我的场景,并希望例如在屏幕上导入多个图像,但我在第一步将图像放到场景中时陷入困境。关于如何使图形工作的任何建议都是一个很好的开始。
package spaceinvaders;
import java.util.Scanner;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/**
*
* @author Twissted
*/
public class SpaceInvaders extends Application {
@Override
public void start ( Stage stage ) throws Exception {
Pane root = new Pane ();
Scene scene = new Scene ( root, 500, 500, Color.BLACK );
stage.setTitle ( "Space Invaders" );
stage.setScene ( scene );
System.out.println ( "Please enter how many rows of aliens you wish to encounter: " );
int r1;
Scanner keyboard = new Scanner ( System.in );
r1 = keyboard.nextInt ();
stage.show ();
}
public static void main (String [] args ) {
launch ( args );
}
}
我明白这可能非常简单,我很喜欢编程部分,但我只是为了我的生活,即使在搜索互联网之后也无法弄清楚如何使该程序的图形部分工作。
答案 0 :(得分:1)
您可以创建ImageView
并将其添加到Pane
。移动其LayoutX
和LayoutY
属性以将其放置在屏幕上。在示例中,文件sprite.png
必须与类相同的包空间。确保以正确的方式打包。
ObservableList<Node> children = pane.getChildren();
ImageView sprite = new ImageView(getClass().getResource("sprite.png").toString());
children.add(sprite);
sprite.setLayoutX(300d);
sprite.setLayoutY(20d);
我仍然建议您使用FXGL。