Grid graphe course

Hello hello,

Today I encounter some difficulties on using JGraphT.
I try to create a grid map (a matrix of squares)

And in the future, I will seek to make the shortest path, the structure of my map must be a graph.
After many trials, I opted for that lib quoted above.

Regarding the creation of the graph, it’s easy and clear.
By cons I have missed something regarding the course of it.

Indeed for each node in the graph, I want to make some actions.
drawing for exemple.

I’m reading the Javadoc for 2h and search on Google and other website. I can not find a solution (which I think is not very complicated. (For each node … to do…)

That’s why I seek some help =)

little code :


public class Model
{
	private JFrame jFrame;
	private Carte carte;
	
	public Model(JFrame _jFrame)
	{
		jFrame = _jFrame;
		initialize();
	}
	
	public void initialize()
	{
		carte = new Carte(this);
	}
	
	public void update()
	{
		carte.update();
	}
	
	public void draw(Graphics g) 
	{
		carte.draw(g);
	}
}


public class Carte
{
	private Model model;
	private UndirectedGraph<Case, DefaultEdge> graph;
	
	public Carte(Model _model)
	{
		model = _model;
		initialize();
	}

	public void initialize()
	{
		graph = createCasesGraph();
	}
	
	public void update()
	{

	}
	
	public void draw(Graphics g) 
	{
		System.out.println(graph.toString());
		//**********************************
		// foreach node
		//   case.draw(g);
		//**********************************		
	}
	
	private static UndirectedGraph<Case, DefaultEdge> createCasesGraph()
	{
		UndirectedGraph<Case, DefaultEdge> g =
			new SimpleGraph<Case, DefaultEdge>(DefaultEdge.class);
		
		// Vertex
		Case v1 = new Case(0,0);
		Case v2 = new Case(0,1);
		Case v3 = new Case(1,0);
		Case v4 = new Case(1,1);

		g.addVertex(v1);
		g.addVertex(v2);
		g.addVertex(v3);
		g.addVertex(v4);

		// Edges
		g.addEdge(v1, v2);
		g.addEdge(v1, v3);
		
		g.addEdge(v2, v1);
		g.addEdge(v2, v4);
		
		g.addEdge(v3, v1);
		g.addEdge(v3, v4);
		
		g.addEdge(v4, v2);
		g.addEdge(v4, v3);
	
		return g;
	}
}


public class Case 
{
	private int col;
	private int row;
	private int height;
	private int width;

	public Case()
	{
		this(0,0);
	}

	public Case(int _col, int _row)
	{
		col = _col;
		row = _row;
		initialize();
	}

	public void initialize()
	{
		height = 50;
		width = 50;
	}

	public void update()
	{

	}

	public void draw(Graphics g)
	{
		g.setColor(Color.black);
		g.fillRect(col * width, row * width, width, height);
		g.setColor(Color.white);
		g.drawRect(col * width, row * width, width, height);

		Graphics2D g2 = (Graphics2D) g;
		FontRenderContext frc = g2.getFontRenderContext();
		int fontSize = 11;
		Font f = new Font("Comic Sans MS",Font.BOLD, fontSize);

		String sScore = new String(col + "-" + row);
		TextLayout tScore = new TextLayout(sScore, f, frc);
		tScore.draw(g2,col * width, row * height + fontSize);
	}
}

Although you can represent a grid as a graph, using a graph-theoretic library like JGraphT is probably overkill for pathfinding on a grid, since it’s going to make you create a parallel node structure just to run Dijkstra’s algorithm on it (and it’s not clear that JGraphT even has A*). At the very least it’s going to waste tons of memory. It might come in handy later on if you start routing between waypoints, but you really don’t want to treat every cell in your grid as a node with 8 edges.

I recommend googling for “A* Patfhinding” (yes google will search on the star here). The first couple hits should provide you more than enough to get started.