From 58cf30d9154c25cc6bf6e4550dc9eac6f8925b7e Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Wed, 20 Aug 2014 14:26:28 +0200 Subject: [PATCH] Added option to JZY3D to do not a continuous rendering and to use the "newt" canvas (which is buggy when detaching another JZY3D plot). --- .../java/ch/psi/plot/jzy3d/MatrixPlot.java | 187 ++++++++++++++---- 1 file changed, 144 insertions(+), 43 deletions(-) diff --git a/ch.psi.plot/src/main/java/ch/psi/plot/jzy3d/MatrixPlot.java b/ch.psi.plot/src/main/java/ch/psi/plot/jzy3d/MatrixPlot.java index 4e7c217..b72aaf6 100644 --- a/ch.psi.plot/src/main/java/ch/psi/plot/jzy3d/MatrixPlot.java +++ b/ch.psi.plot/src/main/java/ch/psi/plot/jzy3d/MatrixPlot.java @@ -15,6 +15,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.awt.event.MouseWheelEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -23,6 +24,7 @@ import java.util.Arrays; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import javax.media.opengl.GLAnimatorControl; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JMenu; @@ -52,6 +54,7 @@ import org.jzy3d.plot3d.primitives.Polygon; import org.jzy3d.plot3d.primitives.ScatterMultiColor; import org.jzy3d.plot3d.primitives.Shape; import org.jzy3d.plot3d.primitives.axes.ContourAxeBox; +import org.jzy3d.plot3d.rendering.canvas.IScreenCanvas; import org.jzy3d.plot3d.rendering.legends.colorbars.AWTColorbarLegend; import org.jzy3d.plot3d.rendering.view.modes.ViewBoundMode; @@ -59,7 +62,7 @@ import org.jzy3d.plot3d.rendering.view.modes.ViewBoundMode; * */ public class MatrixPlot extends MatrixPlotBase { - + JPanel panel; private Chart chart; private Mapper mapper; @@ -67,23 +70,42 @@ public class MatrixPlot extends MatrixPlotBase { private MatrixPlotSeries series; private double[][] data; + enum CanvasType { + + awt, + newt + }; + + static final CanvasType canvasType = CanvasType.awt; + public MatrixPlot() { super(); panel = new MonitoredPanel() { + @Override protected void onShown() { + if ((chart == null) && (series != null)) { + createGraph(); + } checkBounds(true); } + + protected void onHidden() { + if (chart != null) { + chart.dispose(); + chart = null; + } + } }; panel.setLayout(new BorderLayout()); //panel.setLayout(new GridLayout(1,1)); - setColormap(Colormap.TEMPERATURE); + setColormap(Colormap.TEMPERATURE); if (getHardwareAccelerated() != Settings.getInstance().isHardwareAccelerated()) { Settings.getInstance().setHardwareAccelerated(getHardwareAccelerated()); } panel.setPreferredSize(new Dimension(640, 480)); setupPopupMenu(); } - + @Override protected Object onAddedSeries(final MatrixPlotSeries s) { if (!SwingUtilities.isEventDispatchThread()) { @@ -246,6 +268,19 @@ public class MatrixPlot extends MatrixPlotBase { return showAxis; } + boolean continuousRendering = false; + + public void setContinuousRendering(boolean value) { + if (value != continuousRendering) { + continuousRendering = value; + createGraph(); + } + } + + public boolean getContinuousRendering() { + return continuousRendering; + } + public enum Colormap { GRAYSCALE, @@ -591,11 +626,12 @@ public class MatrixPlot extends MatrixPlotBase { */ synchronized (chartLock) { + Chart formerChart = chart; if (series != null) { if ((getContour() == Contour.NONE) || (getContour() == Contour.CONTOUR_3D)) { - chart = org.jzy3d.chart.factories.ContourChartComponentFactory.chart(getQuality().toJzy3dQuality(), "awt"); + chart = org.jzy3d.chart.factories.ContourChartComponentFactory.chart(getQuality().toJzy3dQuality(), canvasType.toString()); } else { - chart = new org.jzy3d.chart.factories.ContourChartComponentFactory().newChart(getQuality().toJzy3dQuality(), "awt"); + chart = new org.jzy3d.chart.factories.ContourChartComponentFactory().newChart(getQuality().toJzy3dQuality(), canvasType.toString()); } if (rangeY == null) { @@ -607,19 +643,30 @@ public class MatrixPlot extends MatrixPlotBase { mapper = new Mapper() { @Override public double f(double x, double y) { - if ((data == null)||(data.length==0)) { + if ((data == null) || (data.length == 0)) { return Double.NaN; } - int indexY=(int) Math.round((y - rangeY.getMin()) / ((rangeY.getMax() - rangeY.getMin()) / (data.length - 1))); - int indexX=(int) Math.round((x - rangeX.getMin()) / ((rangeX.getMax() - rangeX.getMin()) / (data[0].length - 1))); - double ret = data[indexY][indexX]; - if (Double.isInfinite(ret)) - ret=Double.NaN; + int indexY = (int) Math.round((y - rangeY.getMin()) / ((rangeY.getMax() - rangeY.getMin()) / (data.length - 1))); + int indexX = (int) Math.round((x - rangeX.getMin()) / ((rangeX.getMax() - rangeX.getMin()) / (data[0].length - 1))); + double ret = data[indexY][indexX]; + if (Double.isInfinite(ret)) { + ret = Double.NaN; + } return ret; - } }; + + GLAnimatorControl ac = null; Component canvas = (Component) chart.getCanvas(); + if (canvas instanceof IScreenCanvas) { + ac = ((IScreenCanvas) canvas).getAnimator(); + } + final GLAnimatorControl animatorControl = ac; + + if (!getContinuousRendering() && (animatorControl != null)) { + animatorControl.pause(); + } + panel.removeAll(); panel.add(canvas); @@ -633,28 +680,83 @@ public class MatrixPlot extends MatrixPlotBase { chart.getAxeLayout().setYAxeLabel(getAxis(AxisId.Y).getLabel()); chart.getAxeLayout().setZAxeLabel(getAxis(AxisId.Z).getLabel()); - chart.getCanvas().addMouseController(new MouseAdapter() { - @Override - public void mouseReleased(MouseEvent e) { - checkPopup(e); - } + Object listener = (canvasType == CanvasType.newt) + ? new com.jogamp.newt.event.MouseAdapter() { + @Override + public void mousePressed(com.jogamp.newt.event.MouseEvent e) { + if (!getContinuousRendering() && (animatorControl != null)) { + animatorControl.resume(); + } + } - @Override - public void mousePressed(MouseEvent e) { - checkPopup(e); - } + public void mouseWheelMoved(com.jogamp.newt.event.MouseEvent me) { + if (!getContinuousRendering() && (animatorControl != null)) { + if (animatorControl.isPaused()) { + ((IScreenCanvas) canvas).display(); + } + } + } - private void checkPopup(MouseEvent e) { - if (e.isPopupTrigger()) { - menuPopup.show(e.getComponent(), e.getX(), e.getY()); + @Override + public void mouseReleased(com.jogamp.newt.event.MouseEvent e) { + if (e.getButton() == com.jogamp.newt.event.MouseEvent.BUTTON3) { + menuPopup.show(panel, e.getX(), e.getY()); + } + + if (!getContinuousRendering() && (animatorControl != null)) { + if ((e.getClickCount() != 2) && (e.getButtonsDown().length <= 1)) { + animatorControl.pause(); + ((IScreenCanvas) canvas).display(); + } + } + } } - } - }); + : new MouseAdapter() { + + @Override + public void mouseReleased(MouseEvent e) { + checkPopup(e); + if (!getContinuousRendering() && (animatorControl != null)) { + if ((e.getClickCount() != 2) ) { + animatorControl.pause(); + ((IScreenCanvas) canvas).display(); + } + } + } + + public void mouseWheelMoved(MouseWheelEvent e){ + if (!getContinuousRendering() && (animatorControl != null)) { + if (animatorControl.isPaused()) { + ((IScreenCanvas) canvas).display(); + } + } + } + + @Override + public void mousePressed(MouseEvent e) { + checkPopup(e); + if (!getContinuousRendering() && (animatorControl != null)) { + animatorControl.resume(); + } + + } + + private void checkPopup(MouseEvent e) { + if (e.isPopupTrigger()) { + menuPopup.show(e.getComponent(), e.getX(), e.getY()); + } + } + }; + + chart.getCanvas().addMouseController(listener); + + updateGraph(true); + } + if (formerChart != null) { + formerChart.dispose(); } - updateGraph(true); } - } private void updateGraph(final boolean newSeries) { @@ -835,29 +937,28 @@ public class MatrixPlot extends MatrixPlotBase { menuPopup.add(item); } } - + @Override public BufferedImage getSnapshot() { - - File temp=null; - try{ + + File temp = null; + try { temp = File.createTempFile("snapshot", ".bmp"); chart.screenshot(temp); return IO.readImageFromFile(temp.getAbsolutePath()); - } - catch (Exception ex){ + } catch (Exception ex) { return null; - } - finally{ - if (temp!=null) - temp.delete(); + } finally { + if (temp != null) { + temp.delete(); + } } } - + public void saveSnapshot(String filename, String format) throws IOException { - if (chart!=null){ + if (chart != null) { chart.screenshot(new File(filename)); - } - } - + } + } + }