Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Created July 25, 2016 16:57
Show Gist options
  • Save ulmangt/7a9600dcad1de1a79cf1f1bec502f293 to your computer and use it in GitHub Desktop.
Save ulmangt/7a9600dcad1de1a79cf1f1bec502f293 to your computer and use it in GitHub Desktop.
package com.metsci.glimpse.examples.basic;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLOffscreenAutoDrawable;
import javax.media.opengl.GLProfile;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import com.metsci.glimpse.axis.Axis1D;
import com.metsci.glimpse.axis.listener.AxisListener1D;
import com.metsci.glimpse.canvas.GlimpseCanvas;
import com.metsci.glimpse.canvas.NewtSwingGlimpseCanvas;
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.gl.util.GLUtils;
import com.metsci.glimpse.plot.ColorAxisPlot2D;
import com.metsci.glimpse.support.settings.SwingLookAndFeel;
public class ManualRepaintExample
{
public static void main( String[] args ) throws Exception
{
Example example = showWithSwing( GLUtils.getDefaultGLProfile( ) );
attachRepaintListener( example );
}
public static void attachRepaintListener( Example example )
{
final GlimpseCanvas canvas = example.getCanvas( );
ColorAxisPlot2D layout = ( ColorAxisPlot2D ) example.getLayout( );
// add an AxisListener1D which repaints the GlimpseCanvas
// whenever one of the plot axes is modified
AxisListener1D repaint = new AxisListener1D( )
{
@Override
public void axisUpdated( Axis1D axis )
{
SwingUtilities.invokeLater( new Runnable( )
{
@Override
public void run( )
{
canvas.getGLDrawable( ).display( );
}
} );
}
};
layout.getAxisX( ).addAxisListener( repaint );
layout.getAxisY( ).addAxisListener( repaint );
layout.getAxisZ( ).addAxisListener( repaint );
}
// Copy of com.metsci.glimpse.examples.Example.showWithSwing which doesn't add an Animator to the Canvas
public static Example showWithSwing( GLProfile profile ) throws Exception
{
// generate a GLContext by constructing a small offscreen framebuffer
final GLOffscreenAutoDrawable glDrawable = GLUtils.newOffscreenDrawable( profile );
GLContext context = glDrawable.getContext( );
// create a SwingGlimpseCanvas which shares the context
// other canvases could also be created which all share resources through this context
final NewtSwingGlimpseCanvas canvas = new NewtSwingGlimpseCanvas( context );
// create a top level GlimpseLayout which we can add painters and other layouts to
ColorAxisPlot2D layout = new HeatMapExample( ).getLayout( );
canvas.addLayout( layout );
// set a look and feel on the canvas (this will be applied to all attached layouts and painters)
// the look and feel affects default colors, fonts, etc...
canvas.setLookAndFeel( new SwingLookAndFeel( ) );
// create a Swing Frame to contain the GlimpseCanvas
final JFrame frame = new JFrame( "Glimpse Example" );
// This listener is added before adding the SwingGlimpseCanvas to the frame because
// NEWTGLCanvas adds its own WindowListener and this WindowListener should receive the WindowEvent first
// (although I'm now not sure how much this matters)
frame.addWindowListener( new WindowAdapter( )
{
@Override
public void windowClosing( WindowEvent e )
{
// dispose of GlimpseLayouts and GlimpsePainters attached to GlimpseCanvas
canvas.disposeAttached( );
// destroy heavyweight canvas and GLContext
canvas.destroy( );
}
} );
// make the frame visible
frame.setSize( 800, 800 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
// add the GlimpseCanvas to the frame
// this must be done on the Swing EDT to avoid JOGL crashes
// when removing the canvas from the frame
// it should also be done after the frame has been made visible or
// the GlimpseCanvas GLEventListener.reshape( ) may be called
// with spurious sizes (possible NEWT bug)
SwingUtilities.invokeAndWait( new Runnable( )
{
@Override
public void run( )
{
frame.add( canvas );
frame.validate( );
}
} );
return new Example( canvas, frame, layout );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment