Java Maze Problem

Thread Solved

Join Date: Mar 2008
Posts: 12
Reputation: mousey182 is an unknown quantity at this point 
Solved Threads: 0
mousey182 mousey182 is offline Offline
Newbie Poster

Java Maze Problem

 
0
  #1
Mar 27th, 2008
I've tried searching the web and cant find anything to help me. I'm trying to create a 2D java maze application and have come up with the code below.
It generates the maze and GUI no problem, and I've worked the code out to create a xPosition and a yPosition integer.
However, Im trying to get the background colour of the grid layout to change when i click the forward button to 'move' around the maze.
If anyone could have a look and suggest anything I'd really appreciate it!
Thanks
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*; JPanel class
  4. import java.awt.event.*;
  5.  
  6. public class C2D3DGraphicsApp2D extends JFrame implements ActionListener
  7. {
  8. Container yourContainer; //declare container
  9. JMenuItem exitItem, fontItem, forward, rotate, room1, room2, room3, clear, helpItem, aboutItem;
  10. JPanel eastPanel = new JPanel();
  11. JPanel menuArea = new JPanel();
  12. JPanel mazeArea = new JPanel();
  13. JPanel compassArea = new JPanel(); //declare JPanels
  14.  
  15. JButton forwardButton; //declare JButton
  16. JButton rotateButton; //declare JButton
  17. JButton room1Button; //declare JButton
  18. JButton room2Button; //declare JButton
  19. JButton room3Button; //declare JButton
  20. JButton resetButton; //declare JButton
  21. JButton solveButton; //declare JButton
  22. JButton exitButton; //declare JButton
  23.  
  24. JTextField helloTextField; //declare JTextField
  25.  
  26. int [][] imazePlan = {
  27. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0},
  28. {0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,0,0,0,1,0},
  29. {0,1,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,1,0},
  30. {0,0,1,0,0,1,0,1,1,1,1,1,1,1,1,0,0,1,0,0},
  31. {0,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0},
  32. {0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,0,0},
  33. {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0},
  34. {0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,0,0},
  35. {0,1,1,1,1,1,0,0,0,0,0,0,1,0,1,1,1,0,0,0},
  36. {0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,0,0,0},
  37. {0,1,1,1,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1,0},
  38. {0,0,0,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0},
  39. {0,0,0,1,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,0},
  40. {0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,1,1,0},
  41. {0,1,1,1,0,0,0,0,1,1,1,1,0,0,1,0,0,1,0,0},
  42. {0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0},
  43. {0,1,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0},
  44. {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  45. {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  46. {0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  47. };
  48. mazeLayout mlayout = new mazeLayout();
  49. JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length];
  50.  
  51. int xPosition=2;
  52. int yPosition=2;
  53. int xMovement=0;
  54. int yMovement=-1;
  55. int rotation=0;
  56. String direction="North";
  57.  
  58.  
  59. public C2D3DGraphicsApp2D() //could use (String title) then super(title) and declare title.
  60. {
  61. super ("Java 2D/3D Graphics Application"); //set the JFrame title
  62. yourContainer = getContentPane(); // get content pane and name it
  63. yourContainer.setLayout(new BorderLayout()); // use border layout
  64.  
  65.  
  66. eastPanel.setLayout(new BorderLayout());
  67. yourContainer.add(eastPanel, BorderLayout.EAST);
  68.  
  69. controlSetup();
  70. setExtendedState(MAXIMIZED_BOTH);
  71. setVisible(true); //display the JFrame
  72. }
  73.  
  74. public class mazeLayout extends JPanel { //code to create the maze layout
  75. mazeLayout(){
  76. setLayout(new GridLayout(imazePlan.length, imazePlan[0].length));
  77. JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length];
  78. for(int i=0; i<imazePlan.length; i++){
  79. for(int j=0; j<imazePlan[0].length;j++){
  80. wall[i][j] = new JPanel();
  81. if(imazePlan[i][j]==0)
  82. wall[i][j].setBackground(Color.darkGray);
  83. else if(imazePlan[i][j]==2)
  84. wall[i][j].setBackground(Color.GREEN);
  85. else if(imazePlan[i][j]==3)
  86. wall[i][j].setBackground(Color.RED);
  87. else if (imazePlan[i][j]==1)
  88. wall[i][j].setBackground(Color.lightGray);
  89. add(wall[i][j]);
  90. }
  91. }
  92. }
  93. }
  94.  
  95. public void controlSetup() //Code that creates the east Panel layout
  96. {
  97.  
  98. menuArea.setLayout(new GridLayout(0,2)); //set JPanel Layout
  99.  
  100. forwardButton = new JButton("Forward"); // create button
  101. forwardButton.setToolTipText("Move Forward"); // gives a mouse over help / tip.
  102. menuArea.add(forwardButton);
  103. forwardButton.addActionListener(this);
  104.  
  105. rotateButton = new JButton("Rotate"); // create button
  106. rotateButton.setToolTipText("Rotate 90 degrees to the right."); // gives a mouse over help / tip.
  107. menuArea.add(rotateButton);
  108. rotateButton.addActionListener(this);
  109.  
  110. room1Button = new JButton("Room 1"); // create button
  111. room1Button.setToolTipText("Room 1"); // gives a mouse over help / tip.
  112. menuArea.add(room1Button);
  113.  
  114. room2Button = new JButton("Room 2"); // create button
  115. room2Button.setToolTipText("Room 2"); // gives a mouse over help / tip.
  116. menuArea.add(room2Button);
  117.  
  118. room3Button = new JButton("Room 3"); // create button
  119. room3Button.setToolTipText("Room 3"); // gives a mouse over help / tip.
  120. menuArea.add(room3Button);
  121.  
  122. solveButton = new JButton("Solve"); // create button
  123. solveButton.setToolTipText("Solves the maze."); // gives a mouse over help / tip.
  124. menuArea.add(solveButton);
  125.  
  126. resetButton = new JButton("Reset"); // create button
  127. resetButton.setToolTipText("Resets the maze."); // gives a mouse over help / tip.
  128. menuArea.add(resetButton);
  129.  
  130. exitButton = new JButton("Exit"); // create button
  131. exitButton.setToolTipText("Exits the program."); // gives a mouse over help / tip.
  132. exitButton.addActionListener(this);
  133. menuArea.add(exitButton);
  134.  
  135.  
  136. mazeArea.add(mlayout);
  137.  
  138. helloTextField = new JTextField("", 12 ); //create text field to display button response
  139. helloTextField.setEditable( false ); //prevent text field editing
  140.  
  141. compassArea.add( helloTextField, BorderLayout.CENTER); //add text field to container
  142.  
  143. eastPanel.add(compassArea, BorderLayout.CENTER);
  144. eastPanel.add(mazeArea, BorderLayout.NORTH);
  145. eastPanel.add(menuArea, BorderLayout.SOUTH);
  146. }
  147.  
  148.  
  149. public void actionPerformed(ActionEvent event)
  150. {
  151. if (event.getSource() == exitItem)
  152. {
  153. System.exit(0);
  154. }
  155. if (event.getSource() == exitButton)
  156. {
  157. System.exit(0);
  158. }
  159. if (event.getSource() == rotateButton) //"Press") could also be used
  160. {
  161. rotation= ++rotation;
  162. Color color = getBackground();
  163. {
  164. if (rotation==0) //the following is the logic for moving around the maze
  165. {
  166. xMovement=0;
  167. yMovement=-1;
  168. direction="North";
  169. }
  170. else if (rotation==1)
  171. {
  172. xMovement=+1;
  173. yMovement=0;
  174. direction="East";
  175. }
  176. else if (rotation==2)
  177. {
  178. xMovement=0;
  179. yMovement=+1;
  180. direction="South";
  181. }
  182. else if (rotation==3)
  183. {
  184. xMovement=-1;
  185. yMovement=0;
  186. direction="West";
  187. }
  188. else if (rotation > 3)
  189. {
  190. rotation=0;
  191. xMovement=0;
  192. yMovement=-1;
  193. direction="North";
  194. }
  195.  
  196. }
  197. }
  198. if (event.getSource() == forwardButton) //"Press") could also be used
  199. {
  200. //xPosition = xPosition + xMovement;
  201. //yPosition = yPosition + yMovement;
  202. helloTextField.setText(direction); //set the text to this..
  203.  
  204. }
  205.  
  206.  
  207.  
  208. }
  209.  
  210. public static void main(String[] args)
  211. {
  212. try
  213. {
  214. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  215. }
  216. catch (Exception e)
  217. {
  218. System.err.println("Couldn't use the system look and feel: " + e);
  219. }
  220. C2D3DGraphicsApp2D C2D3DGraphicsApp = new C2D3DGraphicsApp2D(); //C2D3DGraphicsApp("Java 2D/3D Graphics Application");
  221. C2D3DGraphicsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way
  222. }
  223. }// end class C2D3DGraphicsApp
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Java Maze Problem

 
0
  #2
