Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Created October 31, 2013 03:20
Show Gist options
  • Save ulmangt/7243941 to your computer and use it in GitHub Desktop.
Save ulmangt/7243941 to your computer and use it in GitHub Desktop.
CustomTimelinePainterExample
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import com.metsci.glimpse.axis.Axis2D;
import com.metsci.glimpse.context.GlimpseBounds;
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.painter.base.GlimpseDataPainter2D;
import com.metsci.glimpse.plot.timeline.StackedTimePlot2D;
import com.metsci.glimpse.plot.timeline.layout.TimePlotInfo;
import com.metsci.glimpse.support.color.GlimpseColor;
import com.metsci.glimpse.support.settings.OceanLookAndFeel;
import com.metsci.glimpse.util.units.Azimuth;
import com.metsci.glimpse.util.units.time.TimeStamp;
public class CustomTimelinePainterExample extends HorizontalTimelinePlotExample
{
public static void main( String[] args ) throws Exception
{
Example example = Example.showWithSwing( new CustomTimelinePainterExample( ) );
// use the ocean look and feel
example.getCanvas( ).setLookAndFeel( new OceanLookAndFeel( ) );
}
@Override
public StackedTimePlot2D getLayout( )
{
final StackedTimePlot2D plot = ( StackedTimePlot2D ) super.getLayout( );
GlimpseDataPainter2D painter = new GlimpseDataPainter2D( )
{
@Override
public void paintTo( GL2 gl, GlimpseBounds bounds, Axis2D axis )
{
// we'll draw the base of our arrow at this y position
double dataValue = 5.0f;
// we'll draw the base of our arrow at this time (x position)
TimeStamp time = plot.getEpoch( ).getTimeStamp( );
// the arrow will point "north east" (i.e. up and to the right)
double direction = Azimuth.fromNavDeg( 45 );
// the arrow will be 30 pixels long
// (note, the position of the base of the arrow is defined in axis coordinates
// but the position of the head is defined by a direction and length in pixels)
double size = 30;
// convert the time to an axis coordinate
double x1 = plot.getEpoch( ).fromTimeStamp( time );
// our data value is already in axis coordinates
double y1 = dataValue;
// calculate the offset in pixels for the tip of the arrow
// in the x and y directions
double pixelOffsetX = Math.cos( direction ) * size;
double pixelOffsetY = Math.sin( direction ) * size;
// convert the pixel offsets to axis coordinates
// (Axis2D is size aware, so it can report the number of screen pixels per axis unit)
double axisOffsetX = pixelOffsetX / axis.getAxisX( ).getPixelsPerValue( );
double axisOffsetY = pixelOffsetY / axis.getAxisY( ).getPixelsPerValue( );
// calculate the coordinates of the arrow tip in axis coordinates
double x2 = x1 + axisOffsetX;
double y2 = y1 + axisOffsetY;
// set the line color and size
gl.glLineWidth( 1.0f );
gl.glColor4fv( GlimpseColor.getRed( ), 0 );
// draw the arrow (just as a line for simplicity of the example)
gl.glBegin( GL.GL_LINES );
try
{
gl.glVertex2d( x1, y1 );
gl.glVertex2d( x2, y2 );
}
finally
{
gl.glEnd( );
}
}
};
// get a timeline plot handle
TimePlotInfo speedPlot = plot.getTimePlot( "speed-plot-1-id" );
// add our custom paitner to the plot
speedPlot.addPainter( painter );
return plot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment