2

I have done about 4 hours worth of internet hunting and have hit my limit. Hopefully you folks can help.

I have a project that has a package that contains some source code. I also have my main source folder which contains this package as well.

In the package directory I have source files, not class files, defined. I used this directory as a source folder, and that works. One of my source files in my main source folder instantiates an object defined in this source file. I need to pass this object a reference to the calling object to be able to callback. So, for example:

package edu.uci.ics.jung.visualization3d;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Map;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.Group;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.OrientedShape3D;
import javax.media.j3d.Text3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import org.apache.commons.collections15.BidiMap;
import org.apache.commons.collections15.bidimap.DualHashBidiMap;

import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.behaviors.PickingCallback;
import com.sun.j3d.utils.universe.SimpleUniverse;

import edu.uci.ics.jung.algorithms.layout.util.VisRunner;
import edu.uci.ics.jung.algorithms.layout3d.Layout;
import edu.uci.ics.jung.algorithms.util.IterativeContext;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Context;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.visualization.picking.MultiPickedState;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization3d.control.MouseRotate;
import edu.uci.ics.jung.visualization3d.control.MouseTranslate;
import edu.uci.ics.jung.visualization3d.control.PickSphereBehavior;
import edu.uci.ics.jung.visualization3d.control.PickTranslateBehavior;
import edu.uci.ics.jung.visualization3d.layout.LayoutEventBroadcaster;



public class VisualizationViewer<V,E> extends JPanel {

    BranchGroup objRoot;
    TransformGroup objTrans;
    GraphBuilder to;
//  Appearance vertexLook;
//  Appearance edgeLook;
    Appearance grayLook;
    /**
     * a listener used to cause pick events to result in
     * repaints, even if they come from another view
     */
    protected ItemListener pickEventListener;
    /**
     * holds the state of which vertices of the graph are
     * currently 'picked'
     */
    protected PickedState<V> pickedVertexState;

    /**
     * holds the state of which edges of the graph are
     * currently 'picked'
     */
    protected PickedState<E> pickedEdgeState;

    protected RenderContext<V,E> renderContext = new PluggableRenderContext<V,E>();

    BidiMap<V,VertexGroup> vertexMap = new DualHashBidiMap<V,VertexGroup>();
    Map<E,EdgeGroup> edgeMap = new HashMap<E,EdgeGroup>();
    Graph<V,E> graph;
    Layout<V,E> layout;

    public VisualizationViewer() {
//      controls = createControls();
        setLayout(new BorderLayout());

        renderContext.setPickedVertexState(new MultiPickedState<V>());
        renderContext.setPickedEdgeState(new MultiPickedState<E>());
        GraphicsConfiguration config = 
            SimpleUniverse.getPreferredConfiguration();
        final Canvas3D c = new Canvas3D(config);
        add(c, BorderLayout.CENTER);
        setPickedVertexState(new MultiPickedState<V>());
        setPickedEdgeState(new MultiPickedState<E>());

        // Create a SpringGraph scene and attach it to the virtual universe
        BranchGroup scene = createSceneGraph(c);
        SimpleUniverse u = new SimpleUniverse(c);
        u.getViewer().getView().setUserHeadToVworldEnable(true);    

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();

        c.getView().setBackClipDistance(20.0);
        //c.getView().setFrontClipDistance(10.0);
        u.addBranchGraph(scene);
    }

    public Layout<V,E> getGraphLayout() {
        return layout;
    }


    public BranchGroup createSceneGraph(final Canvas3D canvas) {

        objRoot = new BranchGroup();
        objRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
        objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);

        TransformGroup objScale = new TransformGroup();
        Transform3D t3d = new Transform3D();
//      t3d.setScale(0.05);
        objScale.setTransform(t3d);
        objRoot.addChild(objScale);

        Transform3D tt = new Transform3D();
        tt.setScale(.05);
        tt.setTranslation(new Vector3f(0, 0, -30.f));
        objTrans = new TransformGroup(tt);
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ );
        objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
        objScale.addChild(objTrans);
//      objRoot.addChild(objTrans);

        // Create Colors, Materials,  and Appearances.
        Appearance look = new Appearance();
        Color3f objColor = new Color3f(0.7f, 0.7f, 0.7f);
        Color3f black = new Color3f(0.f, 0.f, 0.f);
        Color3f white = new Color3f(1.0f, 1.0f, 0.6f);
        Color3f gray  = new Color3f(.2f, .2f, .2f);
        Color3f red = new Color3f(1.0f, 0, 0);
        Color3f yellow = new Color3f(1,1,0);

        Material objMaterial = new Material(objColor, black,
                objColor, white, 100.0f);
        Material blackMaterial = new Material(objColor, black,
                black, objColor, 10.0f);
        Material whiteMaterial = new Material(white, white,
                white, white, 100.0f);
        Material grayMaterial = new Material(gray, black,
                gray, gray, 100.0f);

        Material redMaterial = new Material(red, black,
                red, red, 100.0f);
            Material yellowMaterial = new Material(yellow, black, 
                yellow, yellow, 100);

        look.setMaterial(new Material(objColor, black,
                objColor, white, 100.0f));
        Appearance blackLook = new Appearance();
        blackLook.setMaterial(blackMaterial);

        Appearance whiteLook = new Appearance();
        whiteLook.setMaterial(whiteMaterial);

        Appearance grayLook = new Appearance();
        grayLook.setMaterial(grayMaterial);
        grayLook.setCapability(Appearance.ALLOW_MATERIAL_READ);
        grayLook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

        final Appearance redLook = new Appearance();
        redLook.setMaterial(redMaterial);
//      vertexLook = redLook;

        Appearance objLook = new Appearance();
        objLook.setMaterial(objMaterial);
        grayLook = objLook;
        final Appearance yellowLook = new Appearance();
        yellowLook.setMaterial(yellowMaterial);
        Bounds bounds =
            new BoundingSphere(new Point3d(),
                    300);

        MouseRotate behavior1 = new MouseRotate();
        behavior1.setTransformGroup(objTrans);
        objTrans.addChild(behavior1);
        behavior1.setSchedulingBounds(bounds);

        MouseWheelZoom behavior2 = new MouseWheelZoom();
        behavior2.setTransformGroup(objTrans);
//      behavior2.setFactor(10);
        objTrans.addChild(behavior2);
        behavior2.setSchedulingBounds(bounds);

        MouseTranslate behavior3 = new MouseTranslate();
        behavior3.setTransformGroup(objTrans);
        objTrans.addChild(behavior3);
        behavior3.setSchedulingBounds(bounds);

        PickTranslateBehavior ptb = new PickTranslateBehavior(objRoot,canvas,bounds,PickTool.GEOMETRY);
        ptb.setSchedulingBounds(bounds);
//      objTrans.addChild(ptb);
        ptb.setupCallback(new PickingCallback() {

            public void transformChanged(int type, TransformGroup tg) {
                if(tg == null) return;
                Transform3D t3d = new Transform3D();
                tg.getTransform(t3d);
//              System.err.println(tg+" transformChanged \n"+t3d);
                Point3f p1 = new Point3f();
                V v = vertexMap.getKey(tg);
//              Transform3D lvw = new Transform3D();
//              tg.getLocalToVworld(lvw);
//              System.err.println("lvw = \n"+lvw);
//              lvw.invert();
//              System.err.println("invert lvw = \n"+lvw);
                Point3f p0 = layout.transform(v);
//              Transform3D vwip = new Transform3D();
//              canvas.getVworldToImagePlate(vwip);
//              System.err.println("vwip=\n"+vwip);
//              t3d.mul(lvw);
                t3d.transform(p1);
//              scale.transform(p1);
                System.err.println("change location for vertex "+v+", transformGroup "+tg+" from "+p0+" to "+p1);
//              p1.set(p1.getX()*2,p1.getY()*2,p1.getZ()*2);
//              layout.setLocation(v, p1);

            }});

        PickSphereBehavior psb = new PickSphereBehavior(objRoot,canvas,bounds);

        PickVertexBehavior pvb = new PickVertexBehavior(objRoot,canvas,bounds,renderContext.getPickedVertexState());
        objTrans.addChild(pvb);
        pvb.addChangeListener(new ChangeListener() 
        {

            public void stateChanged(ChangeEvent e) 
            {
                for(V v : graph.getVertices()) 
                {
                    VertexGroup<V> vg = vertexMap.get(v);
                    Appearance look = redLook;
                    if(renderContext.getPickedVertexState().isPicked(v)) 
                    {
                        look = yellowLook;
                    }
                    Node node = vg.getShape();

                    if(node instanceof Primitive) 
                    {
                        ((Primitive)node).setAppearance(look);
                    }
                }

                }
        });

        //Shine it with two colored lights.
        Color3f lColor1 = new Color3f(.5f, .5f, .5f);
        Color3f lColor2 = new Color3f(1.0f, 1.0f, 1.0f);
        Vector3f lDir2  = new Vector3f(-1.0f, 0.0f, -1.0f);
        DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
        AmbientLight ambient = new AmbientLight(lColor1);
        lgt2.setInfluencingBounds(bounds);
        ambient.setInfluencingBounds(bounds);
        objRoot.addChild(lgt2);
        objRoot.addChild(ambient);

        // Let Java 3D perform optimizations on this scene graph.
        objRoot.compile();

