Skip to content

Instantly share code, notes, and snippets.

@ulmangt
Last active December 26, 2015 01:58
Show Gist options
  • Save ulmangt/7074556 to your computer and use it in GitHub Desktop.
Save ulmangt/7074556 to your computer and use it in GitHub Desktop.
Example of using PolygonPainter with a StackedTimePlot2D.
import com.metsci.glimpse.examples.Example;
import com.metsci.glimpse.layout.GlimpseAxisLayout2D;
import com.metsci.glimpse.painter.shape.PolygonPainter;
import com.metsci.glimpse.plot.timeline.StackedTimePlot2D;
import com.metsci.glimpse.plot.timeline.data.Epoch;
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.time.Time;
import com.metsci.glimpse.util.units.time.TimeStamp;
public class TimelineShapeDrawingExample extends CollapsibleTimelinePlotExample
{
public static void main( String[] args ) throws Exception
{
Example example = Example.showWithSwing( new TimelineShapeDrawingExample( ) );
// use the ocean look and feel
example.getCanvas( ).setLookAndFeel( new OceanLookAndFeel( ) );
}
@Override
public StackedTimePlot2D getLayout( )
{
StackedTimePlot2D plot = super.getLayout( );
// get a reference to one of the plots
TimePlotInfo plotInfo = plot.getTimePlot( "speed-plot-1-id" );
// get the GlimpseLayout for the plot (to which painters can be added
// in order to add arbitrary graphics to the plot)
GlimpseAxisLayout2D plotLayout = plotInfo.getLayout( );
// create a painter for displaying polygonal shapes
PolygonPainter polygonPainter = new PolygonPainter( );
plotLayout.addPainter( polygonPainter );
// get the timestamp which we'll start drawing our shape at
Epoch epoch = plot.getEpoch( );
TimeStamp baseTime = epoch.getTimeStamp( );
// build the x and y coordinates of the polygon vertices
float[] coordsX = new float[5];
float[] coordsY = new float[5];
coordsX[0] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 1 ) ) );
coordsX[1] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 0.2 ) ) );
coordsX[2] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 0.4 ) ) );
coordsX[3] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 0.6 ) ) );
coordsX[4] = ( float ) epoch.fromTimeStamp( baseTime.add( Time.fromHours( 1 ) ) );
coordsY[0] = 0.0f;
coordsY[1] = 1.0f;
coordsY[2] = 10.0f;
coordsY[3] = 2.0f;
coordsY[4] = 10.0f;
// each polygon is assigned a groupId
// the appearance of the polygons are customized by groups so many
// polygons with the same display characteristics can be drawn efficiently
int groupId = 0;
int polygonId = 0;
// add the polygon
polygonPainter.addPolygon( groupId, polygonId, coordsX, coordsY, 0.0f );
// customize the polygon's appearance
polygonPainter.setFill( groupId, true );
polygonPainter.setFillColor( groupId, GlimpseColor.getGray( 0.3f ) );
polygonPainter.setLineWidth( groupId, 3.0f );
polygonPainter.setLineColor( groupId, GlimpseColor.getGray( 0.9f ) );
return plot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment