first, here is my code, it works with no errer message:
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.screen.DefaultScreenController;
import de.lessvoid.nifty.screen.Screen;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
- you can embed a jme canvas inside a swing application
*
-
@author pgi
*/
public class Main extends SimpleApplication {
static int event = 1;
private static Screen Demo(final Nifty nifty) {
Screen screen = new ScreenBuilder(“start”) {
{
controller(new DefaultScreenController());
layer(new LayerBuilder(“operation”) {
{
childLayoutCenter();
panel(new PanelBuilder(“t1”) {
{
childLayoutVertical();
alignRight();
width(“20%”);
height(“100%”);
backgroundColor("#a8b8b6");
control(new ButtonBuilder(“but1”, “skills”) {
{
width(“100%”);
}
});
control(new ButtonBuilder(“but2”, “items”) {
{
width(“100%”);
}
});
}
});
}
});
layer(new LayerBuilder(“background”) {
{
childLayoutCenter();
panel(new PanelBuilder(“t2”) {
{
alignLeft();
width(“80%”);
height(“100%”);
backgroundColor("#bec1af");
}
});
}
});
}
}.build(nifty);
return screen;
}
@Override
public void simpleUpdate(float tpf) {
rootNode.detachAllChildren();
if (event == 0) {
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
Nifty nifty = niftyDisplay.getNifty();
guiViewPort.addProcessor(niftyDisplay);
nifty.loadStyleFile(“nifty-default-styles.xml”);
nifty.loadControlFile(“nifty-default-controls.xml”);
Demo(nifty);
nifty.gotoScreen(“start”);
}
if (event == 1) {
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(“Box1”, b);
Material mat = new Material(assetManager,
“Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Blue);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
if (event == 2) {
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(“Box1”, b);
Material mat = new Material(assetManager,
“Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.White);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
if (event == 3) {
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(“Box1”, b);
Material mat = new Material(assetManager,
“Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Green);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
if (event == 4) {
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry(“Box1”, b);
Material mat = new Material(assetManager,
“Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, ColorRGBA.Red);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
}
@Override
public void simpleInitApp() {
// activate windowed input behaviour
flyCam.setDragToRotate(true);
// display a cube
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// create new JME appsettings
AppSettings settings = new AppSettings(true);
settings.setWidth(640);
settings.setHeight(480);
// create new canvas application
final Main app = new Main();
app.setSettings(settings);
app.setShowSettings(false);
app.setDisplayFps(false);
app.setDisplayStatView(false);
app.createCanvas(); // create canvas!
JmeCanvasContext ctx = (JmeCanvasContext) app.getContext();
ctx.setSystemListener(app);
Dimension dim = new Dimension(640, 480);
ctx.getCanvas().setPreferredSize(dim);
// Create Swing window
JFrame window = new JFrame(“Swing Application”);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Fill Swing window with canvas and swing components
JPanel panel = new JPanel(new GridLayout(0, 1)); // a panel
JPanel canvas = new JPanel();
JPanel mainPanel = new JPanel(new FlowLayout());
canvas.add(ctx.getCanvas());
JButton but0 = new JButton(“button1”);
JButton but1 = new JButton(“button2”);
JButton but2 = new JButton(“button3”);
JButton but3 = new JButton(“button4”);
JButton but4 = new JButton(“button5”);
but0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
event = 0;
app.startCanvas();
}
});
but1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
event = 1;
app.startCanvas();
}
});
but2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
event = 2;
app.startCanvas();
}
});
but3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
event = 3;
app.startCanvas();
}
});
but4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
event = 4;
app.startCanvas();
}
});
panel.add(but0); // add some Swing
panel.add(but1);
panel.add(but2);
panel.add(but3);
panel.add(but4);
mainPanel.add(canvas);
mainPanel.add(panel);
window.add(mainPanel);
window.pack();
// Display Swing window including JME canvas!
window.setVisible(true);
app.startCanvas();
}
});
}
}
[/java]
Each button represents a screen.
It is all right if you don’t click button1. However, if you click button1, it will appears a nifty screen, when you try to click another button, the nifty screen does not disappear. I tried almost every nifty option, like adding nifty.exit() on the end of event==0’s block, although it works that it clear the nifty screen, it only shows a blank screen.
I have two question wanting to ask, the first question is how to fix this problem, the second question is that am I using nifty and swing like this code right? will this cause other problems?
Thank you for helper very much~