Mar 27th, 2008
I think you have a good start. I revised your code a little bit to give your mazeLayout class a paintComponent that can be called from C2D3DGraphicsApp2D's actionPerformed. I made mlayout a data member of C2D3DGraphicsApp2D to facilitate the calling of paintComponent. I added a new 2-D array of type Color called wallColor to mazeLayout. You can change wallColor for a certain box on the maze depending on what you want to do. I put an example at the bottom of actionPerformed that changes color of a box on the maze depending on what direction you are facing. I've added some comments with my initials (VD: yes, I know, ha ha ). I'm sure I forgot to comment at least something, but hopefully this'll help.

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*; // JPanel class
  5. import java.awt.event.*;
  6.  
  7. public class C2D3DGraphicsApp2D extends JFrame implements ActionListener
  8. {
  9.  
  10. Container yourContainer; //declare container
  11. JMenuItem exitItem, fontItem, forward, rotate, room1, room2, room3, clear, helpItem, aboutItem;
  12. JPanel eastPanel = new JPanel();
  13. JPanel menuArea = new JPanel();
  14. JPanel mazeArea = new JPanel();
  15. JPanel compassArea = new JPanel(); //declare JPanels
  16. JButton forwardButton; //declare JButton
  17. JButton rotateButton; //declare JButton
  18. JButton room1Button; //declare JButton
  19. JButton room2Button; //declare JButton
  20. JButton room3Button; //declare JButton
  21. JButton resetButton; //declare JButton
  22. JButton solveButton; //declare JButton
  23. JButton exitButton; //declare JButton
  24. JTextField helloTextField; //declare JTextField
  25. mazeLayout mlayout; // put this as part of C2D3DGraphicsApp2D:VD
  26. int[][] imazePlan = {
  27. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0},
  28. {0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0},
  29. {0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0},
  30. {0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0},
  31. {0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
  32. {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
  33. {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  34. {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0},
  35. {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0},
  36. {0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0},
  37. {0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0},
  38. {0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0},
  39. {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0},
  40. {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
  41. {0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0},
  42. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0},
  43. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0},
  44. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  45. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  46. {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  47.  
  48.  
  49. };
  50.  
  51. JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length];
  52. int xPosition = 2;
  53. int yPosition = 2;
  54. int xMovement = 0;
  55. int yMovement = -1;
  56. int rotation = 0;
  57. String direction = "North";
  58.  
  59. public C2D3DGraphicsApp2D() //could use (String title) then super(title) and declare title.
  60. {
  61.  
  62. super("Java 2D/3D Graphics Application"); //set the JFrame title
  63.  
  64. mlayout = new mazeLayout();
  65.  
  66. yourContainer = getContentPane(); // get content pane and name it
  67. yourContainer.setLayout(new BorderLayout()); // use border layout
  68.  
  69.  
  70. eastPanel.setLayout(new BorderLayout());
  71. yourContainer.add(eastPanel, BorderLayout.EAST);
  72.  
  73. controlSetup();
  74. setExtendedState(MAXIMIZED_BOTH);
  75. setVisible(true); //display the JFrame
  76. }
  77.  
  78. public class mazeLayout extends JPanel
  79. { //code to create the maze layout
  80.  
  81. Color[][] wallColor; // added this:VD
  82. JPanel[][] wall; // placed this outside of constructor:VD
  83.  
  84. mazeLayout()
  85. {
  86. setLayout(new GridLayout(imazePlan.length, imazePlan[0].length));
  87. wall = new JPanel[imazePlan.length][imazePlan[0].length];
  88. wallColor = new Color[imazePlan.length][imazePlan[0].length];
  89. for (int i = 0; i < imazePlan.length; i++)
  90. {
  91. for (int j = 0; j < imazePlan[0].length; j++)
  92. {
  93. wall[i][j] = new JPanel();
  94. if (imazePlan[i][j] == 0)
  95. {
  96. wallColor[i][j] = Color.darkGray;
  97. // wall[i][j].setBackground(Color.darkGray);
  98. }
  99. else
  100. {
  101. if (imazePlan[i][j] == 2)
  102. {
  103. wallColor[i][j] = Color.GREEN;
  104. // wall[i][j].setBackground(Color.GREEN);
  105. }
  106. else
  107. {
  108. if (imazePlan[i][j] == 3)
  109. {
  110. wallColor[i][j] = Color.RED;
  111. // wall[i][j].setBackground(Color.RED);
  112. }
  113. else
  114. {
  115. if (imazePlan[i][j] == 1)
  116. {
  117. wallColor[i][j] = Color.lightGray;
  118. // wall[i][j].setBackground(Color.lightGray);
  119. }
  120. }
  121. }
  122. }
  123. add(wall[i][j]);
  124. }
  125. }
  126. }
  127.  
  128. public void paintComponent(Graphics g)
  129. {
  130. Graphics2D g2 = (Graphics2D) g;
  131. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  132. for (int i = 0; i < imazePlan.length; i++)
  133. {
  134. for (int j = 0; j < imazePlan[0].length; j++)
  135. {
  136. wall[i][j].setBackground(wallColor[i][j]);
  137. }
  138. }
  139. }
  140. }
  141.  
  142. public void controlSetup() //Code that creates the east Panel layout
  143. {
  144.  
  145. menuArea.setLayout(new GridLayout(0, 2)); //set JPanel Layout
  146.  
  147. forwardButton = new JButton("Forward"); // create button
  148. forwardButton.setToolTipText("Move Forward"); // gives a mouse over help / tip.
  149. menuArea.add(forwardButton);
  150. forwardButton.addActionListener(this);
  151.  
  152. rotateButton = new JButton("Rotate"); // create button
  153. rotateButton.setToolTipText("Rotate 90 degrees to the right."); // gives a mouse over help / tip.
  154. menuArea.add(rotateButton);
  155. rotateButton.addActionListener(this);
  156.  
  157. room1Button = new JButton("Room 1"); // create button
  158. room1Button.setToolTipText("Room 1"); // gives a mouse over help / tip.
  159. menuArea.add(room1Button);
  160.  
  161. room2Button = new JButton("Room 2"); // create button
  162. room2Button.setToolTipText("Room 2"); // gives a mouse over help / tip.
  163. menuArea.add(room2Button);
  164.  
  165. room3Button = new JButton("Room 3"); // create button
  166. room3Button.setToolTipText("Room 3"); // gives a mouse over help / tip.
  167. menuArea.add(room3Button);
  168.  
  169. solveButton = new JButton("Solve"); // create button
  170. solveButton.setToolTipText("Solves the maze."); // gives a mouse over help / tip.
  171. menuArea.add(solveButton);
  172.  
  173. resetButton = new JButton("Reset"); // create button
  174. resetButton.setToolTipText("Resets the maze."); // gives a mouse over help / tip.
  175. menuArea.add(resetButton);
  176.  
  177. exitButton = new JButton("Exit"); // create button
  178. exitButton.setToolTipText("Exits the program."); // gives a mouse over help / tip.
  179. exitButton.addActionListener(this);
  180. menuArea.add(exitButton);
  181.  
  182.  
  183. mazeArea.add(mlayout);
  184.  
  185. helloTextField = new JTextField("", 12); //create text field to display button response
  186. helloTextField.setEditable(false); //prevent text field editing
  187.  
  188. compassArea.add(helloTextField, BorderLayout.CENTER); //add text field to container
  189.  
  190. eastPanel.add(compassArea, BorderLayout.CENTER);
  191. eastPanel.add(mazeArea, BorderLayout.NORTH);
  192. eastPanel.add(menuArea, BorderLayout.SOUTH);
  193. }
  194.  
  195. public void actionPerformed(ActionEvent event)
  196. {
  197. if (event.getSource() == exitItem)
  198. {
  199. System.exit(0);
  200. }
  201. if (event.getSource() == exitButton)
  202. {
  203. System.exit(0);
  204. }
  205. if (event.getSource() == rotateButton) //"Press") could also be used
  206. {
  207. rotation = ++rotation;
  208. Color color = getBackground();
  209. {
  210. if (rotation == 0) //the following is the logic for moving around the maze
  211. {
  212. xMovement = 0;
  213. yMovement = -1;
  214. direction = "North";
  215. }
  216. else
  217. {
  218. if (rotation == 1)
  219. {
  220. xMovement = +1;
  221. yMovement = 0;
  222. direction = "East";
  223. }
  224. else
  225. {
  226. if (rotation == 2)
  227. {
  228. xMovement = 0;
  229. yMovement = +1;
  230. direction = "South";
  231. }
  232. else
  233. {
  234. if (rotation == 3)
  235. {
  236. xMovement = -1;
  237. yMovement = 0;
  238. direction = "West";
  239. }
  240. else
  241. {
  242. if (rotation > 3)
  243. {
  244. rotation = 0;
  245. xMovement = 0;
  246. yMovement = -1;
  247. direction = "North";
  248. }
  249. }
  250. }
  251. }
  252. }
  253.  
  254. }
  255. }
  256. if (event.getSource() == forwardButton) //"Press") could also be used
  257. {
  258. //xPosition = xPosition + xMovement;
  259. //yPosition = yPosition + yMovement;
  260. helloTextField.setText(direction); //set the text to this..
  261. }
  262.  
  263.  
  264. // added code below as example:VD
  265. if (xMovement == 0)
  266. {
  267. mlayout.wallColor[xPosition][yPosition] = Color.blue;
  268. }
  269. else
  270. {
  271. mlayout.wallColor[xPosition][yPosition] = Color.orange;
  272. }
  273. mlayout.repaint();
  274. // added code above:VD
  275. }
  276.  
  277. public static void main(String[] args)
  278. {
  279. try
  280. {
  281. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  282. } catch (Exception e)
  283. {
  284. System.err.println("Couldn't use the system look and feel: " + e);
  285. }
  286. C2D3DGraphicsApp2D C2D3DGraphicsApp = new C2D3DGraphicsApp2D(); //C2D3DGraphicsApp("Java 2D/3D Graphics Application");
  287. C2D3DGraphicsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way
  288. }
  289. }// end class C2D3DGraphicsApp
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: mousey182 is an unknown quantity at this point 
Solved Threads: 0
mousey182 mousey182 is offline Offline
Newbie Poster

Re: Java Maze Problem

 
0
  #3
Mar 27th, 2008
That is fantastic!!
Thank you so very much, thats been bugging me for days! I've altered it slightly so that my code now looks like the code below. Now all is left is to add some inteligence to stop the user from moving outside the walls.

Would the best way to do this to perform some sort of check before the action performed command below?
Action Performed Code:
  1. if (event.getSource() == forwardButton)
  2. xPosition = xPosition + xMovement;
  3. yPosition = yPosition + yMovement;
  4. helloTextField.setText(direction); //set the text to this..
  5. mlayout.wallColor[xPosition][yPosition] = Color.blue;
  6. mlayout.repaint();
  7. }

New Maze Code:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*; // JPanel class
  4. import java.awt.event.*;
  5.  
  6. public class maze extends JFrame implements ActionListener
  7. {
  8.  
  9. Container yourContainer; //declare container
  10. JMenuItem exitItem, fontItem, forward, rotate, room1, room2, room3, clear, helpItem, aboutItem;
  11. JPanel eastPanel = new JPanel();
  12. JPanel menuArea = new JPanel();
  13. JPanel mazeArea = new JPanel();
  14. JPanel compassArea = new JPanel(); //declare JPanels
  15. JButton forwardButton; //declare JButton
  16. JButton rotateButton; //declare JButton
  17. JButton room1Button; //declare JButton
  18. JButton room2Button; //declare JButton
  19. JButton room3Button; //declare JButton
  20. JButton resetButton; //declare JButton
  21. JButton solveButton; //declare JButton
  22. JButton exitButton; //declare JButton
  23. JTextField helloTextField; //declare JTextField
  24. mazeLayout mlayout; // put this as part of maze:VD
  25. int[][] imazePlan = {
  26. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0},
  27. {0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0},
  28. {0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0},
  29. {0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0},
  30. {0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
  31. {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
  32. {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  33. {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0},
  34. {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0},
  35. {0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0},
  36. {0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0},
  37. {0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0},
  38. {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0},
  39. {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
  40. {0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0},
  41. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0},
  42. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0},
  43. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  44. {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  45. {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  46.  
  47.  
  48. };
  49.  
  50. JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length];
  51. int xPosition = 19;
  52. int yPosition = 1;
  53. int xMovement = -1;
  54. int yMovement = 0;
  55. int rotation = 0;
  56. String direction = "North";
  57.  
  58. public maze() //could use (String title) then super(title) and declare title.
  59. {
  60.  
  61. super("Java 2D/3D Graphics Application"); //set the JFrame title
  62.  
  63. mlayout = new mazeLayout();
  64.  
  65. yourContainer = getContentPane(); // get content pane and name it
  66. yourContainer.setLayout(new BorderLayout()); // use border layout
  67.  
  68.  
  69. eastPanel.setLayout(new BorderLayout());
  70. yourContainer.add(eastPanel, BorderLayout.EAST);
  71.  
  72. controlSetup();
  73. setExtendedState(MAXIMIZED_BOTH);
  74. setVisible(true); //display the JFrame
  75. }
  76.  
  77. public class mazeLayout extends JPanel
  78. { //code to create the maze layout
  79.  
  80. Color[][] wallColor; // added this:VD
  81. JPanel[][] wall; // placed this outside of constructor:VD
  82.  
  83. mazeLayout()
  84. {
  85. setLayout(new GridLayout(imazePlan.length, imazePlan[0].length));
  86. wall = new JPanel[imazePlan.length][imazePlan[0].length];
  87. wallColor = new Color[imazePlan.length][imazePlan[0].length];
  88. for (int i = 0; i < imazePlan.length; i++)
  89. {
  90. for (int j = 0; j < imazePlan[0].length; j++)
  91. {
  92. wall[i][j] = new JPanel();
  93. if (imazePlan[i][j] == 0)
  94. {
  95. wallColor[i][j] = Color.darkGray;
  96. // wall[i][j].setBackground(Color.darkGray);
  97. }
  98. else
  99. {
  100. if (imazePlan[i][j] == 2)
  101. {
  102. wallColor[i][j] = Color.GREEN;
  103. // wall[i][j].setBackground(Color.GREEN);
  104. }
  105. else
  106. {
  107. if (imazePlan[i][j] == 3)
  108. {
  109. wallColor[i][j] = Color.RED;
  110. // wall[i][j].setBackground(Color.RED);
  111. }
  112. else
  113. {
  114. if (imazePlan[i][j] == 1)
  115. {
  116. wallColor[i][j] = Color.lightGray;
  117. // wall[i][j].setBackground(Color.lightGray);
  118. }
  119. }
  120. }
  121. }
  122. add(wall[i][j]);
  123. }
  124. }
  125. }
  126.  
  127. public void paintComponent(Graphics g)
  128. {
  129. Graphics2D g2 = (Graphics2D) g;
  130. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  131. for (int i = 0; i < imazePlan.length; i++)
  132. {
  133. for (int j = 0; j < imazePlan[0].length; j++)
  134. {
  135. wall[i][j].setBackground(wallColor[i][j]);
  136. }
  137. }
  138. }
  139. }
  140.  
  141. public void controlSetup() //Code that creates the east Panel layout
  142. {
  143.  
  144. menuArea.setLayout(new GridLayout(0, 2)); //set JPanel Layout
  145.  
  146. forwardButton = new JButton("Forward"); // create button
  147. forwardButton.setToolTipText("Move Forward"); // gives a mouse over help / tip.
  148. menuArea.add(forwardButton);
  149. forwardButton.addActionListener(this);
  150.  
  151. rotateButton = new JButton("Rotate"); // create button
  152. rotateButton.setToolTipText("Rotate 90 degrees to the right."); // gives a mouse over help / tip.
  153. menuArea.add(rotateButton);
  154. rotateButton.addActionListener(this);
  155.  
  156. room1Button = new JButton("Room 1"); // create button
  157. room1Button.setToolTipText("Room 1"); // gives a mouse over help / tip.
  158. menuArea.add(room1Button);
  159.  
  160. room2Button = new JButton("Room 2"); // create button
  161. room2Button.setToolTipText("Room 2"); // gives a mouse over help / tip.
  162. menuArea.add(room2Button);
  163.  
  164. room3Button = new JButton("Room 3"); // create button
  165. room3Button.setToolTipText("Room 3"); // gives a mouse over help / tip.
  166. menuArea.add(room3Button);
  167.  
  168. solveButton = new JButton("Solve"); // create button
  169. solveButton.setToolTipText("Solves the maze."); // gives a mouse over help / tip.
  170. menuArea.add(solveButton);
  171.  
  172. resetButton = new JButton("Reset"); // create button
  173. resetButton.setToolTipText("Resets the maze."); // gives a mouse over help / tip.
  174. menuArea.add(resetButton);
  175.  
  176. exitButton = new JButton("Exit"); // create button
  177. exitButton.setToolTipText("Exits the program."); // gives a mouse over help / tip.
  178. exitButton.addActionListener(this);
  179. menuArea.add(exitButton);
  180.  
  181.  
  182. mazeArea.add(mlayout);
  183.  
  184. helloTextField = new JTextField("", 12); //create text field to display button response
  185. helloTextField.setEditable(false); //prevent text field editing
  186.  
  187. compassArea.add(helloTextField, BorderLayout.CENTER); //add text field to container
  188.  
  189. eastPanel.add(compassArea, BorderLayout.CENTER);
  190. eastPanel.add(mazeArea, BorderLayout.NORTH);
  191. eastPanel.add(menuArea, BorderLayout.SOUTH);
  192. }
  193.  
  194. public void actionPerformed(ActionEvent event)
  195. {
  196. if (event.getSource() == exitItem)
  197. {
  198. System.exit(0);
  199. }
  200. if (event.getSource() == exitButton)
  201. {
  202. System.exit(0);
  203. }
  204. if (event.getSource() == rotateButton) //"Press") could also be used
  205. {
  206. rotation = ++rotation;
  207. {
  208. if (rotation == 0) //the following is the logic for moving around the maze
  209. {
  210. xMovement = -1;
  211. yMovement = 0;
  212. direction = "North";
  213. helloTextField.setText(direction); //set the text to this..
  214. }
  215. else
  216. {
  217. if (rotation == 1)
  218. {
  219. xMovement = 0;
  220. yMovement = +1;
  221. direction = "East";
  222. helloTextField.setText(direction); //set the text to this..
  223.  
  224. }
  225. else
  226. {
  227. if (rotation == 2)
  228. {
  229. xMovement = +1;
  230. yMovement = 0;
  231. direction = "South";
  232. helloTextField.setText(direction); //set the text to this..
  233. }
  234. else
  235. {
  236. if (rotation == 3)
  237. {
  238. xMovement = 0;
  239. yMovement = -1;
  240. direction = "West";
  241. helloTextField.setText(direction); //set the text to this..
  242. }
  243. else
  244. {
  245. if (rotation > 3)
  246. {
  247. rotation = 0;
  248. xMovement = -1;
  249. yMovement = 0;
  250. direction = "North";
  251. helloTextField.setText(direction); //set the text to this..
  252. }
  253. }
  254. }
  255. }
  256. }
  257.  
  258. }
  259. }
  260. if (event.getSource() == forwardButton)
  261. xPosition = xPosition + xMovement;
  262. yPosition = yPosition + yMovement;
  263. helloTextField.setText(direction); //set the text to this..
  264. mlayout.wallColor[xPosition][yPosition] = Color.blue;
  265. mlayout.repaint();
  266. }
  267.  
  268.  
  269. public static void main(String[] args)
  270. {
  271. try
  272. {
  273. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  274. } catch (Exception e)
  275. {
  276. System.err.println("Couldn't use the system look and feel: " + e);
  277. }
  278. maze C2D3DGraphicsApp = new maze(); //C2D3DGraphicsApp("Java 2D/3D Graphics Application");
  279. C2D3DGraphicsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way
  280. }
  281. }// end class C2D3DGraphicsApp
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Java Maze Problem

 
0
  #4
Mar 27th, 2008
Originally Posted by mousey182 View Post
That is fantastic!!
Thank you so very much, thats been bugging me for days! I've altered it slightly so that my code now looks like the code below. Now all is left is to add some inteligence to stop the user from moving outside the walls.

Would the best way to do this to perform some sort of check before the action performed command below?
Action Performed Code:
  1. if (event.getSource() == forwardButton)
  2. xPosition = xPosition + xMovement;
  3. yPosition = yPosition + yMovement;
  4. helloTextField.setText(direction); //set the text to this..
  5. mlayout.wallColor[xPosition][yPosition] = Color.blue;
  6. mlayout.repaint();
  7. }
Yes, I think a check would be a good idea. You know where you are now, you know the direction you are facing. Calculate the coordinates of where you WILL BE after the step is taken if there is no wall, but don't change xPosition and yPosition yet until you know that you are NOT going to run into a wall. So I would create a couple of new variables called newX and newY. Put in them the coordinates of where you will end up if the step is taken. Check the color of the box at those coordinates. A certain color (light grey? I don't recall) represents a wall. If the new coordinates are not that color, it's a legal move as far as I can tell. So change the color of mlayout.wallColor[newX][newY] to be the color of the person in the maze (green?). Change the color of the box at the current (xPosition,yPosition) back to whatever color it was that represents the floor with no one standing on it (dark gray?). Then change xPosition to newX and yPosition to newY and call mlayout.repaint().

