Created
January 27, 2016 18:22
-
-
Save ulmangt/2d80d7f57139f9e7c9b6 to your computer and use it in GitHub Desktop.
Demonstrates using a SimpleTextPainter to display the position of the cursor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2012, Metron, Inc. | |
| * All rights reserved. | |
| * | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions are met: | |
| * * Redistributions of source code must retain the above copyright | |
| * notice, this list of conditions and the following disclaimer. | |
| * * Redistributions in binary form must reproduce the above copyright | |
| * notice, this list of conditions and the following disclaimer in the | |
| * documentation and/or other materials provided with the distribution. | |
| * * Neither the name of Metron, Inc. nor the | |
| * names of its contributors may be used to endorse or promote products | |
| * derived from this software without specific prior written permission. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| * DISCLAIMED. IN NO EVENT SHALL METRON, INC. BE LIABLE FOR ANY | |
| * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| package com.metsci.glimpse.examples.basic; | |
| import com.metsci.glimpse.axis.Axis2D; | |
| import com.metsci.glimpse.axis.listener.AxisListener2D; | |
| import com.metsci.glimpse.examples.Example; | |
| import com.metsci.glimpse.layout.GlimpseLayout; | |
| import com.metsci.glimpse.layout.GlimpseLayoutProvider; | |
| import com.metsci.glimpse.painter.decoration.BackgroundPainter; | |
| import com.metsci.glimpse.painter.decoration.BorderPainter; | |
| import com.metsci.glimpse.painter.decoration.CrosshairPainter; | |
| import com.metsci.glimpse.painter.decoration.GridPainter; | |
| import com.metsci.glimpse.painter.info.SimpleTextPainter; | |
| import com.metsci.glimpse.painter.info.SimpleTextPainter.HorizontalPosition; | |
| import com.metsci.glimpse.painter.info.SimpleTextPainter.VerticalPosition; | |
| import com.metsci.glimpse.plot.Plot2D; | |
| import com.metsci.glimpse.support.color.GlimpseColor; | |
| import com.metsci.glimpse.support.font.FontUtils; | |
| /** | |
| * Demonstrates using a SimpleTextPainter to display the position of the cursor. | |
| * | |
| * @author ulman | |
| */ | |
| public class CursorPositionExample implements GlimpseLayoutProvider | |
| { | |
| public static void main( String[] args ) throws Exception | |
| { | |
| Example.showWithSwing( new CursorPositionExample( ) ); | |
| } | |
| @Override | |
| public GlimpseLayout getLayout( ) | |
| { | |
| Plot2D plot = new Plot2D( "plot" ); | |
| GlimpseLayout plotLayout = plot.getLayoutCenter( ); | |
| // add a painter to paint a solid dark background on the plot | |
| plotLayout.addPainter( new BackgroundPainter( false ) ); | |
| // add a painter to display grid lines | |
| GridPainter gridPainter = new GridPainter( plot.getLabelHandlerX( ), plot.getLabelHandlerY( ) ); | |
| plotLayout.addPainter( gridPainter ); | |
| // add a painter to display mouse selection crosshairs | |
| CrosshairPainter crosshairPainter = new CrosshairPainter( ); | |
| crosshairPainter.setCursorColor( GlimpseColor.getBlack( ) ); | |
| plotLayout.addPainter( crosshairPainter ); | |
| // add a painter to paint a simple line border on the plot | |
| plotLayout.addPainter( new BorderPainter( ).setColor( GlimpseColor.getBlack( ) ) ); | |
| // add axis and plot labels | |
| plot.setAxisLabelX( "Axis X" ); | |
| plot.setAxisLabelY( "Axis Y" ); | |
| plot.setTitle( "Plot Title" ); | |
| // setup SimpleTextPainter to show cursor position at bottom of screen | |
| // create the painter and add it to the plot | |
| final SimpleTextPainter textPainter = new SimpleTextPainter( ); | |
| plotLayout.addPainter( textPainter ); | |
| // set colors and fonts | |
| textPainter.setBackgroundColor( GlimpseColor.getBlack( 0.4f ) ); | |
| textPainter.setColor( GlimpseColor.getWhite( ) ); | |
| textPainter.setPaintBackground( true ); | |
| textPainter.setFont( FontUtils.getVerdanaBold( 16.0f ) ); | |
| // position the text 5 pixels up and to the right from the bottom left edge | |
| textPainter.setVerticalPadding( 5 ); | |
| textPainter.setHorizontalPadding( 5 ); | |
| textPainter.setHorizontalPosition( HorizontalPosition.Left ); | |
| textPainter.setVerticalPosition( VerticalPosition.Bottom ); | |
| // add an axis listener which updates the displayed text to reflect the cursor position | |
| plot.getAxis( ).addAxisListener( new AxisListener2D( ) | |
| { | |
| @Override | |
| public void axisUpdated( Axis2D axis ) | |
| { | |
| double x = axis.getAxisX( ).getSelectionCenter( ); | |
| double y = axis.getAxisY( ).getSelectionCenter( ); | |
| textPainter.setText( String.format( "( %.2f, %.2f )", x, y ) ); | |
| } | |
| }); | |
| return plot; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment