java3dappletjogljzy3d

Embedding Jzy3D in an applet


I'm trying to make an applet that displays a Jzy3D graph that's manipulable. Currently I've managed to get it to display the graph but I can't affect it in anyway; this makes sense as the applet currently isn't looking for any user input.

How am I supposed to pick up user input (the mouse moving, clicking, etc)? I suppose I could try to make a messenger class that picks up applet input and sends it to Jzy3D which then takes the movement and does things with it but that seems very complicated. Looking at the Community Discussion Board, it was suggested to use JOGLAppletLaucher. Is the point of that to bypass this problem? If so, how is it supposed to do so?

EDIT: I believe I understand now the point of JOGLAppletLauncher (and its succesor JNLPAppletLauncher) but the question of how to transmit user input still stands. Any ideas?

my current code:

package daniel.work;

import java.applet.Applet;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import com.sun.opengl.util.JOGLAppletLauncher;

public class SimpleApplet extends JApplet {

  public void start(){
      try{
          SwingUtilities.invokeAndWait(new Runnable(){
              public void run(){

                  add(ScatterCanvas.generateCanvas());

              }
          });
      }
      catch(Exception e){
          System.out.println(e);
      }
  }
}

and

package daniel.work;

import java.awt.Canvas;

import org.jzy3d.chart.Chart;
import org.jzy3d.colors.Color;
import org.jzy3d.demos.AbstractDemo;
import org.jzy3d.demos.Launcher;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.primitives.Scatter;


public class ScatterCanvas extends AbstractDemo{

    public static Canvas generateCanvas(){
            // Create the dot cloud scene and fill with data
            int size = 2000;
            float x;
            float y;
            float z;
            float a;

            Coord3d[] points = new Coord3d[size];
            Color[]   colors = new Color[size];

            for(int i=0; i<size; i++){
                    x = (float)Math.random() - 0.5f;
                    y = (float)Math.random() - 0.5f;
                    z = (float)Math.random() - 0.5f;
                    points[i] = new Coord3d(x, y, z);
                    a = 0.25f + (float)(points[i].distance(Coord3d.ORIGIN)/Math.sqrt(1.3)) / 2;
                    colors[i] = new Color(x, y, z, a);
            }

            Scatter scatter = new Scatter(points, colors);
            chart = new Chart();
            chart.getScene().add(scatter);
            return (Canvas) chart.getCanvas();
    }


    protected static Chart chart;

    @Override
    public void init() throws Exception {
        // TODO Auto-generated method stub

    }

}


Solution

  • Got it! Here's my code. I also changed my generateScatter to now return Chart "return chart" as opposed to ICavnas

    package myjogl;
    
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.SwingUtilities;
    
    import org.jzy3d.chart.Chart;
    import org.jzy3d.chart.controllers.mouse.ChartMouseController;
    import org.jzy3d.chart.controllers.thread.ChartThreadController;
    import org.jzy3d.plot3d.rendering.canvas.ICanvas;
    
    
    @SuppressWarnings("serial")
    public class JzyApplet extends JApplet implements ActionListener{
    
    private Chart chart = null;
    private ICanvas canvas = null;
    
    public void init(){
        try{
            SwingUtilities.invokeAndWait(new Runnable(){
                public void run(){
                    chart = ScatterCanvas.generateCanvas(); //this just returns a chart object with a scatter object
                    canvas = chart.getCanvas();
    
                    add((Component) canvas);                
                }
            });     
        }
        catch(Exception e){
            System.out.println(e);
        }   
    
        ChartMouseController mouse   = new ChartMouseController();
    
        chart.addController(mouse);
    
    
        ChartThreadController thread = new ChartThreadController();
        mouse.addSlaveThreadController(thread);
        chart.addController(thread);
    
        thread.start();
    
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {}
    
    public void zoom(){
        float value = canvas.getView().getBounds().getXmax()-10;
        canvas.getView().getBounds().setXmax(value);
        canvas.getView().shoot();
    }
    
    
    }