Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Grid graphe course  (Read 861 times)
0 Members and 1 Guest are viewing this topic.
Offline Mandelbrot

Senior Newbie





« Posted 2012-02-17 11:32:38 »

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 :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
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);
   }
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
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;
   }
}

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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);
   }
}

-----------------------------------------------------
I am French, be indulgent with my written words please
Offline sproingie
« Reply #1 - Posted 2012-02-17 18:29:48 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (93 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (213 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!