Problem for add Jzy3d to JPanel

1,199 views
Skip to first unread message

kx1...@gmail.com

unread,
Oct 8, 2013, 9:28:52 PM10/8/13
to jz...@googlegroups.com
I looked at the previous post and resolve, but the link does not work.

My code is:

package org.jzy3d.demos.scatter;

import java.awt.BorderLayout;
import java.awt.Panel;

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

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

public class EmbedNewtTest {
               
    public static void main(String args[]) {

// 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("newt");
Chart chart = new Chart("newt");
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();



JPanel panel3d = new JPanel();
panel3d.setLayout(new java.awt.BorderLayout());
panel3d.add((JComponent)chart.getCanvas());


frame.setLayout(new BorderLayout());
//panel.add((Panel) chart.getCanvas());       
frame.setContentPane(panel3d);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setTitle("Embed Newt Test");
frame.setVisible(true);

    }            
}


My error is:


Exception in thread "main" java.lang.RuntimeException: unhandled canvas! org.jzy3d.plot3d.rendering.canvas.CanvasNewt[panel0,0,0,0x0,invalid,layout=java.awt.FlowLayout]
    at org.jzy3d.plot3d.rendering.view.View.<init>(View.java:103)
    at org.jzy3d.chart.ChartView.<init>(ChartView.java:31)
    at org.jzy3d.factories.ViewFactory.getInstance(ViewFactory.java:11)
    at org.jzy3d.chart.ChartScene.initializeChartView(ChartScene.java:51)
    at org.jzy3d.chart.ChartScene.newView(ChartScene.java:40)
    at org.jzy3d.plot3d.rendering.canvas.CanvasNewt.<init>(CanvasNewt.java:27)
    at org.jzy3d.chart.Chart.initializeCanvas(Chart.java:83)
    at org.jzy3d.chart.Chart.<init>(Chart.java:70)
    at org.jzy3d.chart.Chart.<init>(Chart.java:59)
    at org.jzy3d.chart.Chart.<init>(Chart.java:55)
    at org.jzy3d.demos.scatter.EmbedNewtTest.main(EmbedNewtTest.java:31)

I really appreciate it.

When I change "newt" to "swing", I will get a window, but it does not show the scatter plot.

Thanks again.

Best,
Nina


Matthieu Labas

unread,
Oct 12, 2013, 2:23:52 AM10/12/13
to jz...@googlegroups.com
Hi Nina,

The problem is that, as of version 0.9, jzy3d doesn't seem to handle "newt" canvas.
I had the same problem and I found out on another site that it is not a jzy3d problem but of ... Swing component size!

Make the following changes in your code:

        Chart chart = new Chart("swing"); // Use "swing" canvas
       
...

       
JPanel panel3d = new JPanel();

       
panel3d.setPreferredSize(new Dimension(800, 600)); // Set default size for panel3d
        panel3d
.setLayout(new java.awt.BorderLayout());
        panel3d
.add((JComponent)chart.getCanvas(), BorderLayout.CENTER); // Add chart in CENTER so that it will be resized automatically
       
...
        frame
.add(panel3d, BorderLayout.CENTER); // Add the panel3d in CENTER so that it will be resized automatically
        frame
.pack(); // The frame will take the size of the panel3d (the only component it has)
        frame
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // EXIT_ON_CLOSE is better that DISPOSE_ON_CLOSE as it will exit the application. DISPOSE_ON_CLOSE will let the JVM run...


It's working fine on my computer like that.

Please tell us if it working for you too!

Regards,
Matthieu

kx1...@gmail.com

unread,
Oct 12, 2013, 11:34:35 PM10/12/13
to jz...@googlegroups.com
Hey Matthieu,


Thanks for your quick reply!


It works, I am so glad.;)


But it seems have another problem.


When we used Chart before, we can use mouse to change the direction, scroll the mouse up and down to see the change of the points in the Chart. 

But when we changed Chart to JPanel the mouse does not work.;( 

Maybe we need to add the mouse action?

I really appreciate it.


Best,

Nina

Message has been deleted

Matthieu Labas

unread,
Oct 13, 2013, 11:25:08 AM10/13/13
to jz...@googlegroups.com
Hey I finally was able to find the source of my tests! I had that part working in a JPanel. You have to add a CameraMouseController to the Chart: chart.addController(new CameraMouseController());

The following code works with "canvasType" set to "swing":

    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 = (canvasType != null ? new Chart(canvasType) : new Chart());
        chart
.getAxeLayout().setMainColor(Color.WHITE);
        chart
.getView().setBackgroundColor(Color.BLACK);
        chart
.getScene().add(scatter);
       
chart.addController(new CameraMouseController());

       
       
// Embed into Swing.
       
JFrame frame = new JFrame();

        frame
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame
.getContentPane().setLayout(new BorderLayout());
       
JPanel panel = new JPanel();
        panel
.setLayout(new BorderLayout());
        panel
.setOpaque(false);
        panel
.setPreferredSize(new Dimension(300,300));
        panel
.setBorder(new MatteBorder(5, 5, 5, 5, java.awt.Color.BLACK));
       
if (chart.getCanvas() instanceof Canvas) {
           
System.out.println("Canvas "+chart.getCanvas());
            panel
.add((Canvas)chart.getCanvas());
       
} else if (chart.getCanvas() instanceof Panel) {
           
System.out.println("Panel "+chart.getCanvas());
            panel
.add((Panel) chart.getCanvas());
       
} else {
           
System.err.println("Other "+chart.getCanvas());
            panel
.add((JPanel)chart.getCanvas());
       
}
        frame
.getContentPane().add(panel, BorderLayout.CENTER);
        frame
.pack();
        frame
.setTitle(canvasType);
        frame
.setVisible(true);
   
}


Regards,
Matthieu

kx1...@gmail.com

unread,
Oct 14, 2013, 1:08:43 PM10/14/13
to jz...@googlegroups.com
Hey Matthieu,

Thanks so much for your help and time!

Finally, I put all of them to CardLayout.;) Because I have several JPanel. It works good.

I wander if I can add the mouse action, such as when I move the mouse to one point, it will display the coordinate. Like the attachment.
I think I might consider about MouseListener and mouseEntered/mouseExited probably. Do you have some hints in this part, please?

Thanks again. Have a nice day!

Best,
Nina
sample.PNG

Matthieu Labas

unread,
Oct 17, 2013, 5:55:40 AM10/17/13
to jz...@googlegroups.com
Hi Nina,

Unfortunately I'm not versed enough on jzy3d to help you with that as I've just covered the tutorials to display some graph and assess its use. But I think it's possible: see this thread. You'd probably want to have a look at the MousePickingController.

Regards,
Matthieu

kx1...@gmail.com

unread,
Oct 18, 2013, 4:38:16 PM10/18/13
to jz...@googlegroups.com
Hey Matthieu,

I will look at it. Waiting for my good new.;)

Have a nice weekend!

Best,
Nina
Reply all
Reply to author
Forward
0 new messages