I was trying to display a picture in a box with JavaFX. I followed methods documented on Oracle, but it still did not work, though it was extremely similar to the example shown on Oracle. My code is here:
public class TesterJavaFX extends Application {
#Override
public void start(Stage primaryStage) {
Image img = new Image("character.png");
ImageView imgview = new ImageView();
imgview.setImage(img);
imgview.setFitWidth(100);
imgview.setPreserveRatio(true);
imgview.setSmooth(true);
imgview.setCache(true);
HBox box = new HBox();
box.getChildren().add(imgview);
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I have a file called rocket.png in the "src" directory. It even shows up on the IDE. But Java causes a illegal argument exception, and i don't know why. Can anybody help me? Thanks.
Note: The imports are all present.
The string passed to the Image constructor is a URL. If the image file is in the root of your classpath, the following should work:
Image img = new Image(getClass().getResource("/character.png").toExternalForm());
Related
I have a code written using javafx and java 11. As I showed in the below code snippet, when I move my ImageView to right of the scene, The ImageView disappears.
At first:
and then after moving it using arrow keys with the help of scene event listener:
then:
and finally:
I don't use fxml. Here is my demo which has the problem too.
public class HelloApplication extends Application {
public String fetchResource(String path) {
return Objects.requireNonNull(getClass().getResource(path)).toString();
}
#Override
public void start(Stage stage) throws IOException {
Rectangle r = new Rectangle(100, 100);
ImageView spaceShip = new ImageView(fetchResource("spaceShip.png"));
spaceShip.setFitHeight(100);
spaceShip.setFitWidth(100);
spaceShip.setX(0);
spaceShip.setY(0);
EventHandler<KeyEvent> keyListener = event -> {
if (event.getCode() == KeyCode.RIGHT) {
spaceShip.setX(spaceShip.getX() + 20);
}
};
Group game = new Group(spaceShip);
Scene scene = new Scene(game, 1840, 1080);
scene.setOnKeyPressed(keyListener);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
I should mention that I create a rectangle and check the scenario with that, And there was not any problem. I think there is some problem with ImageView.
I couldn't reproduce the issue with your code, but I could with SceneBuilder. You may get the desired result if you wrap the Group in a Pane (at least it works in SceneBuilder).
Group game = new Group(spaceShip);
Pane pane = new Pane(game);
Scene scene = new Scene(pane, 1840, 1080);
stage.setScene(scene);
stage.show();
I am completly lost atm. I have been working with scenebuilder and javaFX in the past but I am stuck like 5 hours now and I didnt get a step further. Let me explain:
I have a working java Eclipse Project, using maven dependencies
The Main is where I want to use JavaFX or load a fxml into
The programm takes many many VCC Files and extracts the data to put it all together in an excel
The programm works but I cant load a FXML file into the main or even show a pane in there
Now does my Java Main class has to extend Application? I tried both ways - doenst work.
Some example code:
public void start(Stage primaryStage) {
try {
bpmain = new BorderPane(FXMLLoader.load(new File("src\\fxml\\UserInterface.fxml").toURI().toURL()));
primaryStage.setScene(new Scene(bpmain));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
or this (from original Docs)
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
but this start method is just not getting called... where do I put that?
What my Programm should look like is pretty simple actually. I want a small UI Windows that lets you pick a Folder where the VCC data lives in and a OK Button that basically should run the Main method.
So a TextField that when its picked a Path in the Main gets replaced (filepath) and just a simple OK Button that says: yeah run the main - because the main works perfectly it is just that I cant show that ui and I dont know how to really connect it to the Main.java
Any help is appreciated - Ty
Option 1
public class Launch extends Application {
public static Stage stage = null;
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
this.stage = stage;
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Option 2:
public class SidebarController implements Initializable {
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
void btnHome_OnMouseClicked(MouseEvent event) throws IOException {
BorderPane borderPane = (BorderPane) ((Node) event.getSource()).getScene().getRoot();
Parent sidebar = FXMLLoader.load(getClass().getResource("/fxml/ContentArea.fxml"));
borderPane.setCenter(sidebar);
}
}
I am building an application that has 2 SubScenes, one for GUI and one for displaying 3D models (in my case OBJ).
For the import of the OBJ file I use the ObjModelImporterJFX library by InteractiveMesh.
Like the title says and as seen in this picture the models start to glitch when they are handled by a SubScene. It seems like some covered parts that should not be visible are rendered like they were. Just for comparison this is how it looks normally, when the models are handled direcly by the scene.
Here is my code:
public class Main extends Application{
#Override
public void start(Stage stage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
PerspectiveCamera camera = new PerspectiveCamera(true);
Group model = loadOBJ("course.obj");
Group course = new Group(model);
BorderPane borderPane = new BorderPane();
Pane pane1 = new Pane(course);
Pane pane2 = new Pane(root);
SubScene subScene1 = new SubScene(pane1, 1000, 720);
subScene1.setCamera(camera);
SubScene subScene2 = new SubScene(pane2,500,500);
borderPane.setLeft(subScene1);
borderPane.setRight(subScene2);
Scene scene = new Scene(borderPane, 1280,720, true);
stage.setScene(scene);
stage.show();
}
private Group loadOBJ(String fileName){
URL url = getClass().getResource(fileName);
Group modelRoot = new Group();
ObjModelImporter importer = new ObjModelImporter();
importer.read(url);
for (MeshView view : importer.getImport()){
modelRoot.getChildren().add(view);
}
return modelRoot;
}
public static void main(String[] args){
launch(args);
}
}
I hope that someone has an idea why this is. Thanks in advance :)
UPDATE: To fix this enable depthBuffer by changing the constructor of the SubScene from
SubScene subScene1 = new SubScene(pane1, 1000, 720);
to
SubScene subScene1 = new SubScene(pane1, 1000, 720, true, null);
not just this, other codes have the same problem. just can't use ImageView.
Environment : macOS, IntelliJ
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
public class ShowHboxVbox extends Application {
static String s = "/Users/fangyuan/Desktop/PIC.png";
#Override
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
borderPane.setTop(getHbox());
Scene scene = new Scene(borderPane);
primaryStage.setTitle("title");
primaryStage.setScene(scene);
primaryStage.show();
}
private HBox getHbox() {
HBox hBox = new HBox(15);
hBox.setPadding(new Insets(15,15,15,15));
hBox.setStyle("-fx-background-color: gold");
hBox.getChildren().add(new Button("computer science"));
hBox.getChildren().add(new Button("chemist"));
ImageView imageView = new ImageView(new Image(s));
hBox.getChildren().add(imageView);
return hBox;
}
}
The Image constructor takes a url as a parameter. If you don't put a protocol in it, then it assumes that the item comes off of the classpath. Obviously, /Users/fangyuan/Desktop/PIC.png won't be in your classpath.
To read from a file instead of the classpath, then stick the file:// protocol in front of the path you want to read:
file:///Users/fangyuan/Desktop/PIC.png
Or
Paths.get("/Users/fangyuan/Desktop/PIC.png").toUri().toString()
which would output the same thing.
I am using Javafx (without using FXML), and I am passing the stage into the a controller to change the scene on the stage when a button is clicked. The scene changes correctly but the size of the stage and the scene increases.It increases in size by about 0.1 (in the width) and the height sometimes also increases (not every time).
Here is the controller being used.
public class Controller {
public Controller(){
}
public static void homeButtonhandler(Stage stage){
stage.close();
}
public static void adminButtonhandler(Stage stage){
adminPane adminPane1 = new adminPane(stage);
Scene adminScene = new Scene (adminPane1);
stage.setScene(adminScene);
}}
The adminPane extends another class I created called mainPane which both extend the Pane class. Which have other panes within them to create the GUI structure. The sizing for the main pane is set up like so:
top = createTop(stage);
this.getChildren().addAll(top);
this.setWidth(stage.getWidth());
this.setPrefWidth(stage.getWidth());
this.setMaxWidth(stage.getWidth());
this.setMinWidth(stage.getWidth());
this.setHeight(stage.getHeight());
this.setMaxHeight(stage.getHeight());
this.setMinHeight(stage.getHeight());
this.setPrefHeight(stage.getHeight());
I am testing the classes using:
public class test extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
//primaryStage.setResizable(true);
mainPane myPane = new mainPane(primaryStage);
Scene homeScene = new Scene (myPane);
primaryStage.setScene(homeScene);
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/resources/icons/joulesIcon.png")));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I believe it has something to do with my passing of the stage, any pointers would be much appreciated.
I also found that stuff was being drawn below the bottom of the window when I used primaryStage.setScene(new Scene(root, primaryStage.getWidth(), primaryStage.getHeight())). My case was resolved by using the dimensions of the old scene rather than the dimensions of the primary stage.
public void setScene(Parent root) {
Scene oldScene = primaryStage.getScene();
primaryStage.setScene(oldScene == null
? new Scene(root, primaryStage.getMinWidth(), primaryStage.getMinHeight())
: new Scene(root, oldScene.getWidth(), oldScene.getHeight()));
}