Add a slider for some parameter

From JReality Wiki
Jump to: navigation, search

Source file: SliderExample

Run as Java webstart


Mathematical objects often come in families. When one visualizes those objects it is desirable to have sliders that enable the user to go through sees families.

Suppose we want to visualize the associated family of the helicoid and cateonid, i.e., the immersions <math>(u,v)\mapsto ( \cos\alpha \sinh v \sin u + \sin\alpha \cosh v \cos u, -\cos \alpha \sinh v \cos u + \sin \alpha \cosh v \sin u, u \cos \alpha + v \sin \alpha) </math>


Let us change the tutorial Use a parametric surface factory so that it performs this task.

First of all add a class that implements de.jreality.geometry.Immersion and has a parameter, for our task one may add the following class:


public static class HelicoidCatenoid implements Immersion {
private double alpha;
public double getAlpha() {
	return alpha;
}
public void setAlpha(double alpha) {
	this.alpha = alpha;
}
public void evaluate(double u, double v, double[] xyz, int index) {
	xyz[index]= Math.cos(alpha) * Math.sinh(v) * Math.sin(u) + Math.sin(alpha) * Math.cosh(v) * Math.cos(u);
	xyz[index+2]= -Math.cos(alpha) * Math.sinh(v) * Math.cos(u) + Math.sin(alpha) * Math.cosh(v) * Math.sin(u);
	xyz[index+1]= u * Math.cos(alpha) + v * Math.sin(alpha);
}
public int getDimensionOfAmbientSpace() { return 3;	}
public boolean isImmutable() { return false; }
};


Then adapt the code of Use a parametric surface factory.

To have access to the immersion and the factory from the listener of the slider, we have to put their references into final variables. So we need to replace the line which instantiates the ParametricSurfaceFactoryby


final HelicoidCatenoid helicoidCatenoid = new HelicoidCatenoid();
final ParametricSurfaceFactory psf = new ParametricSurfaceFactory(helicoidCatenoid);


Add all of the following before the call of JRViewer.startup(). Create a slider as usual, but use de.jreality.ui.JSliderVR.


final int steps=60;
final JSliderVR slider=new JSliderVR(0,steps,0);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
		helicoidCatenoid.setAlpha( 2*Math.PI * slider.getValue()/ steps );
		psf.update();
	}
});


Finally wrap the slider in a de.jreality.plugin.ViewSchrinkPanelPlugin as in the example before, but add a layout manager to the swing component of the ViewShrinkPanelPlugin, otherwise the slider would be very small.


ViewShrinkPanelPlugin plugin = new ViewShrinkPanelPlugin() {
	public PluginInfo getPluginInfo() {
		return new PluginInfo("alpha");
	}
};
plugin.getShrinkPanel().setLayout(new GridBagLayout());
plugin.getShrinkPanel().add(slider);
v.registerPlugin(plugin);


In the second line we need to add a layout manager to the swing component of the ViewShrinkPanelPlugin, otherwise the slider would be very small.


Previous: Add an inspector for a factory Developer Tutorial: Contents Next: Developer Tutorial