public
Last active

Using library JavaPlot for interactive rotating a Gnuplot in a JPanel

  • Download Gist
InteractiveGnuPlot.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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);
 
}
 
}

it doesn't work

Yeah, doesn't work still. I also get an error trying to run JPlot's example though.
This spits out:

** Start of error stream **
gnuplot> set title 'test'
gnuplot>
gnuplot> set term png
Terminal type set to 'png'
Options are 'nocrop font arial 12 size 640,480 '
gnuplot>
gnuplot> _gnuplot_error = 1
gnuplot>
gnuplot> splot sin(x) * y title 'sin(x) * y' ; _gnuplot_error = 0
gnuplot>

** End of error stream **
Exception in thread "main" com.panayotis.gnuplot.GNUPlotException: Error while parsing 'plot' arguments.
    at com.panayotis.gnuplot.GNUPlotExec.plot(GNUPlotExec.java:198)
    at com.panayotis.gnuplot.GNUPlot.plot(GNUPlot.java:267)
    at com.panayotis.gnuplot.swing.JPlot.plot(JPlot.java:64)
    at com.entropix.sounderviewer.InteractiveGnuPlot.createJPanel(InteractiveGnuPlot.java:29)
    at com.entropix.sounderviewer.InteractiveGnuPlot.main(InteractiveGnuPlot.java:17)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.