Results 1 to 6 of 6
  1. #1
    naffie is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Question Implement Astar search algorithm on JPanel

    Hey Guys,I've been learning java and i'm trying to implement the a star path finding algorithm using a simple JFrame with JPanel that has a grid layout, for days now,with no success.After i click the start button on the main class,it's supposed to launch the second class which runs the algorithm.For now,nothing happens after clicking it.

    Where exactly am i going wrong?Is there another approach that would still use a Jpanel/ Jframe?
    Here's my code i'll post both classes

    --------------------------The first class----------------------------------



    Java Code:
     public class Grid extends JFrame {
    
    	private static final long serialVersionUID = 1L;
    	public JPanel mainPanel, southPanel;
    	public final JFrame mainFrame = new JFrame();
    	public static int a = 7;
    	public static int b = 7;
    	public JPanel[][] panelcontainer = new JPanel[a][b];
    	private Maze maze;
    	private int i;
    	private int j;
    
    	public Grid() {
    		mainPanel = new JPanel();
    		mainPanel.setLayout(new GridLayout(a, b));
    
    		for ( i = 0; i < a; i++) {
    
    			for ( j = 0; j < b; j++) {
    
    				panelcontainer[i][j] = new JPanel();
    				panelcontainer[i][j].setBorder(BorderFactory.createLineBorder(
    						Color.black, 1));
    				panelcontainer[i][j].setBackground(Color.blue);
    				mainPanel.add(panelcontainer[i][j]);
    
    				mainPanel.setBackground(Color.darkGray);
    			}
    		}
    
    		// new panel at the south border to hold start button
    		southPanel = new JPanel();
    
    		// --- Create a button. Add a listener to it.
    		JButton startButton = new JButton("start");
    		southPanel.add(startButton);
    		southPanel.setBackground(Color.red);
    		startButton.addActionListener(new ActionListener() {
    
    			
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    
    //				panelcontainer[i][j].setVisible(false);
    				 maze = new Maze(7,7);
    				// maze.setVisible(true);
    				maze.findBestPath();
    
    			}
    		});
    
    		// JFrame specifications
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    
    		add(mainPanel, BorderLayout.CENTER);
    		add(southPanel, BorderLayout.PAGE_END);
    		//add(new Maze(7, 7));
    		// mainPanel.add(new Maze());
    		pack();
    		setVisible(true);
    		setSize(464, 485);
    		setTitle("Astar Simulation");
    		setLocationRelativeTo(null);
    	}
    
    	public static void main(String[] args) {
    		// Schedule a job for the event-dispatching thread:
    		// creating and showing this application's GUI.
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				new Grid();
    
    				// Grid.this.validate();
    			       
    			}
    		});
    
    	}
    
    }

    --------------------------------and the second class:------------------------------------



    Java Code:
     public class Maze extends JPanel {
    
    	private int rows;
    	private int columns;
         private Maze maze;
    
    	public JPanel[][] astarPanel;
    	private JPanel goal;
    	private Set<JPanel> adjacencies = new HashSet<JPanel>();
    
    	private List<JPanel> opened = new ArrayList<JPanel>();
    	private List<JPanel> closed = new ArrayList<JPanel>();
    	private List<JPanel> bestList = new ArrayList<JPanel>();
    	private boolean start;
    	private boolean end;
    	private int x;
    	private int y;
    	public JPanel mainPanel2;
    	private JPanel parent;
    	private double localCost; // cost of getting from this square to goal i.e
    								// 'H'
    	private double parentCost; // cost of getting from parent square to this
    								// node i.e 'G'
    	private double passThroughCost;// cost of getting from the start to the goal
    									// i.e 'F'
    
    	// through this square
    
    	public Maze(int rows, int columns) {
    
    		this.rows = rows;
    		this.columns = columns;
    		//this.x = x;
    		//this.y = y;
    
    		mainPanel2 = new JPanel();
    		mainPanel2.setLayout(new GridLayout(rows, columns));
    
    		astarPanel = new JPanel[rows][columns];
    		generate();
    	}
    
    	private void generate() {
    
    		createSquares();
    		setStartAndGoal();
    		generateAdjacenies();
    	}
    
    	public int getRows() {
    
    		return rows;
    	}
    
    	public int getColumns() {
    
    		return columns;
    	}
    
    	private void setStartAndGoal() {
    		maze = new Maze(rows, columns);
    
    		   astarPanel[0][0]= maze.setStart(true);
    		Random random = new Random();
    		int goalX = 0, goalY = 0;
    		while (goalX == 0 && goalY == 0) {
    			goalX = random.nextInt(rows);
    			goalY = random.nextInt(columns);
    		}
    		goal = astarPanel[goalX][goalY];
    		 goal = maze.setEnd(true);
    	}
    
    	private void generateAdjacenies() {
    
    		for (int i = 0; i < rows; i++) {
    			for (int j = 0; j < columns; j++) {
    				((Maze) astarPanel[i][j]).calculateAdjacencies();
    			}
    		}
    	}
    
    	private void createSquares() {
    
    		for (int i = 0; i < rows; i++) {
    			for (int j = 0; j < columns; j++) {
    				astarPanel[i][j] = new JPanel();
    				astarPanel[i][j].setBorder(BorderFactory.createLineBorder(
    						Color.black, 1));
    				astarPanel[i][j].setBackground(Color.blue);
    
    				mainPanel2.add(astarPanel[i][j]);
    
    				mainPanel2.setBackground(Color.darkGray);
    			}
    		}
    	}
    
    	public JPanel getSquare(int x, int y) {
    
    		return astarPanel[x][y];
    	}
    
    	public void setSquare(JPanel square) {
    
    		astarPanel[square.getX()][square.getY()] = square;
    	}
    
    	public void drawPath() {
    
    		for (int i = 0; i < rows; i++) {
    			for (int j = 0; j < columns; j++) {
    				JPanel square = astarPanel[i][j];
    
    				if ((j - 1 < 0) && (i - 1 < 0)) {
    
    					if (bestList.contains(square)) {
    						astarPanel[i][j].setBackground(Color.CYAN);
    						return;
    					}
    				}
    			}
    		}
    	}
    
    	public void findBestPath() {
    		Set<JPanel> adjacencies = ((Maze) astarPanel[0][0]).getAdjacencies();
    		for (JPanel adjacency : adjacencies) {
    			((Maze) adjacency).setParent(astarPanel[0][0]);
    			if (((Maze) adjacency).isStart() == false) {
    				opened.add(adjacency);
    			}
    		}
    
    		while (opened.size() > 0) {
    			JPanel best = findBestPassThrough();
    			opened.remove(best);
    			closed.add(best);
    			if (((Maze) best).isEnd()) {
    				populateBestList(goal);
    				drawPath();
    				return;
    			} else {
    				Set<JPanel> neighbors = ((Maze) best).getAdjacencies();
    				for (JPanel neighbor : neighbors) {
    					if (opened.contains(neighbor)) {
    
    						neighbor = astarPanel[getX()][getY()];
    						JPanel tmpSquare = new JPanel();
    						tmpSquare = neighbor;
    						// JPanel tmpSquare = new JPanel(neighbor.getX(),
    						// neighbor.getY(), this);
    						((Maze) tmpSquare).setParent(best);
    						if (((Maze) tmpSquare).getPassThrough(goal) >= ((Maze) neighbor)
    								.getPassThrough(goal)) {
    							continue;
    						}
    					}
    
    					if (closed.contains(neighbor)) {
    						neighbor = astarPanel[getX()][getY()];
    						JPanel tmpSquare = new JPanel();
    						tmpSquare = neighbor;
    
    						// JPanel tmpSquare = new JPanel(neighbor.getX(),
    						// neighbor.getY(), this);
    						((Maze) tmpSquare).setParent(best);
    						if (((Maze) tmpSquare).getPassThrough(goal) >= ((Maze) neighbor)
    								.getPassThrough(goal)) {
    							continue;
    						}
    					}
    
    					((Maze) neighbor).setParent(best);
    
    					opened.remove(neighbor);
    					closed.remove(neighbor);
    					opened.add(0, neighbor);
    				}
    			}
    		}
    
    		
    	}
    
    	public void populateBestList(JPanel goalSquare) {
    
    		bestList.add(goalSquare);
    		if (((Maze) goalSquare.getParent()).isStart() == false) {
    			populateBestList((JPanel) goalSquare.getParent());
    		}
    
    		return;
    	}
    
    	public JPanel findBestPassThrough() {
    
    		JPanel best = null;
    		for (JPanel square : opened) {
    			if (best == null
    					|| ((Maze) square).getPassThrough(goal) < ((Maze) best)
    							.getPassThrough(goal)) {
    				best = square;
    			}
    		}
    
    		return best;
    	}
    
    	
    	public void calculateAdjacencies() {
    
    		int top = x - 1;
    		int bottom = x + 1;
    		int left = y - 1;
    		int right = y + 1;
    
    		if (bottom < ((Maze) astarPanel[x][y]).getRows()) {
    			if (isAdjacent()) {
    				((Maze) ((Maze) astarPanel[x][y]).getSquare(bottom, y))
    						.addAdjacency(this);
    				this.addAdjacency(((Maze) astarPanel[x][y])
    						.getSquare(bottom, y));
    			}
    		}
    
    		if (right < ((Maze) astarPanel[x][y]).getColumns()) {
    			if (isAdjacent()) {
    				((Maze) ((Maze) astarPanel[x][y]).getSquare(x, right))
    						.addAdjacency(this);
    				this.addAdjacency(((Maze) astarPanel[x][y]).getSquare(x, right));
    			}
    		}
    	}
    
    	
    	public double getPassThrough(JPanel goal) {
    
    		if (isStart()) {
    			return 0.0;
    		}
    		return getLocalCost(goal) + getParentCost();
    
    	}
    
    	public double getLocalCost(JPanel goal) {
    
    		if (isStart()) {
    			return 0.0;
    		}
    
    		localCost = 10.0 * (Math.abs(x - goal.getX()) + Math
    				.abs(y - goal.getY()));
    		return localCost;
    	}
    
    	public double getParentCost() {
    
    		if (isStart()) {
    			return 0.0;
    		}
    
    		if (parentCost == 0.0) {
    			parentCost = 10.0 + .5 * (((Maze) parent).getParentCost() - 10.0);
    		}
    
    		return parentCost;
    	}
    
    	public boolean isAdjacent() {
    
    		if (Math.random() > .5) {
    			return true;
    		}
    		return false;
    	}
    
    }
    Last edited by naffie; 10-02-2013 at 03:09 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Implement Astar search algorithm on JPanel

    You'll have to debug it.
    But the general structure looks wrong to me.
    You create a Maze (a JPanel) in the action listener, but that panel does not sit anywhere.
    The Maze also creates a load of panels, none of which (I think) get added to the Maze.
    So I don't see why you should expect anything to appear on your screen.

    For something like this I would recommend producing a simple model that represents your map, and produce an A* that runs against that model.
    Then I would construct a GUI that accesses that model in order to display the map and the final route taken.
    You're mixing up your gui stuff with the A* logic and, I suspect, that is the root cause of the problem.
    Please do not ask for code as refusal often offends.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Implement Astar search algorithm on JPanel

    Other approach:

    I actually did an exact school assignment like this years ago. I did it using custom painting though, so I created my own class which extended JPanel and I implemented my own paintComponent() method to draw a maze on there, with the ability to click on an unoccupied cell of the maze to set the begin and end point (which would light up the cells with two different colors). Upon setting the end point, the A* algorithm would be used to draw the shortest path between those two points.

    The maze itself was just a simple 2D array of Character objects if I remember correctly, each character representing something specific like 'w' for wall, 's' for start', 'e' for end and 'p' for a drawn path. a null value would be an unoccupied cell. The JPanel's drawing logic can then use that array to paint all the cells with different colors, and updating the maze is as simple as changing the array and performing a repaint().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    naffie is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Implement Astar search algorithm on JPanel

    Thanks Tolls.I read about an approach like you're suggesting but the implementation was to complex.I will do more research on how i can follow your approach.So in this case,all the GUI panel stuff is in only one class which just calls the others?

  5. #5
    naffie is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Re: Implement Astar search algorithm on JPanel

    Thank you @gimbal2 .I've see that approach and several implementations like that.In my case though,i though it would be easier if the maze just drew itself.The only problem is that i got my logic and the GUI stuff mixed up like Tolls said.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Implement Astar search algorithm on JPanel

    I wasn't suggesting anything, only answering the "other approaches" bit of your question. I would indeed try to fix what you have.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. In search of a graph algorithm...
    By kjkrum in forum Forum Lobby
    Replies: 0
    Last Post: 07-14-2012, 01:17 PM
  2. How to implement the wall follower algorithm in java?
    By michaelnana in forum New To Java
    Replies: 8
    Last Post: 02-10-2012, 08:42 PM
  3. implement binary search method using recursion?
    By chopo1980 in forum New To Java
    Replies: 1
    Last Post: 12-12-2009, 03:58 PM
  4. Implement Search Functionality
    By New2484 in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 07:07 AM
  5. Using Java To Implement RSA Algorithm
    By Floetic in forum New To Java
    Replies: 3
    Last Post: 03-31-2008, 11:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •