2

私は単純な Java ゲームを作成しました。ゲームをロードすると、単純な再生ボタンと閉じるボタンを含むスタートアップ メニューがポップアップします。スタート ボタンを押すと、Java が別のクラスに変更され、ユーザーがゲームに移動します。閉じるを押すと、ゲームがシャットダウンします。現在、ゲーム メニューは非常に退屈です。スタートアップ メニューにビデオを追加したいのですが、これは Java コードを使用して可能ですか? もしそうなら、それは複雑ですか?

メニューのこれまでのコードを以下に示します。主な 2 つのメソッドは 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

JMF はもう広く使用されていないと思うので、 Xugglerをご覧になることをお勧めします。

この Stack Overflow Question/Answerも見てみたいと思います。これは、あなたの質問に非常にうまく答えているからです。

于 2013-01-03T20:04:51.690 に答える