Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't run the method, Please help!!

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't run the method, Please help!!

    package triangle_projection;
     
    import java.io.*;
    import java.awt.*;
     
    public class Prim extends JComponent {
     
    	public static int [][] Graph;
    	public static int [][] Tree;
    	public static int [] near;
     
    	public static int n;
    	public static int mincost = 0;
    	public static int k, l;
     
            public Edge edges[] = new Edge[100];
            public Main main = new Main();
            public Graph graph;
     
            //create a minal spanning tree
    	public static void prims()
    	{
    		getMinKL();
    		mincost = Graph[k][l];
    		Tree[1][1] = l;
    		Tree[1][2] = k;
    		for(int i=1; i<=n; i++)
    			near[i] = (Graph[i][l]<Graph[i][k])?l:k;
    		near[k] = near[l] = 0;
     
    		for(int i=2; i<n; i++)
    		{
    			int j = getMin();
    			Tree[i][1] = j; Tree[i][2] = near[j];
    			mincost = mincost+Graph[j][near[j]];
    			near[j] =0;
    			for (int k=1; k<=n; k++)
    				if( (near[k] !=0) && Graph[k][ near[k] ]> Graph[k][j] )
    					near[k] =j;
    		}
    	}
     
            public int numofVer() { return n; } // return the total number of the node
     
            //get the original space distance of 2 points
            public int GetDis(int one, int two){
                int dis = Graph[one][two];
                return dis;
            }
     
            public static void main (String[] args) throws IOException
            {
    		prims();
                    [COLOR="Red"]//main.createNode();[/COLOR]
     
    		System.out.println("\n\nSolution : \n\n");
     
                    JFrame window = new JFrame();
                    window.setBounds(500, 500, 500, 500);
                    window.setBackground(new Color(255,255,255));
                    window.getContentPane().add(new Graph());
                    window.setVisible(true);
    	}
    }

    package triangle_projection;
     
    public class Main {
     
        private int n=0;   
        private Prim prim = new Prim();
        public int Vertnum = prim.numofVer();
        public Point pt = new Point();
        public Graph graph = new Graph();
        private Node One, Two;
     
        public void createNode() {
     
                //Random rand = new Random();
     
                for (int j=1; j<=Vertnum; j++){
                    if (j == 1){
                        nodes[j] = new Node(0,0,j);
                    }
                    else if (j == 2){
                        int dis = prim.GetDis(1, 2);
                        nodes[j] = new Node(dis,0,j);
                    }
                    else{
                        for (int i=1; i<=3; i++){
                            dist2[i] = prim.GetDis(i, j);
                        }
                        for (int i=1; i<=3; i++){
                            if((i!=j)&&(i<j))
                            {
                                int x1 = pt.getx1(One, Two, dist2[2], dist2[3]);
                                int y1 = pt.gety1(One, Two, dist2[2], dist2[3]);
                                int x2 = pt.getx2(One, Two, dist2[2], dist2[3]);
                                int y2 = pt.gety2(One, Two, dist2[2], dist2[3]);
     
                                int n1 = graph.isNode(x1,y1);
                                int n2 = graph.isNode(x2,y2);
     
                                if ( n1 == -1){
                                    if(x1 > 0 && y1 > 0){
                                    nodes[i+2] = new Node(x1,y1,j);
                                    }
                                }
     
                                if ( n2 == -1){
                                    if(x2 > 0 && y2 > 0){
                                    nodes[i+2] = new Node(x2,y2,j);
                                    }
                                }
                             }
                        }
                    }
                }
     
                for (int l=1; l<=Vertnum; l++){
                     for (int d=1; d<=3; d++){
                            if((d!=l)&&(d<l))
                            {
                                realDist(nodes[l], nodes[d]);
                            }
                    }
                }
             }
        }
    }

     
    package triangle_projection;
    import java.awt.*;
     
    public class Graph extends Canvas{
     
    ....
     
            /*public Graph(int maxx, int maxy) {
    		mx = maxx;
    		my = maxy;
    		setSize(mx, my);
    		ni=0; ei=0;
    		nodes = new Node[Vertnum]; //create a Node array of size 9
    		edges2 = new Edge[Vertnum*(Vertnum-1)/2]; //create an Edge array of size 36
    	}*/
     
            //paint method draws graphics on the screen and called implicitly
    	public void paint(Graphics g) {
    		for (int i=0;i < ei;i++) {
    			Edge e = edges2[i];
    			/* draw an edge */
    			drawEdge(e.firstNode(), e.secondNode(), e.weight(), g);
    		}
    		for (int i=0;i < ni;i++) {
    			Node n = nodes[i];
    			/* draw a node */
    			drawNode(n.xPos()-8, n.yPos()-8, n.no()+1, g);
    		}
    	}
     
     
            //drawNode method draws a node at the given coordinates and in given the color
    	private void drawNode(int x, int y, int no, Graphics g) {
    		g.fillOval(x, y, 17, 17); // draw an oval which is filled with the current color
    		g.setColor(blue); // set current color to black
    		g.drawOval(x, y, 16, 16); // draw an oval
    		g.drawString(Integer.toString(no), x+5, y+13); // draw the node number
    	}
     
            //drawEdge method draws an edge between the given points and in the given color
    	private void drawEdge(Node first, Node second, int weight, Graphics g) {
    		int hyp, x, y;
    		int x1 = first.xPos();
    		int y1 = first.yPos();
    		int x2 = second.xPos();
    		int y2 = second.yPos();
    		x = x2 - x1;
    		y = y2 - y1;
    		hyp = (int) Math.sqrt(x*x + y*y); // calculate the distance between the given two point
                    g.drawLine(x1, y1, x2, y2); // draw a line between the given points
    		x1 = x1 + (int)((x/hyp)*(hyp/2)); // calculate the middle of the given two points
    		y1 = y1 + (int)((y/hyp)*(hyp/2));
    		g.setColor(yellow); // set current color to yellow
    		g.fillOval(x1-8, y1-8, 17, 17);
    		if (weight < 10)
    			g.drawString(Integer.toString(weight), x1-3, y1+5); // draw the edge weight
    		else
    			g.drawString(Integer.toString(weight), x1-6, y1+5); // draw the edge weight
    	}
            //numOfNodes method return the number of nodes on the screen
    	public int numOfNodes() { return ni; }
     
    }

    How can i run the createNode() in Main.java, in the Prim. java?? The error is Non-static variable cannot be reference on static content. So what can i change to make it work?
    Also, the Prim.java is also not showing the jframe which is connected to Graph.java....
    Can anyone please help??
    Thank you


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Can't run the method, Please help!!

    I would advise not having a class called Main. This is not a good thing to do because the main method is what the JVM uses to execute applications. I would then never have a variable called main because this could be misleading the JVM to your actual intentions.

    Regardless, your problem is JAVA 101 that is overlooked alot. It is one of those problems that you will make once and never again.

    The main method is always static. As a rule, all methods and class variables accessed inside "static content" needs to be static. The exception to this is any variables local to the method. So, since the main is static, all variables and methods you use inside of it that have been declared outside of it need to be static.

    So, your problem is the declaration of:
    public Main main = new Main();
    For the class that holds your main specifically, all methods and variable need to be static or you will not be able to use them. So, change these:
    public Edge edges[] = new Edge[100];
            public Main main = new Main();
            public Graph graph;
    To:
    public static Edge edges[] = new Edge[100];
            public static Main main = new Main();
            public static Graph graph;

    And, change these:
    public int numofVer() { return n; } // return the total number of the node
     
            //get the original space distance of 2 points
            public int GetDis(int one, int two){
                int dis = Graph[one][two];
                return dis;
            }
    To:
    public static int numofVer() { return n; } // return the total number of the node
     
            //get the original space distance of 2 points
            public static int GetDis(int one, int two){
                int dis = Graph[one][two];
                return dis;
            }

    JAVA basics for you. It is an error that I think all beginners get and I field it all the time.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/