2

I have made a simple Java game, when I load the game a start-up menu pops up with a simple play button and a close button, if I press the start button Java changes to another class which takes the user to the game, if I press close the game shuts down. At the moment the game menu is very boring I wish to add a video to the start-up menu, is this possible using Java code and if so, is it complicated?

My code so far for my menu is shown bellow, the main two methods are the render and update methods, the render tells java what to add to the screen and the update method tells java what to do if a button is pressed:

    package javagame;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState{

    //public String mouse= "no input yet";//COORDS if needed

    Image bg;

    public Menu(int state){
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
    bg = new Image("res/croftbg.png");
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{ 
    bg.draw(0,0);
    //g.drawString(mouse, 10, 50);//COORDS if needed
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
    int posX = Mouse.getX();
    int posY = Mouse.getY();
    //mouse = "mouse pos:" +posX+"  " +posY;//Coords if needed
    //play now button
    if((posX>110 && posX<140) && (posY>5 && posY<25)){//checks if mouse is inside play now button
        if(Mouse.isButtonDown(0)){//checks if left mouse pressed
            sbg.enterState(1);//change to play state ie(1)
        }
    }
    //exit button
    if((posX>510 && posX<535) && (posY>5 && posY<25)){
        if(Mouse.isButtonDown(0)){
            System.exit(0);//closes the widow
        }
    }
}

    public int getID(){
        return 0;
    }

Also, I do not require any audio, I only need the video.

Thank you in advance.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Rahul Khosla
  • 349
  • 9
  • 21
  • I don't know exactly why you show the code. You can use JMF and add some method to play a video. However, exists and others modalities such wrapped multimedia library from C++. – Mihai8 Jan 03 '13 at 20:03

2 Answers2

2

Of course you can, use Java Media Framework

// A JPanel the plays media from a URL
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JPanel;

public class MediaPanel extends JPanel {

    public MediaPanel(URL mediaURL) {
        setLayout(new BorderLayout()); // use a BorderLayout

        // Use lightweight components for Swing compatibility
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);

        try {
            // create a player to play the media specified in the URL
            Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);

            // get the components for the video and the playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();

            if (video != null) {
                add(video, BorderLayout.CENTER); // add video component
            }
            if (controls != null) {
                add(controls, BorderLayout.SOUTH); // add controls
            }
            mediaPlayer.start(); // start playing the media clip
        } // end try
        catch (NoPlayerException noPlayerException) {
            System.err.println("No media player found");
        } // end catch
        catch (CannotRealizeException cannotRealizeException) {
            System.err.println("Could not realize media player");
        } // end catch
        catch (IOException iOException) {
            System.err.println("Error reading from the source");
        } // end catch
    } // end MediaPanel constructor
} // end class MediaPanel

REFERENCES

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Thank you for answering my question. I am new to Java and am having difficulty adding the media player to my main menu (the code written in my question) would I have to make a new class and call the player in my menu class or would I require to copy and actually place it into my menu class? Also, is it possible to position it and place it in a point on my main menu rather than it take up the full screen? and one last thing, would it still work if I removed all of the controls, I only require the video and no controls such as the play button, slider and stop button. – Rahul Khosla Jan 03 '13 at 21:51
  • @RahulKhosla sorry Im not sure about slick API I only know Java and Swing very well. But basically you should add the video component to a container: `add(video, BorderLayout.CENTER); // add video component` – David Kroukamp Jan 03 '13 at 21:52
2

I would suggest you taking a look at Xuggler, as I think JMF is not widely used anymore.

I would also take a look at this Stack Overflow Question/Answer as it really does answer your question quite nicely.

Community
  • 1
  • 1
Halfwarr
  • 7,853
  • 6
  • 33
  • 51