//      VisRunner runner = new VisRunner((IterativeContext)elayout);
//      runner.relax();

        return objRoot;
    }

    public void setGraphLayout(Layout<V,E> inLayout) {

//      this.layout = inLayout;
        this.graph = inLayout.getGraph();
        BranchGroup branch = new BranchGroup();
        LayoutEventBroadcaster<V,E> elayout =
            new LayoutEventBroadcaster<V,E>(inLayout);
        this.layout = elayout;
        for(V v : graph.getVertices()) {
            VertexGroup<V> vg = new VertexGroup<V>(v, renderContext.getVertexShapeTransformer().transform(v));
            vg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            vg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            vertexMap.put(v, vg);
            branch.addChild(vg);
            String label = renderContext.getVertexStringer().transform(v);
            if(label != null) {
                String fontName = "Serif";
                Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
                        new FontExtrusion());
                Text3D txt = new Text3D(f3d, label, 
                        new Point3f(2f,2f,0));
                OrientedShape3D textShape = new OrientedShape3D();
                textShape.setGeometry(txt);
                textShape.setAppearance(grayLook);
//              textShape.setAlignmentAxis( 0.0f, 1.0f, 0.0f);
                textShape.setAlignmentMode(OrientedShape3D.ROTATE_ABOUT_POINT);
                textShape.setRotationPoint(new Point3f());
//              objScale.addChild( textShape );
//              BranchGroup bg = new BranchGroup();
//              bg.addChild(textShape);
//              branch.addChild(bg);



//              Text2D text = new Text2D(label+" more text here", new Color3f(0,0,0),"Serif",50,Font.BOLD);
                Transform3D tt = new Transform3D();
//              tt.setTranslation(new Vector3f(100,100,100));
                tt.setScale(5);
                TransformGroup tg = new TransformGroup(tt);
//              textShape.setGeometry(text);
                tg.addChild(textShape);
                BranchGroup bg = new BranchGroup();
                bg.addChild(tg);
//              branch.addChild(bg);
                vg.getLabelNode().addChild(bg);

            }

        }
        for(E edge : graph.getEdges()) {
            EdgeGroup<E> eg = 
                new EdgeGroup<E>(edge, renderContext.getEdgeShapeTransformer().transform(Context.<Graph<V,E>,E>getInstance(graph, edge)));
            eg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            eg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            edgeMap.put(edge, eg);
            branch.addChild(eg);
        }

//      System.err.println("branch is "+branch);
//      for(int i=0; i<branch.numChildren(); i++) {
//          System.err.println("branch child ["+i+"] is "+branch.getChild(i));
//      }

        objTrans.addChild(branch);
        elayout.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                for(V v : vertexMap.keySet()) {
                    Point3f p = VisualizationViewer.this.layout.transform(v);
                    Vector3f pv = new Vector3f(p.getX(), p.getY(), p.getZ());
                    Transform3D tx = new Transform3D();
                    tx.setTranslation(pv);
                    vertexMap.get(v).setTransform(tx);
                }

                for(E edge : graph.getEdges()) {
                    Pair<V> endpoints = graph.getEndpoints(edge);
                    V start = endpoints.getFirst();
                    V end = endpoints.getSecond();
                    EdgeGroup eg = edgeMap.get(edge);
                    eg.setEndpoints(layout.transform(start), layout.transform(end));
                }
            }});

        elayout.setSize(new BoundingSphere(new Point3d(), 200));
        elayout.initialize();
        VisRunner runner = new VisRunner((IterativeContext)elayout);
        runner.relax();
        try
        {
            Thread.sleep(1000);
        } catch (InterruptedException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        runner.stop();

//      for(int i=0; i<objTrans.numChildren(); i++) {
//          System.err.println("objTrans child ["+i+"] is "+objTrans.getChild(i));
//      }



    }

    public void setPickedVertexState(PickedState<V> pickedVertexState) {
        if(pickEventListener != null && this.pickedVertexState != null) {
            this.pickedVertexState.removeItemListener(pickEventListener);
        }
        this.pickedVertexState = pickedVertexState;
        this.renderContext.setPickedVertexState(pickedVertexState);
        if(pickEventListener == null) {
            pickEventListener = new ItemListener() {

                public void itemStateChanged(ItemEvent e) {
                    System.err.println(e.getItem()+" was picked");
                }
            };
        }
        pickedVertexState.addItemListener(pickEventListener);
    }

    public void setPickedEdgeState(PickedState<E> pickedEdgeState) {
        if(pickEventListener != null && this.pickedEdgeState != null) {
            this.pickedEdgeState.removeItemListener(pickEventListener);
        }
        this.pickedEdgeState = pickedEdgeState;
        this.renderContext.setPickedEdgeState(pickedEdgeState);
        if(pickEventListener == null) {
            pickEventListener = new ItemListener() {

                public void itemStateChanged(ItemEvent e) {
                    repaint();
                }
            };
        }
        pickedEdgeState.addItemListener(pickEventListener);
    }

    /**
     * @return the renderContext
     */
    public RenderContext<V, E> getRenderContext() {
        return renderContext;
    }
}

Here is graph builder

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.LayoutManager;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Canvas3D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.universe.SimpleUniverse;

import edu.uci.ics.jung.algorithms.layout3d.FRLayout;
import edu.uci.ics.jung.algorithms.layout3d.Layout;
import edu.uci.ics.jung.algorithms.layout3d.SpringLayout;
//import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization3d.VisualizationViewer;



public class GraphBuilder
{
    Graph<GraphNode, GraphEdge> g;
    Map<String, GraphNode> nodeList;
    VisualizationViewer<GraphNode, GraphEdge> vv;
    String root;
    private class GraphNode
    {
        String name;
        int i;
        GraphNode(int num)
        {
            i = num;
            name = Integer.toString(num);
        }

        GraphNode(String n)
        {
        name = n;
        }
        public String toString()
        {
            return name;
        }

    }

    private class GraphEdge
    {
        String name;
        GraphEdge()
        {

        }
    }

    GraphBuilder()
    {
        AccessGTDB demouser = new AccessGTDB();
        g = new SparseMultigraph<GraphNode, GraphEdge>();
        buildGraph(g);
        //buildTestGraph(g);

    }

    public JPanel getPanel()
    {
        AccessGTDB db = openDataBase();
        FRLayout<GraphNode, GraphEdge> fdl = new FRLayout<GraphNode, GraphEdge>(getBranchGraph(root, db));
        //Layout<GraphNode, GraphEdge> fdl = new SpringLayout<GraphNode, GraphEdge>(g);
        fdl.setSize(new BoundingSphere((new Point3d()), 1000));
        vv = new VisualizationViewer<GraphNode, GraphEdge>();
        vv.getRenderContext().setVertexStringer(new ToStringLabeller<GraphNode>());
        vv.setGraphLayout(fdl);
        vv.setPreferredSize(new Dimension(400, 600));
        closeDataBase(db);

        return vv;
    }

    public void pickedNodeCallBack()
    {

    }