If you DID hit a wall, the move is illegal so don't do any of this. Just return from actionPerformed without changing any variable values, any wall colors, and without repainting.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: mousey182 is an unknown quantity at this point 
Solved Threads: 0
mousey182 mousey182 is offline Offline
Newbie Poster

Re: Java Maze Problem

 
0
  #5
Mar 27th, 2008
Originally Posted by VernonDozier View Post
Yes, I think a check would be a good idea. You know where you are now, you know the direction you are facing. Calculate the coordinates of where you WILL BE after the step is taken if there is no wall, but don't change xPosition and yPosition yet until you know that you are NOT going to run into a wall. So I would create a couple of new variables called newX and newY. Put in them the coordinates of where you will end up if the step is taken. Check the color of the box at those coordinates. A certain color (light grey? I don't recall) represents a wall. If the new coordinates are not that color, it's a legal move as far as I can tell. So change the color of mlayout.wallColor[newX][newY] to be the color of the person in the maze (green?). Change the color of the box at the current (xPosition,yPosition) back to whatever color it was that represents the floor with no one standing on it (dark gray?). Then change xPosition to newX and yPosition to newY and call mlayout.repaint().

If you DID hit a wall, the move is illegal so don't do any of this. Just return from actionPerformed without changing any variable values, any wall colors, and without repainting.

