将 jzy3d.canvas 转换为 awt.component

Posted

技术标签:

【中文标题】将 jzy3d.canvas 转换为 awt.component【英文标题】:Cast jzy3d.canvas to awt.component 【发布时间】:2017-02-21 03:10:29 【问题描述】:

我需要将 jzy3d 画布转换为 java.awt.component,我想使用 JCombobox 和按钮在我的框架中显示图表,但是当我想将画布转换为组件时,程序被删除了。谢谢您的回答。我已经尝试this 并没有帮助我。

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;
    


【问题讨论】:

"I need cast the jzy3d canvas to java.awt.component" -- 如果 jzy3d 没有从 java.awt.Component 扩展,你可以想要任何东西,但它不会发生。 jzy3d 文档是怎么说的?如果这是我的问题,我会从这里开始。 【参考方案1】:

这对我有用。简单的解决方案,当我将 jzy3d 画布放在画布上并将其放入面板时。

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);

【讨论】:

什么叫addMouseListener?我正在编写此代码的类没有该功能。 我的错,这些方法来自组件类 谢谢!另外,我发现在将画布添加到面板后可能需要添加类似 centerPanel.updateUI() 的内容。

以上是关于将 jzy3d.canvas 转换为 awt.component的主要内容,如果未能解决你的问题,请参考以下文章

期末设计(十三周)

期末设计(十三周)

更改 JColorChooser 的预览面板

试设计一个窗口,内含一个按钮。开始运行时,按钮显示“Click Me”字样,当按钮按下时,按钮显示为“Click Me Again”字样,再按一次,则按钮显示“Click Me”字样,依此循环。

将 A 转换为 1 B 转换为 2 ... Z 转换为 26,然后将 AA 转换为 27 AB 转换为 28(Excel 中列引用的列索引)

怎么用ABBYY将PDF转换为JPEG图像