i am trying to create a videoplayer with javafx, but when I try to run the class, it shows some error
public class MoviePlayer extends Application{
public static void main (String [] args){
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Group root = new Group();
Media media = new Media("C:\\Users\\goldAnthony\\Videos\\Whistle.mp4");
MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
root.getChildren().add(view);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
}
this is the error it shows
Error: Could not find or load main class Player.VideoPlayer2
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
some i have asked says the code works fine, i dont really know what seems to be the problem because it gives this error above
Related
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 trying to create a stage in JavaFX that can pop up to the front in windows, while the main stage stays minimized.
This only works however, when the main stage is visible on the screen. I have tried making it work using Modality, but then the user can't interact with the main stage, which is not what i want.
The problem can be reproduced with the following Application:
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Scene mainScene = new Scene(new Parent() {});
Stage mainStage = new Stage();
mainStage.setScene(mainScene);
mainStage.show();
mainStage.setIconified(true);
Scene popUpScene = new Scene(new Parent() {});
Stage popUpStage = new Stage();
popUpStage.setScene(popUpScene);
Thread.sleep(5000);
popUp(popUpStage);
}
public static void popUp(Stage popUpStage){
if (popUpStage.isIconified()) popUpStage.setIconified(false);
popUpStage.show();
popUpStage.requestFocus();
popUpStage.toFront();
}
}
Is there anyone who has an answer to this problem?
Just add these two line to popUp. First line brings it to front. Second line allows interaction with the main stage or other windows.
popUpStage.setAlwaysOnTop(true);
popUpStage.setAlwaysOnTop(false);
I'm working on JavaFX 8 app and I'm trying to open MP3 file using MediaPlayer.
I had passed wrong URL errors and I've no exceptions right now, when I start such part of code, but app opens and there is no sound. Tried with some oracle tutorial and when i put such URL: "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv" everything is working so I guess it's still wrong URL, but app is starting and I've litterally no clue whats wrong.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
//Add a scene
Group root = new Group();
Scene scene = new Scene(root, 500, 200);
File file = new File("C:\\Users\\Me\\Desktop\\SomeFile.mp3");
Media media = new Media(file.toURI().toASCIIString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
// create mediaView and add media player to the viewer
MediaView mediaView = new MediaView(mediaPlayer);
((Group)scene.getRoot()).getChildren().add(mediaView);
//show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Considering the file is correctly located and loaded you can do the following:
file.toURI().toURL().toExternalForm()
If you are writing an app that plays various media files, consider using user interface to obtain paths to external file resources instead of hardcoding them. You can use FileChooser or DirectoryChooser
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());
I recently found javafx 2.1 very useful for my project of making a video player but after a
success I faced a problem with the video size Dimensions. In other words, when I run the
program and video is playing normally I can't see the whole video because it's dimensions
are bigger than my screen resolution .What Can I do in the following code to resize the actual size of video in windows7 64bit:
public class HelloFx extends Application {
public static void main(String[] args){
launch(args);
}
#Override
public void start(final Stage stage) throws Exception {
stage.setTitle("Movie Player");
final BorderPane root = new BorderPane();
final Media media = new Media("file:///Users//user//Videos//Sintel.mp4");
final MediaPlayer player = new MediaPlayer(media);
final MediaView view = new MediaView(player);
// System.out.println("media.width: "+media.getWidth());
root.getChildren().add(view);
final Scene scene = new Scene(root, 400, 400, Color.BLACK);
stage.setScene(scene);
stage.show();
player.play();
player.setOnReady(new Runnable() {
#Override
public void run() {
int w = player.getMedia().getWidth();
int h = player.getMedia().getHeight();
stage.setMinWidth(w);
stage.setMinHeight(h);
}
});
//player.play();
}
}
The JavaFX 2 MediaView class has 2 functions which can help. They are .setFitHeight() and .setFitWidth() .
So, you could, instead of letting the media dictate the size of screen, let your stage set the size of the screen...
public void run() {
int w = stage.getWidth(); // player.getMedia().getWidth();
int h = stage.getHeight(); // player.getMedia().getHeight();
// stage.setMinWidth(w);
// stage.setMinHeight(h);
// make the video conform to the size of the stage now...
player.setFitWidth(w);
player.setFitHeight(h);
}
Then the video should fit inside of the stage. That above code is pretty crude, and you may want to "Scale" the video better, ie: find the ratio of the media width VS the stage width & media height VS stage height ... But that code above should get you started.