Nifty add a HoverEffect without XML

I’m trying to get an image made by an imagebuilder brighter when the mouse enters it.

public class GUI extends NiftyJmeDisplay{
	
	public GUI(AssetManager  assetManager,InputManager inputManager,AudioRenderer audioRenderer,ViewPort guiViewPort)
	{
		super(assetManager,inputManager,audioRenderer,guiViewPort);
		
		Nifty gui = getNifty();		
		gui.addScreen("screen", new ScreenBuilder("screen"){{
	             controller(new DefaultScreenController());
	 
	             layer(new LayerBuilder("foreground") {{
	        	    childLayoutCenter();
	                    image(new ImageBuilder("resume") {{
	                        filename("assets/img/menu/bar.jpg");
	                        visibleToMouse(true);
	                        controller(new DefaultScreenController());
			        onHoverEffect(new HoverEffectBuilder(""){{
	                        	//what goes here?
	                        }});
	                    }});
	              }});
             
	      }}.build(gui));
		gui.gotoScreen("screen");
	}
}

I removed all the unneeded parts of my code to make the sample clearer. I’d only be interested in answers that don’t use XML at all. The Nifty manual only explains this part with XML, which I do not want to use.

You would fill it in the same as any other effect. For example:

onHoverEffect(new HoverEffectBuilder("colorBar") {{
    effectParameter("color", "#444f");
}});

In your case I would use the changeImage effect along with a sprite of your normal / hover / clicked button images and switch them with the image mode.

I see. Would one do it like this?

onHoverEffect(new HoverEffectBuilder("changeImage") {{
    effectParameter("image", "assets/img/menu/bar2.jpg");
}});

This seems to give me a nullPointerException.

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
	at de.lessvoid.nifty.effects.impl.ChangeImage.deactivate(ChangeImage.java:40)
	at de.lessvoid.nifty.effects.Effect.setActiveInternal(Effect.java:139)
	at de.lessvoid.nifty.effects.Effect.deactivate(Effect.java:132)
	at de.lessvoid.nifty.effects.EffectProcessorImpl.processHoverDeactivate(EffectProcessorImpl.java:262)
	at de.lessvoid.nifty.effects.EffectManager.handleHoverDeactivate(EffectManager.java:186)
	at de.lessvoid.nifty.elements.Element.mouseEventHoverPreprocess(Element.java:1592)
	at de.lessvoid.nifty.screen.MouseOverHandler.processMouseEvent(MouseOverHandler.java:99)
	at de.lessvoid.nifty.screen.Screen.forwardMouseEventToLayers(Screen.java:369)
	at de.lessvoid.nifty.screen.Screen.mouseEvent(Screen.java:339)
	at de.lessvoid.nifty.Nifty.forwardMouseEventToScreen(Nifty.java:308)
	at de.lessvoid.nifty.Nifty.access$1600(Nifty.java:77)
	at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(Nifty.java:1373)
	at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(Nifty.java:1329)
	at com.jme3.niftygui.InputSystemJme.onMouseMotionEventQueued(InputSystemJme.java:222)
	at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:294)
	at de.lessvoid.nifty.Nifty.update(Nifty.java:288)
	at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:113)
	at com.jme3.input.InputManager.processQueue(InputManager.java:819)
	at com.jme3.input.InputManager.update(InputManager.java:883)
	at com.jme3.app.Application.update(Application.java:604)
	at com.jme3.app.SimpleApplication.update(SimpleApplication.java:231)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
	at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
	at java.lang.Thread.run(Unknown Source)

This is when you need to read the manual. The change image effect has an active and deactive image defined just like the xml. You have the right idea though.

Right. Figured it out, thanks. If anyone strolls by with the same problem, this is the code that works (where bar.jpg is the dark picture and bar2.jpg the highlighted one):

onHoverEffect(new HoverEffectBuilder("changeImage") {{
    effectParameter("active", "assets/img/menu/bar2.jpg");
    effectParameter("inactive", "assets/img/menu/bar.jpg");
}});

And this will be of great help to you: https://github.com/void256/nifty-gui/wiki/Effects#changeImage