Okay Matthias, I made a new class for the widget like you said but it seems to be giving me the child already in tree error and cannot find theme: root.
Code:
package com.adrian.spanish;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Widget;
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
import de.matthiasmann.twl.theme.ThemeManager;
import de.matthiasmann.twl.ToggleButton;
public class MatchingStateOne extends BasicGameState{
public Root root = new Root();
public MatchingStateOne(int stateID) {
this.stateID = stateID;
}
@Override
public int getID() {
// TODO Auto-generated method stub
return stateID;
}
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
// construct & configure root widget
if (root == null) { //make sure the widget hasn't already been created
root.setTheme("");
return; }
// save Slick's GL state while loading the theme
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
try {
lwjglRenderer = new LWJGLRenderer();
File file = new File("theme/simple.xml");
theme = ThemeManager.createThemeManager(file.toURL(),lwjglRenderer);
gui = new GUI(root, lwjglRenderer);
gui.applyTheme(theme);
gui.setBackground(theme.getImageNoWarning("image"));
} catch (LWJGLException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally {
// restore Slick's GL state
GL11.glPopAttrib();
}
// connect input
twlInputAdapter = new TWLInputAdapter(gui, container.getInput());
container.getInput().addPrimaryListener(twlInputAdapter);
}
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
twlInputAdapter.render();
}
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
twlInputAdapter.update();
}
public Rectangle newRect(float x, float y, float width, float height) {
return new Rectangle(x, y, width, height);
}
public void addToMap() {
for(int i = 0; i < countries.length; i++) {
map.put(countries[i], capitals[i]);
}
}
/*instance variables*/
private Map<String, String> map = new HashMap<String, String>();
private String[] countries = {"Spain", "Equatorial","Mexico",
"Guatemala","Honduras","El Salvador","Nicaragua","Costa Rica",
"Panama", "Cuba", "Dominican Republic",
"Puerto Rico", "Venezuela", "Colombia", "Ecuador",
"Peru", "Paraguay", "Chile", "Argentina", "Uruguay"};
private String[] capitals = {"Madrid", "Malabo", "Mexico City",
"Guatemala City", "Tegucigalpa",
"San Salvador", "Managua", "San José",
"Panama City", "Havana", "Santo Domingo",
"San Juan", "Caracas", "Bogotá", "Quito",
"Lima", "Asunción", "Santiago", "Buenos Aires", "Montevideo"};
/*Instance variables*/
private int stateID;
/* Max of 20 buttons or add more to the arrays */
private LWJGLRenderer lwjglRenderer;
private ThemeManager theme;
private GUI gui;
private TWLInputAdapter twlInputAdapter;
}
And here is the widget class:
Code:
package com.adrian.spanish;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.Widget;
public class Root extends Widget{
public Root() {
}
public void layout() {
setTheme("");
country.setTheme("countries");
country.setPosition(40, 50);
country.setMaxSize(100, 100);
add(country);
}
/*Ivars*/
public Button country = new Button();
}