    private void buildGraph(Graph<GraphNode, GraphEdge> gr)
    {
        int errorno;                            //  error flag

        AccessGTDB demouser = openDataBase();
        String brObj = demouser.getRootObject();        //  returns the name of the root object for currently selected database
        root = brObj;   //main root node for this database, need to keep track of it for access
        GraphNode tmpNode = new GraphNode(brObj); 
        nodeList = new HashMap<String, GraphNode>();
        nodeList.put(brObj, tmpNode);
        gr.addVertex(tmpNode);

        gr = buildFullGraph(gr, brObj, demouser);

        closeDataBase(demouser);

    }

    /*private Graph<GraphNode, GraphEdge> buildTestGraph(Graph<GraphNode, GraphEdge> gr)
    {
        nodeList = new HashMap<String, GraphNode>();

        for(int i = 0; i < 4; i++)
        {
            GraphNode tmp = new GraphNode(i);
            gr.addVertex(tmp);
            nodeList.put(Integer.toString(i), tmp);
        }

        for(int k = 0; k < 3; k++)
        {
            GraphEdge tmp = new GraphEdge();
            gr.addEdge(tmp, nodeList.get(Integer.toString(k)), nodeList.get(Integer.toString(k+1)));
        }
        GraphEdge tmp = new GraphEdge();
        gr.addEdge(tmp, nodeList.get(Integer.toString(3)), nodeList.get(Integer.toString(1)));

        return gr;
    }
    */
    private Graph<GraphNode, GraphEdge> buildFullGraph(Graph<GraphNode, GraphEdge> gr, String brObj, AccessGTDB demouser)
    {

        GraphNode brNode = nodeList.get(brObj);
        String branchList[];                              //  array to receive the list of all branches
        branchList = demouser.getObjectTree( brObj );     //  get all branches off the selected object
        int bl = branchList.length;                       //  length of the returned array

        //if leaf return
        if(bl <= 1)
        {
            return gr;
        }

        for ( int j=1; j < bl; j++)                       //  loop through all the branches
        {
            GraphNode tmpNode = new GraphNode(branchList[j]);
            nodeList.put(branchList[j], tmpNode);
            gr.addVertex(tmpNode);
            GraphEdge tmpEdge = new GraphEdge();
            gr.addEdge(tmpEdge, brNode, tmpNode);
            gr = buildFullGraph(gr, branchList[j], demouser);
        }

        return gr;                                         
    }

    private Graph<GraphNode, GraphEdge> getBranchGraph(String brObj, AccessGTDB demouser)
    {
        Graph<GraphNode, GraphEdge> toDraw = new SparseMultigraph<GraphNode, GraphEdge>();
        GraphNode brNode = nodeList.get(brObj);
        String branchList[];                              //  array to receive the list of all branches
        branchList = demouser.getObjectTree( brObj );     //  get all branches off the selected object
        int bl = branchList.length;                       //  length of the returned array

        for ( int j=1; j < bl; j++)                       //  loop through all the branches
        {
            GraphNode tmpNode = new GraphNode(branchList[j]);
            toDraw.addVertex(tmpNode);
            GraphEdge tmpEdge = new GraphEdge();
            toDraw.addEdge(tmpEdge, brNode, tmpNode);
        }

        return toDraw;                                         
    }

    private AccessGTDB openDataBase()
    {
        int errorno;                            //  error flag

        AccessGTDB demouser = new AccessGTDB();
        String DBList[];                            //  array that will contain the list of possible databases (demo only has one)
        DBList = demouser.getDatabases();           //  call to retrieve the list of possible databases
        int nl = DBList.length;

        errorno = demouser.selectDatabase( DBList[0] );     //  select the chosen database
        errorno = demouser.serverLogin( "localhost", "demouser", "********" );
        if ( errorno != 0 )                      //  verify server system was found and login was valid
        {
            System.out.printf ("\n");
            System.out.printf ("server login error number = %d\n",errorno);
            System.out.printf ("Program Terminated.\n");
            System.exit( errorno );
        }

            int tdbsize = -1;
        demouser.setTempDBSize ( tdbsize );

        errorno = demouser.openDatabase( "dbengine", "dbuser", "********" );     //  database login; for demo all values are ingored so can be anything

        if ( errorno != 0 )                      //  verify database engine was found and login was valid
        {
            System.out.printf ("\n");
            System.out.printf ("database open error number = %d\n",errorno);
            System.out.printf ("Program Terminated.\n");
            System.exit( errorno );
        }

        return demouser;
    }

    private void closeDataBase(AccessGTDB demouser)
    {
        int errorno;
        errorno = demouser.closeDatabase();                  //  log out of the database

        if ( errorno != 0 )                                  //  just make sure everything went ok
        {
            System.out.printf ("\n");
            System.out.printf ("database close error number = %d\n",errorno);
            System.out.printf ("Program Terminated.\n");
            System.exit( errorno );
        }
        // serverLogout
        //  while not technically needed, log off the server hosting the database (for localhost, this merely erases login name and password)
        errorno = demouser.serverLogout();                    //  log off the database server

          if ( errorno != 0 )                                   //  just make sure everything went ok
              {
              System.out.printf ("\n");
              System.out.printf ("server logout error number = %d\n",errorno);
              System.out.printf ("Program Terminated.\n");
              System.exit( errorno );
          }

    }
}

When I try to define GraphBuilder in VisualizationViewer, eclipse tells me GraphBuilder cannot be resolved to a type. Yet I can right click it and go to the definition. Am i missing something obvious here? Thanks for the help!

Addendum, I've cut out all classified material here, that does not have an effect on the problem.

Simply defining GraphBuilder as a class variable in VisualizationViewer is causing this error on line 69.

Alan
  • 31
  • 1
  • 1
  • 3
  • 4
    I think you need to start from the beginning. Your code simply makes no sense. Try the ["Classes and Objects"](http://download.oracle.com/javase/tutorial/java/javaOO/index.html) portion of the Java Tutorial trail. – Mark Peters Jun 15 '11 at 21:21
  • 2
    For starters, `package` is a reserved keyword and you can't name a class that. If that is what you're trying to do; I can't be sure from your snippet. – Mark Peters Jun 15 '11 at 21:25
  • I have an object, myclassa. It instantiates an object package. Package needs to modify some things in myclassa, so I need to be able to callback to myclassa from package. – Alan Jun 15 '11 at 21:25
  • this is all made up as far as words for the package naming and classes naming. its simply an abstract example. – Alan Jun 15 '11 at 21:26
  • @Alan: If it's an abstract example, why severely confuse things by using a name like "package" for a class? You realize they're very different concepts right? And that there's a difference between "object" and "class"? I think you need to give a much better and concrete example if we're to help you at all. It would give me some level of confidence if you tried to make it valid Java code. An [SSCCE](http://sscce.org) would be beneficial. – Mark Peters Jun 15 '11 at 21:28
  • @Alan, a package is used to organize classes in a source code database (this is a file system in most cases). It does not have any representation in source code except for the package declaration at the top of every source file. The `package { myclassa n;}` declaration is illegal in Java. – Vineet Reynolds Jun 15 '11 at 21:30
  • What about Animal, Cat, Dog, Bird? Give your self a break Alan, and don't be so abstract! – Costis Aivalis Jun 15 '11 at 21:31
  • @Costis. This is one case where one letter naming ( e.g. 'a', 'b', 'c' ) would be a step up. – Alexander Pogrebnyak Jun 15 '11 at 21:35
  • I guess you are right Alexander! – Costis Aivalis Jun 15 '11 at 21:37
  • @Alan: It's not even close to fixed. The problem is, you probably have some *nuance* that is causing your problem, and nuances don't get preserved into abstract (and completely erroneous) examples. It would be like asking us to fix a rattle in your car by saying only that your car has an engine and a body and not letting us look at it. You need to give us an example that **actually demonstrates the problem**. – Mark Peters Jun 15 '11 at 21:38
  • Very friendly board here guys. Ill look somewhere else. – Alan Jun 15 '11 at 21:40
  • 2
    @Alan: Your choice, we're volunteers. And we are being friendly, we're telling you exactly what effort **you** need to put in so that we can help **you**, **for free**. Here's a tip: prepare an actual example in your IDE that demonstrates the problem first, and then bring it over into this question once you know it's valid. You aren't doing your share of the work. – Mark Peters Jun 15 '11 at 21:42
  • You are right I was being lazy and I apologize for that, I cut out everything I can't post online, and now you have a concrete example. – Alan Jun 15 '11 at 21:58
  • @Alan: Answered (my 2nd answer). Remember to accept the answer that worked ;) – trutheality Jun 15 '11 at 21:59
  • @Alan: I'm glad you did some effort to get a concrete example, but I still highly recommend you read the link I posted earlier ([SSCCE](http://sscce.org)). 98% of that code is irrelevant to your specific problem and could be removed for brevity. Before posting it here, you should just delete any line of code that doesn't cause the error to either change or go away. You're going to continue to have resistant volunteers if you don't learn [how to ask smart questions](http://www.catb.org/~esr/faqs/smart-questions.html#volume). – Mark Peters Jun 15 '11 at 22:00

4 Answers4

2

I would recommend you read some Java tutorials like:

http://download.oracle.com/javase/tutorial/getStarted/application/index.html

Regarding your problem, I'm not sure where to start to solve your problem, but I think you want to do something like this

package demo.apackagename;

public class ClassA {

   public static void main(String [ ] args){
      ClassA a = new ClassA();
      ClassB b = new ClassB(a);
      b.methodBThatCallsA();
   }

   public void methodA() {
      System.err.println("methodA");
   }
}

Note that main() is the entry point of the application:

package demo.apackagename;

public class ClassB {
   private ClassA a = null;
   public ClassB(ClassA a_) {
      this.a = a_;
   }

   public void methodBThatCallsA() {
      System.out.println("Class B");
      a.methodA();
   }
}
Nathan
  • 8,093
  • 8
  • 50
  • 76
Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
1

Obsolete Answer

Filling in the "blanks": Your code looks like this now:

import things.*;

class MyClass{
    MyClass(){
        SpecialClass x = new SpecialClass(this); // totally fails.
    }
}

class SpecialClass{
    SpecialClass(){
        MyClass y;
    }
}

Why totally fails? Because SpecialClass doesn't have a constructor that takes a MyClass lol!

Also, like, this doesn't like "exist" before the constructor finishes dude.

trutheality
  • 23,114
  • 6
  • 54
  • 68
  • 2
    You can leak references to `this` before the constructor finishes. You just can't leak it before the **super** constructor is called. – Mark Peters Jun 15 '11 at 21:46
  • Did not know that. Is that considered bad practice? – trutheality Jun 15 '11 at 21:47
  • Depends what you're leaking it to, and whether people will try to use you before you've finished constructing. One common example in Swing code is to add `this` to one of the components as a listener. I don't think it's great practice unless you can prove (at least to yourself) that nobody's going to use it improperly before the constructor finishes. – Mark Peters Jun 15 '11 at 21:49
1

I might be a bit sleepy here but I think you need to define the constructor of GraphBuilder as

public GraphBuilder(){
}

if you leave it as

GraphBuilder(){
}

the scope of a constructor without the visibility keyword is only of its package.

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • No, the problem I am having is that if I try to declare GraphBuilder as a class variable in Visualization Viewer(line 69) it gives me this error. Its like eclipse won't recognize GraphBuilder is on the build path when it is in VisualizationViewer. – Alan Jun 15 '11 at 22:22
  • @Alan. What package is `GraphBuilder` in? – Alexander Pogrebnyak Jun 15 '11 at 23:32
0

You need to import GraphBuilder in your VisualizationViewer file.

trutheality
  • 23,114
  • 6
  • 54
  • 68
  • Except eclipse wont recognize that. GraphBuilder is in my main source file. VisualizationViewer is in this package. Both are on the build path. What am I doing wrong with eclipse? – Alan Jun 15 '11 at 22:02
  • GraphBuilder is not in a package. But it is on the build path. If eclipse has no problem with classes being next to one another and not in a package, why does this file thats in a package can't see what is on the build path? – Alan Jun 15 '11 at 22:11
  • If you mean that `GraphBuilder` is in the Default package (no package specified) then you would import it with `import GraphBuilder;`. In general it's bad practice to have anything in the default package (or, "without a package"). Classes that are in the same package always see each other, so no include is needed there. – trutheality Jun 15 '11 at 23:08
  • @Alan Correction. Take a look at http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package -- you can't include a class if it doesn't have a package. It doesn't matter that it's on the build path. – trutheality Jun 15 '11 at 23:21