I need cast the jzy3d canvas to java.awt.component, I want to display the chart in my frame with JCombobox and button but when I want to cast canvas to component, the program was dropped. Thank you for your answers. I have try this and don't help me.
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
org.jzy3d.plot3d.rendering.canvas.VoidCanvas cannot be cast to
java.awt.Component
at cz.bia.Launcher.lambda$setComponents$0(Launcher.java:63)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
Launcher.java:
package cz.bia;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import cz.bia.model.IFunction;
import cz.bia.Yao.*;
import java.awt.*;
public class Launcher extends JFrame {
IFunction[] funs = {new FirstYao(), new SecondYao(), new ThirdYao(), new FourthYao(), new FifthYao(), new SixthYao(), new SeventhYao(), new EighthYao(), new NinthYao(), new TenthYao(),
new EleventhYao(), new TwelfthYao(), new ThirteenthYao(), new FourthteenthYao(), new FifteenthYao(), new SixteenthYao(), new SeventeenthYao(), new EighteenthYao(),
new NineteenthYao(), new TwentiethYao(), new TwentyFirstYao()};
String[] funsToMenu = {"1 Yao", "2 Yao", "3 Yao", "4 Yao", "5 Yao", "6 Yao", "7 Yao", "8 Yao", "9 Yao", "10 Yao", "11 Yao", "12 Yao", "13 Yao", "14 Yao NI",
"15 Yao NI", "16 Yao", "17 Yao", "18 Yao", "19 Yao NI", "20 Yao NI", "21 Yao NI" };
private JPanel contentPane;;
JPanel southPanel;
JPanel leftPanel;
private ChartModel chart;
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Launcher().setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Launcher() throws Exception {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 5));
leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
setComponents();
setContentPane(contentPane);
setContentPane(southPanel);
setContentPane(leftPanel);
}
private void setComponents() {
Button drawIt = new Button("Draw it");
JComboBox functions = new JComboBox(funsToMenu);
functions.setForeground(Color.gray);
functions.setFont(new Font("Arial", Font.PLAIN, 14));
functions.setMaximumRowCount(21);
leftPanel.add(functions);
drawIt.setVisible(true);
chart = new ChartModel(funs[functions.getSelectedIndex()]);
leftPanel.add(drawIt);
drawIt.addActionListener( e -> {
add( (Component) chart.getChart().getCanvas());
});
}
}
ChartMode.java:
package cz.bia;
import java.awt.Component;
import cz.bia.model.IFunction;
import org.jzy3d.chart.Chart;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Builder;
import org.jzy3d.plot3d.builder.Mapper;
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
import org.jzy3d.plot3d.primitives.Shape;
import org.jzy3d.plot3d.rendering.canvas.Quality;
/**
* Created by Jakub on 11.10.2016.
*/
public class ChartModel extends Component {
private IFunction function;
private Chart chart;
private Mapper mapper;
public Shape surface;
public ChartModel(IFunction function){
this.function = function;
init();
}
public void init(){
Range range = new Range(-100,300);
int steps = 50;
surface = Builder.buildOrthonormal(new OrthonormalGrid(range, steps, range, steps), this.getMapper(this.function));
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
surface.setFaceDisplayed(true);
surface.setWireframeDisplayed(true);
surface.setWireframeColor(Color.BLACK);
this.chart = new Chart(Quality.Advanced, "swing");
chart.getScene().getGraph().add(surface);
}
private Mapper getMapper(IFunction function){
Mapper mapper = new Mapper() {
public double f(double x, double y) {
double [] param = {x,y};
return function.calculate(param);
}
};
this.mapper = mapper;
return mapper;
}
public Chart getChart() {
return chart;
}
public void setChart(Chart chart) {
this.chart = chart;
}
}
This working for me. Easy solution, when I give jzy3d canvas to my canvas and put it into panel.
Chart chart = AWTChartComponentFactory.chart(Quality.Advanced, IChartComponentFactory.Toolkit.swing);
ChartScene scene = chart.getScene();
AWTCameraMouseController controller = new AWTCameraMouseController(chart);
Component canvas = (Component) chart.getCanvas();
canvas.addMouseListener(controller);
canvas.addMouseMotionListener(controller);
canvas.addMouseWheelListener(controller);
centerPanel.add(canvas, BorderLayout.CENTER);