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);
}
}