Created
July 12, 2011 17:30
-
-
Save timaschew/1078486 to your computer and use it in GitHub Desktop.
Using library JavaPlot for interactive rotating a Gnuplot in a JPanel
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
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseMotionListener; | |
import javax.swing.JFrame; | |
import com.panayotis.gnuplot.JavaPlot; | |
import com.panayotis.gnuplot.swing.JPlot; | |
// include JavaPlot.jar from this site to your classpath | |
// http://gnujavaplot.sourceforge.net/JavaPlot/About.html | |
public class InteractiveGnuPlot { | |
public static void main(String[] args) { | |
createJPanel(); | |
} | |
private static void createJPanel() { | |
JavaPlot javaPlot = new JavaPlot(true); | |
final JPlot plot = new JPlot(javaPlot); | |
final JavaPlot p = plot.getJavaPlot(); | |
// 3d function | |
p.addPlot("sin(x)*y"); | |
plot.plot(); | |
plot.addMouseMotionListener(new MouseMotionListener() { | |
@Override | |
public void mouseMoved(MouseEvent e) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void mouseDragged(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
double rotX = (double) x / plot.getWidth() * 360; | |
double rotY = (double) y / plot.getHeight() * 360; | |
// range check | |
if (rotX < 0) { | |
rotX = 0; | |
} | |
if (rotX > 360) { | |
rotX = 360; | |
} | |
if (rotY < 0) { | |
rotY = 0; | |
} | |
if (rotY > 360) { | |
rotY = 360; | |
} | |
// set view | |
p.set("view", rotY + "," + rotX); | |
// repaint | |
plot.plot(); | |
plot.repaint(); | |
} | |
}); | |
// pack and display frame | |
JFrame f = new JFrame(); | |
f.getContentPane().add(plot); | |
f.pack(); | |
f.setLocationRelativeTo(null); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its work with ein little Patch for JavaPlot.jar
But the Mouse postition ist on start on MouesOver not correct.