Thats Brilliant!!
Thank you so much for your help...have got me out of a right hole there!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: mousey182 is an unknown quantity at this point 
Solved Threads: 0
mousey182 mousey182 is offline Offline
Newbie Poster

Re: Java Maze Problem

 
0
  #6
Mar 28th, 2008
Hello!
Me again!
lol!
Ive tried working on my solve maze algorithm and can see no reason why its not working!
The following loops should, as far as I can see 'solve' the maze.
Any ideas??
Sorry to be a hassle!

  1. if (event.getSource() == solveButton)
  2. {
  3. int i=1;
  4. if (mlayout.wallColor[xPosition][yPosition] != Color.RED){
  5. while (mlayout.wallColor[xPosition-i][yPosition] == Color.LIGHT_GRAY)
  6. {
  7. newXPosition=xPosition-1;
  8. mlayout.wallColor[newXPosition][yPosition] = Color.BLUE;
  9. xPosition=newXPosition;
  10. mlayout.repaint();
  11. }
  12. while (mlayout.wallColor[xPosition][yPosition+i] == Color.LIGHT_GRAY)
  13. {
  14. newYPosition=yPosition+1;
  15. mlayout.wallColor[xPosition][newYPosition] = Color.BLUE;
  16. yPosition=newYPosition;
  17. mlayout.repaint();
  18. }
  19. while (mlayout.wallColor[xPosition+i][yPosition] == Color.LIGHT_GRAY)
  20. {
  21. newXPosition=xPosition+1;
  22. mlayout.wallColor[newXPosition][yPosition] = Color.BLUE;
  23. xPosition=newXPosition;
  24. mlayout.repaint();
  25. }
  26. while (mlayout.wallColor[xPosition][yPosition-i] == Color.LIGHT_GRAY)
  27. {
  28. newYPosition=yPosition-1;
  29. mlayout.wallColor[xPosition][newYPosition] = Color.BLUE;
  30. yPosition=newYPosition;
  31. mlayout.repaint();
  32. }
  33. }
  34.  
  35. }
  36. }
