2013-01-03 25 views
2

我做了一个简单的Java游戏,当我加载游戏时,弹出一个简单的播放按钮和一个关闭按钮,如果我按下了启动菜单如果我按下关闭游戏,则启动按钮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

我不知道你为什么显示代码。您可以使用JMF并添加一些方法来播放视频。然而,存在和其他模式如C++包装多媒体库。 – user1929959

回答

2

我会建议你在Xuggler考虑看看,因为我认为JMF不广泛使用了。

我也看看这个堆栈溢出Question/Answer,因为它确实很好地回答你的问题。

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 

参考

+0

谢谢你回答我的问题。我是Java新手,难以将媒体播放器添加到我的主菜单(用我的问题编写的代码),我不得不在我的菜单类中创建一个新类并调用播放器,或者我需要复制并实际放置它进入我的菜单类?另外,是否可以将它放在我的主菜单中的某个点上,而不是占据整个屏幕?还有最后一件事,如果我删除了所有的控件,它仍然可以工作,我只需要视频并且不需要播放按钮,滑块和停止按钮等控件。 –

+0

@RahulKhosla对不起我不知道有关光滑的API我只知道Java和Swing很好。但基本上你应该将视频组件添加到容器中:'add(video,BorderLayout.CENTER); //添加视频组件' –

+0

好的,谢谢你的帮助。 –

相关问题