Creating a 3D Menu using FengGUI #4
Now we are going to start off by creating a main menu using the FengGUI. Under your project, you should have a src directory, a lib directory, and you should create an image directory.
In the image directory you should paste the following two images. These images will be used for our main menu. We are only going to initially create a menu with one button that says play. We can later add some more images to quit the game or do other functions.


You should create a package under the src directory called com.guid.asteroid3D and create two classes under that package called Asteroid3DStart.java and MainMenu.java. Paste the following code into those classes.
MainMenu.java
package com.guid.asteroid3D;
import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.GameMenuButton;
import org.fenggui.background.PlainBackground;
import org.fenggui.composites.MessageWindow;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.RowLayout;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;
import org.fenggui.util.Spacing;
public class MainMenu
{
private GameMenuButton play;
public void buildGUI(Display display)
{
final Container c = new Container();
c.getAppearance().add(new PlainBackground(Color.BLACK));
display.addWidget(c);
c.getAppearance().setPadding(new Spacing(10, 10));
c.setLayoutManager(new RowLayout(false));
initButtons(c, display);
buildMainMenu(c, display);
}
private void initButtons(final Container c, final Display display)
{
play = new GameMenuButton("C:/Programming Tools/eclipse/workspace/JMENewProject/src/images/play.jpg", "C:/Programming Tools/eclipse/workspace/JMENewProject/src/images/play1.jpg");
play.addButtonPressedListener(new IButtonPressedListener()
{
public void buttonPressed(ButtonPressedEvent e)
{
MessageWindow mw = new MessageWindow("Nothing to play. Just a demo.");
mw.pack();
display.addWidget(mw);
StaticLayout.center(mw, display);
}
});
}
private void buildMainMenu(final Container c, final Display display)
{
c.removeAllWidgets();
c.addWidget(play);
c.pack();
StaticLayout.center(c, display);
}
}
Asteroid3DStart.java
package com.guid.asteroid3D;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.render.jogl.EventBinding;
import org.fenggui.render.jogl.JOGLBinding;
import com.sun.opengl.util.Animator;
public class Asteroid3DStart extends JFrame implements GLEventListener {
private static final long serialVersionUID=123l;
private GL gl = null;
private GLCanvas canvas = null;
private Display display = null;
public Asteroid3DStart() {
canvas = new GLCanvas();
canvas.addGLEventListener(this);
getContentPane().add(canvas, java.awt.BorderLayout.CENTER);
setSize(600, 400);
setTitle("Asteroid3D");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Animator animator = new Animator(canvas);
animator.start();
}
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL();
gl.glClearColor(1.0f, 0.8f, 0.2f, 0.0f);
display = FengGUI.createDisplay(new JOGLBinding(canvas));
new EventBinding(canvas, display);
MainMenu menu=new MainMenu();
menu.buildGUI(display);
}
public void display(GLAutoDrawable drawable) {
gl.glLoadIdentity();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
display.display();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
// do nothing
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
// do nothing
}
public static void main(String[] args) {
new Asteroid3DStart();
}
}
The Asteroid3DStart.java class contains the main method. We are also going to use it as the games JFrame, and we will use it for the GLEventListener. You create the canvas object which is need to paint the openGL objects. You also need the GLEventListener to keep track of the openGL events. You have to add the canvas to the Frames ContentPane. You also have to activate the animator to keep the canvas painted as events happen. The init method is required by the GLEventListener. In that class, you will create the FengGUI display. You will pass that object to the MainMenu which will build your FengGUI Menu.
In the MainMenu class, you call the method buildGUI. In this class you will create a container that will hold your menu objects. You will also set the layout information in this container. You will call the initButtons method which creates the GameMenuButton object and adds its listener. When the button is pressed, the listener is called. Make sure you set the correct path to the images you downloaded. The second image is the mouseover image. The buildMainMenu adds the object to the container.
Go To #5

0 comments:
Post a Comment