Last active
August 2, 2016 10:40
-
-
Save alicanalbayrak/d9d7169b3965f2bd39aefe310317c27d to your computer and use it in GitHub Desktop.
Glimpse plot example with wrapped axes
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) 2016, 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 javax.media.opengl.GL2; | |
import com.metsci.glimpse.axis.Axis2D; | |
import com.metsci.glimpse.axis.WrappedAxis1D; | |
import com.metsci.glimpse.axis.listener.mouse.AxisMouseListener2D; | |
import com.metsci.glimpse.context.GlimpseBounds; | |
import com.metsci.glimpse.examples.Example; | |
import com.metsci.glimpse.layout.GlimpseAxisLayout2D; | |
import com.metsci.glimpse.layout.GlimpseLayout; | |
import com.metsci.glimpse.layout.GlimpseLayoutProvider; | |
import com.metsci.glimpse.painter.base.GlimpseDataPainter2D; | |
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.group.WrappedPainter; | |
import com.metsci.glimpse.plot.Plot2D; | |
import com.metsci.glimpse.support.color.GlimpseColor; | |
public class SimpleWrappedAxisExample implements GlimpseLayoutProvider { | |
public static void main(String[] args) throws Exception { | |
Example.showWithSwing(new SimpleWrappedAxisExample()); | |
} | |
@Override | |
public GlimpseLayout getLayout() { | |
Plot2D plot = new Plot2D("Simple Wrapped Axis Example"); | |
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"); | |
WrappedAxis1D wrappedAxisX = new WrappedAxis1D(0, 100); | |
WrappedAxis1D wrappedAxisY = new WrappedAxis1D(-100, 100); | |
wrappedAxisX.setMaxSpan(3000); | |
wrappedAxisX.setMin(0); | |
wrappedAxisX.setMax(100); | |
wrappedAxisY.setMaxSpan(3000); | |
wrappedAxisY.setMin(-100); | |
wrappedAxisY.setMax(100); | |
GlimpseAxisLayout2D wrappedLayout = new GlimpseAxisLayout2D(new Axis2D(wrappedAxisX, wrappedAxisY)); | |
plotLayout.addLayout(wrappedLayout); | |
wrappedLayout.getAxis().unlockAspectRatioXY(); | |
wrappedLayout.setLayoutData("pos container.x container.y container.x2 container.y2"); | |
wrappedLayout.addGlimpseMouseAllListener(new AxisMouseListener2D()); | |
wrappedLayout.setEventConsumer(false); | |
WrappedPainter wrappedPainter = new WrappedPainter(); | |
// clear color buffer first! | |
wrappedPainter.addPainter(new GlimpseDataPainter2D() { | |
@Override | |
public void paintTo(GL2 gl, GlimpseBounds bounds, Axis2D axis) { | |
gl.glClearColor(0f, 0f, 0f, 0f); | |
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); | |
} | |
}); | |
wrappedPainter.addPainter(new GlimpseDataPainter2D() { | |
@Override | |
public void paintTo(GL2 gl, GlimpseBounds bounds, Axis2D axis) { | |
gl.glBegin(GL2.GL_TRIANGLES); | |
gl.glColor4f(1f, 0f, 0f, 0.5f); | |
gl.glVertex2d(0, -100); | |
gl.glColor4f(0f, 1f, 0f, 0.5f); | |
gl.glVertex2d(50, 100); | |
gl.glColor4f(0f, 0f, 1f, 0.5f); | |
gl.glVertex2d(100, -100); | |
gl.glEnd(); | |
} | |
}); | |
wrappedLayout.addPainter(wrappedPainter); | |
return plot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment