George Tomlinson
George Tomlinson

Reputation: 1911

Almost identical programs. One works, one doesn't

I have 2 virtually identical programs. The only difference is the name of the class and hence one line of the program which refers to that name (to construct an instance of the class). The first runs fine. The line that's causing the problem (although it's in both programs and doesn't cause a problem in the first), which netbeans is reporting isn't a statement is
panel.add(chartPanel); Deatils of the error:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - not a statement
at splinepanel.SplinePanel.createContentPane(SplinePanel.java:139)
at splinepanel.SplinePanel.createAndShowGUI(SplinePanel.java:157)
at splinepanel.SplinePanel.access$000(SplinePanel.java:33)
at splinepanel.SplinePanel$1.run(SplinePanel.java:170)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Can anyone help please? The first program (which works) is called PanelExmpale.java. The second program which doesn't work is as follows:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package splinepanel;

/*

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
* 
* */
import javax.swing.*;
import java.awt.*;       // Using AWT containers and components
import java.awt.event.*; // Using AWT events and listener interfaces
import javax.swing.*;    // Using Swing components and containers
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.ChartPanel;
import org.jfree.data.general.Series;
import org.jfree.util.PublicCloneable;
import org.jfree.chart.renderer.xy.XYSplineRenderer;

/**
 *
 * @author User
 */
public class SplinePanel{

    public JPanel createContentPane(){
        // This is where we'll put all our widgets
        // in the next tutorials!
        JPanel panel = new JPanel();

        /*

        SplineFactory s = new SplineFactory(); 

        double[] c = new double[12];
    c[0]  =   0.0;  // x0
    c[1]  =   0.0;  // y0
    c[2]  =   0.0;  // z0      

    c[3]  =   1.0;  // x1
    c[4]  =   1.0;  // y1
    c[5]  =   0.0;  // z1      

    c[6]  =   2.0;  // x2
    c[7]  =  -1.0;  // y2
    c[8]  =   0.0;  // z2      

    c[9]  =  10.0;  // x3
    c[10] =   0.0;  // y3
    c[11] =   0.0;  // z3      

    double[] spline1 = SplineFactory.createBezier (c,     20);
    double[] spline2 = SplineFactory.createCubic (c,      20);
    double[] spline3 = SplineFactory.createCatmullRom (c, 20); 

    /*

    System.out.println ("-- Bezier");
    for (int i = 0; i < spline1.length; i+=3)
      System.out.println (spline1[i] + "," + spline1[i+1] + "," + spline1[i+2]);

    System.out.println ("-- Cubic");
    for (int i = 0; i < spline2.length; i+=3)
      System.out.println (spline2[i] + "," + spline2[i+1] + "," + spline2[i+2]);

    System.out.println ("-- Catmull-Rom");
    for (int i = 0; i < spline3.length; i+=3)
      System.out.println (spline3[i] + "," + spline3[i+1] + "," + spline3[i+2]);

    */



        panel.setLayout(new BorderLayout());

        XYSeries series = new XYSeries("MyGraph");

        /*

        System.out.println ("-- Bezier");
        for (int i = 0; i < spline1.length; i+=3)
         System.out.println (spline1[i] + "," + spline1[i+1] + "," + spline1[i+2]);
        // series.add(spline1[i])

        System.out.println ("-- Cubic");
        for (int i = 0; i < spline2.length; i+=3)
         System.out.println (spline2[i] + "," + spline2[i+1] + "," + spline2[i+2]);

        System.out.println ("-- Catmull-Rom");
        for (int i = 0; i < spline3.length; i+=3)
         System.out.println (spline3[i] + "," + spline3[i+1] + "," + spline3[i+2]); 

         */

        /* 
        series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);
        */

         series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        JFreeChart chart = ChartFactory.createXYLineChart(
                "XY Chart",
                "x-axis",
                "y-axis",
                dataset, 
                PlotOrientation.VERTICAL,
                true,
                true,
                false
                );
        ChartPanel chartPanel = new ChartPanel(chart);

        //chart.getXYPlot().setRenderer(new XYSplineRenderer());





        panel.add(chartPanel);
        //panel.setSize(800, 500);

        //content panes must be opaque



        panel.setOpaque(true); 
        return panel;  
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");

        //Create and set up the content pane.
        SplinePanel demo = new SplinePanel();
        frame.setContentPane(demo.createContentPane());

        // The other bits and pieces that make our program a bit more stable.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 650);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Running a clean and build leads to 100 errors, the first of which is:

error: duplicate class: org.jfree.experimental.chart.demo.CombinedCategoryPlotDemo1 
public class CombinedCategoryPlotDemo1 extends ApplicationFrame { 

Upvotes: 1

Views: 243

Answers (1)

trashgod
trashgod

Reputation: 205765

As noted in comments and shown below, your code runs fine. It looks like you've got more than needed on the classpath in your build environment. On the command line, as shown here, you typically need just jfreechart and jcommon. In NetBeans, check the Library Manager, seen here and below.

NetBeans Ant Library Manager showing JFreeChart:

Ant Library Manager

Image, as run:

image

Code, as run:

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class SplinePanel {

    public JPanel createContentPane() {
        JPanel panel = new JPanel(new GridLayout());
        XYSeries series = new XYSeries("MyGraph");
        series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Chart",
            "x-axis",
            "y-axis",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);
        ChartPanel chartPanel = new ChartPanel(chart);
        panel.add(chartPanel);
        panel.setOpaque(true);
        return panel;
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");
        SplinePanel demo = new SplinePanel();
        frame.setContentPane(demo.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Upvotes: 4

Related Questions