我制作了一个简单的Java游戏,当我加载游戏时会弹出一个启动菜单,弹出一个简单的播放按钮和一个关闭按钮,如果我按下开始按钮,Java会更改为另一个类,该类将用户带到游戏,如果我按下关闭游戏关闭。目前游戏菜单非常无聊我希望在启动菜单中添加一个视频,这是否可以使用Java代码,如果是这样,是否很复杂?
我的菜单到目前为止的代码如下所示,主要的两个方法是渲染和更新方法,渲染告诉java要添加到屏幕的内容,更新方法告诉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;
}
另外,我不需要任何音频,我只需要视频。
提前谢谢。
答案 0 :(得分: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
<强>参考强>
答案 1 :(得分:2)
我建议你看看Xuggler,因为我认为JMF不再被广泛使用了。
我还会看看这个Stack Overflow Question/Answer因为它确实很好地回答了你的问题。