2

我做了一个简单的 Java 游戏,当我加载游戏时,会弹出一个带有简单播放按钮和关闭按钮的启动菜单,如果我按下开始按钮 Java 会更改为另一个将用户带到游戏的类,如果我按关闭游戏关闭。目前游戏菜单很无聊我想在启动菜单中添加一个视频,这可以使用Java代码吗?如果可以,是否复杂?

到目前为止,我的菜单代码如下所示,主要的两个方法是 render 和 update 方法,render 告诉 java 要添加到屏幕上的内容,而 update 方法告诉 java 在按下按钮时要做什么:

    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;
    }

另外,我不需要任何音频,我只需要视频。

先感谢您。

4

2 回答 2

2

当然可以,使用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

参考

于 2013-01-03T20:03:07.430 回答
2

我建议你看看Xuggler,因为我认为 JMF 不再被广泛使用。

我还会看看这个 Stack Overflow Question/Answer,因为它确实很好地回答了你的问题。

于 2013-01-03T20:04:51.690 回答