Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Regenuluz (posted 2013-03-17 07:52:59, viewed 218 times)

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   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169  
package org.anized.main;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Runner extends JFrame implements Runnable {

   private List<Node> map = new ArrayList<>();
   private List<Node> path = new ArrayList<>();

   private Node start;
   private Node goal;

   private final static int MAP_SIZE = 256;

   public Runner() {
      this.setTitle("Pathfinder");
      this.setSize(410, 410);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
   }

   public void init() {
      Node[][] grid = new Node[MAP_SIZE][MAP_SIZE];

      for(int y = 0; y < MAP_SIZE; y++) {
         for(int x = 0; x < MAP_SIZE; x++) {
//            if((x < 8 && y == 5) || (x > 2 && y == 7) || (x == 7 && y == 9) || (x == 5 && y == 8))
//               continue;

            grid[x][y] = new Node(x, y, x + "," + y);
            this.map.add(grid[x][y]);
         }
      }

      for(int y = 0; y < MAP_SIZE; y++) {
         for(int x = 0; x < MAP_SIZE; x++) {
            if(grid[x][y] == null)
               continue;

            // Neigbor left
            if(x > 0) {
               if(grid[x - 1][y] != null)
                  grid[x][y].addNeighbor(grid[x - 1][y]);
            }

            // Neighbor right
            if(x < MAP_SIZE - 1) {
               if(grid[x + 1][y] != null)
                  grid[x][y].addNeighbor(grid[x + 1][y]);
            }

            // Neighbor up
            if(y < MAP_SIZE - 1) {
               if(grid[x][y + 1] != null)
                  grid[x][y].addNeighbor(grid[x][y + 1]);
            }

            // Neighbor down
            if(y > 0) {
               if(grid[x][y - 1] != null)
                  grid[x][y].addNeighbor(grid[x][y - 1]);
            }
         }
      }
   }

   @Override
   public void run() {
      init();
      Random rand = new Random();

      while(true) {
         this.start = this.map.get(rand.nextInt(this.map.size()));
         this.goal = this.map.get(rand.nextInt(this.map.size()));

         System.out.format("Searching from (%s) to (%s)\n", this.start, this.goal);

         long t1 = System.nanoTime();
         this.path = AStar.path(this.start, this.goal);
         System.out.println("Path found in: " + (System.nanoTime() - t1) / 1E9 + "s");
         System.out.println(this.path);

         render();

         try {
            Thread.sleep(1500);
         } catch(InterruptedException e) {
            e.printStackTrace();
         }
      }

   }

   private void render() {
      BufferStrategy bs = this.getBufferStrategy();
      if(bs == null) {
         this.createBufferStrategy(2);
         bs = this.getBufferStrategy();
      }

      Graphics2D g = (Graphics2D) bs.getDrawGraphics();

      g.setColor(Color.white);
      g.fillRect(0, 0, this.getWidth(), this.getHeight());

      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

      g.setColor(Color.blue);
      for(Node node : this.map) {
         for(Node n : node.getNeighbors()) {
            int rx = node.x * 20 + 10;
            int ry = this.getHeight() - (node.y * 20) - 10;
            g.drawLine(rx, ry, n.x * 20 + 10, this.getHeight() - (n.y * 20) - 10);
         }
      }

      for(Node node : this.map) {
         if(node == this.start) {
            g.setColor(Color.green);
         } else if(node == this.goal) {
            g.setColor(Color.red);
         } else if(node.searchCount != AStar.searchCount) {
            g.setColor(Color.black);
         } else {
            g.setColor(Color.blue);
         }

         int rx = node.x * 20 + 10;
         int ry = this.getHeight() - (node.y * 20) - 10;
         g.fillOval(rx - 3, ry - 3, 9, 9);

         g.setColor(Color.black);
         g.drawString(node.name, rx, ry - 3);
      }

      g.setColor(Color.red);
      int x1 = -1, x2, y1 = -1, y2;
      for(Node node : this.path) {
         x2 = node.x * 20 + 10;
         y2 = this.getHeight() - (node.y * 20) - 10;

         if(x1 != -1) {
            g.drawLine(x1, y1, x2, y2);
         }

         x1 = node.x * 20 + 10;
         y1 = this.getHeight() - (node.y * 20) - 10;
      }

      g.dispose();
      bs.show();
   }

   public static void main(String[] args) {
      Runner r = new Runner();
      new Thread(r).start();
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
xxMrPHDxx (17 views)
2017-11-21 16:21:00

xxMrPHDxx (11 views)
2017-11-21 16:14:31

xxMrPHDxx (14 views)
2017-11-21 16:10:57

Ecumene (113 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (247 views)
2017-08-02 12:28:51

cybrmynd (244 views)
2017-08-02 12:19:43

cybrmynd (244 views)
2017-08-02 12:18:09

Sralse (258 views)
2017-07-25 17:13:48

Archive (877 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!