Last edited by mousey182; Mar 28th, 2008 at 2:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Java Maze Problem

 
0
  #7
Mar 28th, 2008
Not sure if your updated code does this already, but I don't see an actionListener ever added for solveButton from the code in post 3.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: mousey182 is an unknown quantity at this point 
Solved Threads: 0
mousey182 mousey182 is offline Offline
Newbie Poster

Re: Java Maze Problem

 
0
  #8
Mar 28th, 2008
Sorry! I should have said!
Ive added the action listener, and altered the code so that only a single blue square moves around the maze.
When the solve button is clicked, the line runs up to the first corner, turns right then runs to the next corner and stops. Ive got no idea why it wont carry on round the maze!
Is there anything obvious I'm missing?
Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Java Maze Problem

 
0
  #9
Mar 28th, 2008
Took me forever to spot this, but that's OK, I like a good puzzle. One, if you consistently hit "Solve", it gets closer and closer. Change the top "if" statement within your "solveButton" from an "if" to a "while". This'll keep going through the loop till it solves it, like this:
  1. while (mlayout.wallColor[xPosition][yPosition] != Color.RED)
Two, and more important, are your four "while" conditions within this larger "if" statement above ("while" after you change it):
  1. while (mlayout.wallColor[xPosition + i][yPosition] == Color.LIGHT_GRAY)
The above is an example. The other three are like it. None of them look for a red square. You are solving the maze all the way EXCEPT for the last step (when you change the top "if" to a "while"). You need to look out for a red square as well as a light gray square in order to make that final step to the finish line.

Finally, since you are looking for a red square to get out of that while loop and call the maze solved, make sure that when you DO take that final step, don't change that last box to blue.

That was aggravating, but rewarding!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 12
Reputation: mousey182 is an unknown quantity at this point 
Solved Threads: 0
mousey182 mousey182 is offline Offline
Newbie Poster

Re: Java Maze Problem

 
0
  #10
Mar 29th, 2008
Hi,
Thanks again for the post! I cant tell you how I appreciate you helping me.
I changed the top if statement to a while statement, but when clicked, it doesn't do anything and actually crashes my GUI.
The while statement was how I originally tried it, but faced this exact problem. Using the if statement was the only way I could get it to solve any portion of the maze.
I didn't notice however that if you keep clicking the solve button it eventually solves the maze.
So I've tried looping the if statement in a for loop and a while loop but to no avail!!
I cant see why this isnt working, as far as I can tell, as you suggested, using the while loop should work!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC