The Artima Developer Community
Sponsored Link

Java Answers Forum
Need Help Bad

1 reply on 1 page. Most recent reply: Mar 10, 2004 10:29 AM by Niall

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Niall

Posts: 10
Nickname: niall
Registered: Feb, 2004

Need Help Bad Posted: Mar 10, 2004 9:47 AM
Reply to this message Reply
Advertisement
I have created a program where the user draws a series of vertices and associated edges on screen. This part of the program works with no problems. However, when I try to use StringTokenizer to read the data back from a file, the graph is not drawn properly. The vertices and edges are correctly added to their relevant vectors, and everything seems to be normal, however only the vertices are drawn on screen.

This only happens when I open a file, when I draw the edges manually, they are displayed correctly. When an edge is to be drawn, it first has to search the vector of vertices to get the corresponding x and y co-ordinates. I am guessing that it is at this part of the program I am getting an error.

I can fix this problem using arrays, however it is part of a project I am doing and I am required to use vectors. If anyone could help I would be extremely grateful, Thanks.

Below is the code where I read from file:
public void openFile()// open a file
{  final JFileChooser fc = new JFileChooser();
   int returnVal = fc.showOpenDialog(MenuFrame.this);
   if (returnVal == JFileChooser.APPROVE_OPTION) 	
  {
   File file = fc.getSelectedFile();
   try 
   { 
     Reader dataReader;
     dataReader = new FileReader(file.getName()); 
     BufferedReader reader = new BufferedReader(dataReader);
     String inputLine;
     while ( (inputLine = reader.readLine()) != null ) 
     {  StringTokenizer tokenizer = new StringTokenizer(inputLine);
        String token;
        token = tokenizer.nextToken();
       if (token.equals("Vertex")) 
       {	String vertexName = tokenizer.nextToken();
       	addVertex(vertexName,	
	(Integer.parseInt(tokenizer.nextToken())),
	(Integer.parseInt(tokenizer.nextToken())));
       }
      else if (token.equals("Edge")) 
      {	String from = tokenizer.nextToken();
      	String to = tokenizer.nextToken();
	addEdge(from, to, (							  Integer.parseInt(tokenizer.nextToken())));
      }
  }
}

Here is the code where I draw the vertices and edges:
public void paintComponent(Graphics g)
{	// PaintComponent
  Graphics2D g2 = (Graphics2D)g;
  super.paintComponent(g);   
							
  for (int i=0; i<vertices.size();i++)
  {  Object vectorObject = vertices.get(i);
     Vertex newYourObject;
     newYourObject = ((Vertex)vectorObject);
     int getVX = newYourObject.xLoc;
     int getVY = newYourObject.yLoc;
     String getVName = newYourObject.name;
    g.setColor(newYourObject.vertexColor);
    g.fillOval(getVX, getVY, 10, 10);
    g.drawString(getVName,getVX, getVY); 
  }
						
  // Draw all edges
  for (int i=0; i<edgesVec.size();i++) 
  {  Object edgeObject = edgesVec.get(i);
     Edge newEdgeObject;
     newEdgeObject = ((Edge)edgeObject);
     getNameFrom = newEdgeObject.nameFrom;
     getNameTo = newEdgeObject.nameTo;
     getWeight = newEdgeObject.weight;
     for (int j=0; j<vertices.size();j++)
    {	Object vectorObject = vertices.get(j);
	Vertex newYourObject;
	newYourObject = ((Vertex)vectorObject);
	if(newYourObject.name == getNameFrom)
	{	getX1 = newYourObject.xLoc;
		getY1 = newYourObject.yLoc;
	}	
	if(newYourObject.name == getNameTo)
	{	getX2 = newYourObject.xLoc;
		getY2 = newYourObject.yLoc;
	}	
     }
    g.drawLine(getX1+5, getY1+5, getX2+5, getY2+5);
    g.drawString(""+getWeight, 
      (getX1 + getX2) / 2 -10, (getY1 + getY2) / 2);
}


Niall

Posts: 10
Nickname: niall
Registered: Feb, 2004

Re: Need Help Bad Posted: Mar 10, 2004 10:29 AM
Reply to this message Reply
Sorry, just realised I should be using .equals() instead of ==. D'oh!!!

Flat View: This topic has 1 reply on 1 page
Topic: commit() and rollback() problem! Previous Topic   Next Topic Topic: CPU monitoring thru java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use