Basic embedding of JZY3D into Swing: What am I doing wrong?

1,606 views
Skip to first unread message

Jeremy McCormick

unread,
May 10, 2013, 3:52:23 PM5/10/13
to jz...@googlegroups.com
Hi,

I finally got more of a handle on how the Canvas implementations work.  So now I'm trying to embed into Swing but without much success. 

Can someone tell me what I'm doing wrong?

Here's the code...

----------------------------------------------

package jzy3d.example;

import java.awt.Canvas;
import java.awt.Panel;

import javax.swing.JFrame;
import javax.swing.JPanel;

import junit.framework.TestCase;

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

public class Cloud3DTest extends TestCase {
            
    public static void plot(String canvasType) {
// Create a test chart.
int size = 100000;
Coord3d[] points = new Coord3d[size];
for (int i=0; i<size; i++) {
   points[i] = new Coord3d(
   (float) Math.random() - 0.5f, 
   (float) Math.random() - 0.5f, 
   (float) Math.random() - 0.5f);    
}
Scatter scatter = new Scatter(points);
scatter.setColor(Color.BLUE);
Chart chart = new Chart(canvasType);
chart.getAxeLayout().setMainColor(Color.WHITE);
chart.getView().setBackgroundColor(Color.BLACK);
chart.getScene().add(scatter);
// Embed into Swing.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setOpaque(false);
if (chart.getCanvas() instanceof Canvas) {
   panel.add((Canvas) chart.getCanvas());
} else if (chart.getCanvas() instanceof Panel) {
   panel.add((Panel) chart.getCanvas());
}
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setTitle(canvasType);
frame.setVisible(true);
    }
        
     public static void main(String args[]) {
plot("swing");
plot("newt");
plot("awt");
    }        
}

----------------------------------------------

None of the three canvas types show anything.  In fact, the parent JPanel is quite tiny so it isn't even sized correctly.  

Am I doing something dumb?  Or missing something obvious here?  Probably both...

--Jeremy

Jeremy McCormick

unread,
May 10, 2013, 4:14:14 PM5/10/13
to jz...@googlegroups.com
BTW, it seems that putting the component directly into a frame works okay, at least for the "newt" canvas.

This doesn't work with Swing though.  The chart fails to display and just shows random screen junk.  (kind of odd)

I really need to embed the JZY3D components inside JPanels though, and I can't get that to work.  It just shows grey.  

--Jeremy

Nils Hoffmann

unread,
May 12, 2013, 4:31:12 AM5/12/13
to jz...@googlegroups.com
Hi Jeremy,
within the following demo project

https://github.com/nilshoffmann/netbeans-jogl2/blob/master/src/net/nilshoffmann/jogl2demo/Jzy3DDemoTopComponent.java

I add a Jzy3d chart using the NEWT canvas to a TopComponent, which is essentially a JComponent/JPanel.

Maybe that can give you a hint.

Nils
--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes Jzy3d.
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse jzy3d+un...@googlegroups.com.
Pour plus d'options, visitez le site https://groups.google.com/groups/opt_out .
 
 

Martin Pernollet

unread,
May 13, 2013, 5:34:24 AM5/13/13
to Jzy3d
Hi,
You answered your own question :) You should use Newt.
I made this more clear in the swing canvas javadoc
https://github.com/jzy3d/jzy3d-api/blob/master/src/api/org/jzy3d/plot3d/rendering/canvas/CanvasSwing.java
Martin


2013/5/12 Nils Hoffmann <nils.h...@web.de>

Matthieu Labas

unread,
Jun 9, 2013, 5:47:25 AM6/9/13
to jz...@googlegroups.com
Hi,

The link you gave is broken (the class doesn't exist).

I have tried to instanciate a Chart with "newt" Canva (though I'm new to this and don't know precisely what are the implications, I just want to integrate a 3D graph in a JPanel in my window) but I get a "unhandled canvas!" exception:

Chart chart = new Chart(Quality.Advanced, "newt");

unhandled canvas! org.jzy3d.plot3d.rendering.canvas.CanvasNewt[panel0,0,0,0x0,invalid,layout=java.awt.FlowLayout]

From the sources, I found out that only CanvaSwing, CanvaAWT and OffscreenCanvas are handled in View constructor, though "newt" is valid in Chart constructor.
I tried with "swing" instead of "newt" but I get a blank screen instead of my Spheres.

I'm using jzy3d-0.9. I tried Jeremy's code but I still get the same Exception thrown out.
Am I missing something obvious?

Regards,
Matthieu

Martin Pernollet

unread,
Jun 20, 2013, 10:09:16 AM6/20/13
to Jzy3d
Hi,
Sorry for late reply.
This is a known bug of 0.9. To work with ongoing 0.9.1 and this bug fixed, you should use the maven repository as explained in this readme:
https://github.com/jzy3d/jzy3d-master
Cheers,
Martin


2013/6/9 Matthieu Labas <matthie...@gmail.com>
Reply all
Reply to author
Forward
0 new messages