Swing - 在一个窗口中绘制树和Jfreechart

时间:2015-06-30 20:25:26

标签: java swing tree jpanel

我有一个我正在编写的程序,我需要它来绘制树并绘制其点。

问题:

我的油漆似乎在我的图表/情节下面,即使它们在两个面板中。我能做什么才能使它们不重叠。甚至可能是滚动条?

这是一个示例左边是图形重叠,右边是显示下面的内容。

Left is graph overlapping tree, right shows what is underneath

加成: 什么是绘制树的更好方法,一些节点重叠。

代码:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ui extends JFrame {
    JPanel tp,chartPanel;
    Tree t;
    Node root;
    Parser p;
    double x = 0, y = 0;
    int generation = 0;
    int timeSeconds = 0;
    double fitness = Double.MIN_VALUE;
    int sizeX = 1000;
    int sizeY = 1000;
    int depth = 10;
    public ui() {
        super("Simple Tree");
        setSize(sizeX, sizeY);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        tp = new TPanel();
        tp.setLayout(new BorderLayout());
        chartPanel = new TPanel();
        chartPanel.setLayout(new BorderLayout());
        //JButton genButton = new JButton("Generate Tree");
        //tp.add(genButton);

        add(tp);
        add(chartPanel);

    }
    public ChartPanel GraphTree(Node n){
        XYSeriesCollection dataSet = new XYSeriesCollection();
        XYSeries series = new XYSeries("Data");
        XYSeries actual = new XYSeries("Actual");
        Parser p = new Parser();
        for(double index = -10; index < 10; index+=0.1){
            series.add(index, p.TreeOutputAtPoint(n,index));
        }
        dataSet.addSeries(series);
        JFreeChart jc = ChartFactory.createXYLineChart("Tree Graph", "Input", "Output",dataSet,
            PlotOrientation.VERTICAL, true, true, false);
        ChartPanel cp = new ChartPanel(jc);
        cp.setPreferredSize(new java.awt.Dimension(200, 500));
        cp.setVisible(true);

        chartPanel.add(cp, BorderLayout.SOUTH);
        chartPanel.validate();
        chartPanel.removeAll();
        chartPanel.revalidate();
        //tp.repaint();
        return cp;
    }
public void drawTree(Node r, int gen, double fit, int timeSec){
    root = r;
    p = new Parser();
    generation = gen;
    timeSeconds = timeSec;
    fitness = fit;
}


    class TPanel extends JPanel {
        public TPanel() {
            setPreferredSize(new Dimension(sizeX, sizeY));// fill whole frame
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(root != null){
                Eval(root, g, sizeX/2, 100);
                g.drawString("Generation: " + generation, 15, 100);
                g.drawString("Time it took: " + timeSeconds + " seconds", 15, 120);
                g.drawString("Fitness: " + fitness, 15, 140);
            }

        }
    }
    public static void Eval(Node input, Graphics g, int x, int y) {
        g.drawString(input.data, x, y);
        if (!input.leaf) {
                g.drawLine(x, y + 5, x - 20, y + 20);
                Eval(input.left, g, x - 30, y + 30);
            if (!Utility.isUnary(input.data)) {
                g.drawLine(x, y + 5, x + 20, y + 20);
                Eval(input.right, g, x + 30, y + 30);
            }
        }

    }

}

https://github.com/kevkid/GeneticAlgorithmTest/blob/master/GeneticAlgorithmTest/src/ui.java

任何帮助都将不胜感激。

0 个答案:

没有答案