我正在尝试创建一个显示可操作的Jzy3D图形的applet。目前我已经设法让它显示图表,但无论如何我不能影响它;这是有道理的,因为小程序当前不寻找任何用户输入。
我应该如何获取用户输入(鼠标移动,点击等)?我想我可以尝试创建一个信使类来获取applet输入并将其发送给Jzy3D,然后Jzy3D接受运动并使用它进行操作,但这看起来非常复杂。 查看Community Discussion Board,建议使用JOGLAppletLaucher。是否可以绕过这个问题?如果是这样,它应该怎么做?
编辑:我相信我现在理解JOGLAppletLauncher(及其成功者JNLPAppletLauncher)的观点,但是如何传输用户输入的问题仍然存在。有什么想法吗?
我目前的代码:
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);
}
}
}
和
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
}
}
答案 0 :(得分:0)
知道了!这是我的代码。我还将我的generateScatter改为现在返回Chart“返回图表”而不是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();
}
}