| | |
Java Maze Problem
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; JPanel class import java.awt.event.*; public class C2D3DGraphicsApp2D extends JFrame implements ActionListener { Container yourContainer; //declare container JMenuItem exitItem, fontItem, forward, rotate, room1, room2, room3, clear, helpItem, aboutItem; JPanel eastPanel = new JPanel(); JPanel menuArea = new JPanel(); JPanel mazeArea = new JPanel(); JPanel compassArea = new JPanel(); //declare JPanels JButton forwardButton; //declare JButton JButton rotateButton; //declare JButton JButton room1Button; //declare JButton JButton room2Button; //declare JButton JButton room3Button; //declare JButton JButton resetButton; //declare JButton JButton solveButton; //declare JButton JButton exitButton; //declare JButton JTextField helloTextField; //declare JTextField int [][] imazePlan = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,0,0,0,1,0}, {0,1,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0,1,1,0}, {0,0,1,0,0,1,0,1,1,1,1,1,1,1,1,0,0,1,0,0}, {0,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,0,0}, {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,0,0}, {0,1,1,1,1,1,0,0,0,0,0,0,1,0,1,1,1,0,0,0}, {0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,0,0,0}, {0,1,1,1,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1,0}, {0,0,0,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0}, {0,0,0,1,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,0}, {0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,1,1,0}, {0,1,1,1,0,0,0,0,1,1,1,1,0,0,1,0,0,1,0,0}, {0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0}, {0,1,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0}, {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, }; mazeLayout mlayout = new mazeLayout(); JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length]; int xPosition=2; int yPosition=2; int xMovement=0; int yMovement=-1; int rotation=0; String direction="North"; public C2D3DGraphicsApp2D() //could use (String title) then super(title) and declare title. { super ("Java 2D/3D Graphics Application"); //set the JFrame title yourContainer = getContentPane(); // get content pane and name it yourContainer.setLayout(new BorderLayout()); // use border layout eastPanel.setLayout(new BorderLayout()); yourContainer.add(eastPanel, BorderLayout.EAST); controlSetup(); setExtendedState(MAXIMIZED_BOTH); setVisible(true); //display the JFrame } public class mazeLayout extends JPanel { //code to create the maze layout mazeLayout(){ setLayout(new GridLayout(imazePlan.length, imazePlan[0].length)); JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length]; for(int i=0; i<imazePlan.length; i++){ for(int j=0; j<imazePlan[0].length;j++){ wall[i][j] = new JPanel(); if(imazePlan[i][j]==0) wall[i][j].setBackground(Color.darkGray); else if(imazePlan[i][j]==2) wall[i][j].setBackground(Color.GREEN); else if(imazePlan[i][j]==3) wall[i][j].setBackground(Color.RED); else if (imazePlan[i][j]==1) wall[i][j].setBackground(Color.lightGray); add(wall[i][j]); } } } } public void controlSetup() //Code that creates the east Panel layout { menuArea.setLayout(new GridLayout(0,2)); //set JPanel Layout forwardButton = new JButton("Forward"); // create button forwardButton.setToolTipText("Move Forward"); // gives a mouse over help / tip. menuArea.add(forwardButton); forwardButton.addActionListener(this); rotateButton = new JButton("Rotate"); // create button rotateButton.setToolTipText("Rotate 90 degrees to the right."); // gives a mouse over help / tip. menuArea.add(rotateButton); rotateButton.addActionListener(this); room1Button = new JButton("Room 1"); // create button room1Button.setToolTipText("Room 1"); // gives a mouse over help / tip. menuArea.add(room1Button); room2Button = new JButton("Room 2"); // create button room2Button.setToolTipText("Room 2"); // gives a mouse over help / tip. menuArea.add(room2Button); room3Button = new JButton("Room 3"); // create button room3Button.setToolTipText("Room 3"); // gives a mouse over help / tip. menuArea.add(room3Button); solveButton = new JButton("Solve"); // create button solveButton.setToolTipText("Solves the maze."); // gives a mouse over help / tip. menuArea.add(solveButton); resetButton = new JButton("Reset"); // create button resetButton.setToolTipText("Resets the maze."); // gives a mouse over help / tip. menuArea.add(resetButton); exitButton = new JButton("Exit"); // create button exitButton.setToolTipText("Exits the program."); // gives a mouse over help / tip. exitButton.addActionListener(this); menuArea.add(exitButton); mazeArea.add(mlayout); helloTextField = new JTextField("", 12 ); //create text field to display button response helloTextField.setEditable( false ); //prevent text field editing compassArea.add( helloTextField, BorderLayout.CENTER); //add text field to container eastPanel.add(compassArea, BorderLayout.CENTER); eastPanel.add(mazeArea, BorderLayout.NORTH); eastPanel.add(menuArea, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent event) { if (event.getSource() == exitItem) { System.exit(0); } if (event.getSource() == exitButton) { System.exit(0); } if (event.getSource() == rotateButton) //"Press") could also be used { rotation= ++rotation; Color color = getBackground(); { if (rotation==0) //the following is the logic for moving around the maze { xMovement=0; yMovement=-1; direction="North"; } else if (rotation==1) { xMovement=+1; yMovement=0; direction="East"; } else if (rotation==2) { xMovement=0; yMovement=+1; direction="South"; } else if (rotation==3) { xMovement=-1; yMovement=0; direction="West"; } else if (rotation > 3) { rotation=0; xMovement=0; yMovement=-1; direction="North"; } } } if (event.getSource() == forwardButton) //"Press") could also be used { //xPosition = xPosition + xMovement; //yPosition = yPosition + yMovement; helloTextField.setText(direction); //set the text to this.. } } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.err.println("Couldn't use the system look and feel: " + e); } C2D3DGraphicsApp2D C2D3DGraphicsApp = new C2D3DGraphicsApp2D(); //C2D3DGraphicsApp("Java 2D/3D Graphics Application"); C2D3DGraphicsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way } }// end class C2D3DGraphicsApp
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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.
). I'm sure I forgot to comment at least something, but hopefully this'll help. JAVA Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; // JPanel class import java.awt.event.*; public class C2D3DGraphicsApp2D extends JFrame implements ActionListener { Container yourContainer; //declare container JMenuItem exitItem, fontItem, forward, rotate, room1, room2, room3, clear, helpItem, aboutItem; JPanel eastPanel = new JPanel(); JPanel menuArea = new JPanel(); JPanel mazeArea = new JPanel(); JPanel compassArea = new JPanel(); //declare JPanels JButton forwardButton; //declare JButton JButton rotateButton; //declare JButton JButton room1Button; //declare JButton JButton room2Button; //declare JButton JButton room3Button; //declare JButton JButton resetButton; //declare JButton JButton solveButton; //declare JButton JButton exitButton; //declare JButton JTextField helloTextField; //declare JTextField mazeLayout mlayout; // put this as part of C2D3DGraphicsApp2D:VD int[][] imazePlan = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0}, {0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0}, {0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, {0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length]; int xPosition = 2; int yPosition = 2; int xMovement = 0; int yMovement = -1; int rotation = 0; String direction = "North"; public C2D3DGraphicsApp2D() //could use (String title) then super(title) and declare title. { super("Java 2D/3D Graphics Application"); //set the JFrame title mlayout = new mazeLayout(); yourContainer = getContentPane(); // get content pane and name it yourContainer.setLayout(new BorderLayout()); // use border layout eastPanel.setLayout(new BorderLayout()); yourContainer.add(eastPanel, BorderLayout.EAST); controlSetup(); setExtendedState(MAXIMIZED_BOTH); setVisible(true); //display the JFrame } public class mazeLayout extends JPanel { //code to create the maze layout Color[][] wallColor; // added this:VD JPanel[][] wall; // placed this outside of constructor:VD mazeLayout() { setLayout(new GridLayout(imazePlan.length, imazePlan[0].length)); wall = new JPanel[imazePlan.length][imazePlan[0].length]; wallColor = new Color[imazePlan.length][imazePlan[0].length]; for (int i = 0; i < imazePlan.length; i++) { for (int j = 0; j < imazePlan[0].length; j++) { wall[i][j] = new JPanel(); if (imazePlan[i][j] == 0) { wallColor[i][j] = Color.darkGray; // wall[i][j].setBackground(Color.darkGray); } else { if (imazePlan[i][j] == 2) { wallColor[i][j] = Color.GREEN; // wall[i][j].setBackground(Color.GREEN); } else { if (imazePlan[i][j] == 3) { wallColor[i][j] = Color.RED; // wall[i][j].setBackground(Color.RED); } else { if (imazePlan[i][j] == 1) { wallColor[i][j] = Color.lightGray; // wall[i][j].setBackground(Color.lightGray); } } } } add(wall[i][j]); } } } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (int i = 0; i < imazePlan.length; i++) { for (int j = 0; j < imazePlan[0].length; j++) { wall[i][j].setBackground(wallColor[i][j]); } } } } public void controlSetup() //Code that creates the east Panel layout { menuArea.setLayout(new GridLayout(0, 2)); //set JPanel Layout forwardButton = new JButton("Forward"); // create button forwardButton.setToolTipText("Move Forward"); // gives a mouse over help / tip. menuArea.add(forwardButton); forwardButton.addActionListener(this); rotateButton = new JButton("Rotate"); // create button rotateButton.setToolTipText("Rotate 90 degrees to the right."); // gives a mouse over help / tip. menuArea.add(rotateButton); rotateButton.addActionListener(this); room1Button = new JButton("Room 1"); // create button room1Button.setToolTipText("Room 1"); // gives a mouse over help / tip. menuArea.add(room1Button); room2Button = new JButton("Room 2"); // create button room2Button.setToolTipText("Room 2"); // gives a mouse over help / tip. menuArea.add(room2Button); room3Button = new JButton("Room 3"); // create button room3Button.setToolTipText("Room 3"); // gives a mouse over help / tip. menuArea.add(room3Button); solveButton = new JButton("Solve"); // create button solveButton.setToolTipText("Solves the maze."); // gives a mouse over help / tip. menuArea.add(solveButton); resetButton = new JButton("Reset"); // create button resetButton.setToolTipText("Resets the maze."); // gives a mouse over help / tip. menuArea.add(resetButton); exitButton = new JButton("Exit"); // create button exitButton.setToolTipText("Exits the program."); // gives a mouse over help / tip. exitButton.addActionListener(this); menuArea.add(exitButton); mazeArea.add(mlayout); helloTextField = new JTextField("", 12); //create text field to display button response helloTextField.setEditable(false); //prevent text field editing compassArea.add(helloTextField, BorderLayout.CENTER); //add text field to container eastPanel.add(compassArea, BorderLayout.CENTER); eastPanel.add(mazeArea, BorderLayout.NORTH); eastPanel.add(menuArea, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent event) { if (event.getSource() == exitItem) { System.exit(0); } if (event.getSource() == exitButton) { System.exit(0); } if (event.getSource() == rotateButton) //"Press") could also be used { rotation = ++rotation; Color color = getBackground(); { if (rotation == 0) //the following is the logic for moving around the maze { xMovement = 0; yMovement = -1; direction = "North"; } else { if (rotation == 1) { xMovement = +1; yMovement = 0; direction = "East"; } else { if (rotation == 2) { xMovement = 0; yMovement = +1; direction = "South"; } else { if (rotation == 3) { xMovement = -1; yMovement = 0; direction = "West"; } else { if (rotation > 3) { rotation = 0; xMovement = 0; yMovement = -1; direction = "North"; } } } } } } } if (event.getSource() == forwardButton) //"Press") could also be used { //xPosition = xPosition + xMovement; //yPosition = yPosition + yMovement; helloTextField.setText(direction); //set the text to this.. } // added code below as example:VD if (xMovement == 0) { mlayout.wallColor[xPosition][yPosition] = Color.blue; } else { mlayout.wallColor[xPosition][yPosition] = Color.orange; } mlayout.repaint(); // added code above:VD } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.err.println("Couldn't use the system look and feel: " + e); } C2D3DGraphicsApp2D C2D3DGraphicsApp = new C2D3DGraphicsApp2D(); //C2D3DGraphicsApp("Java 2D/3D Graphics Application"); C2D3DGraphicsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way } }// end class C2D3DGraphicsApp
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Solved Threads: 0
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:
New Maze Code:
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:
Java Syntax (Toggle Plain Text)
if (event.getSource() == forwardButton) xPosition = xPosition + xMovement; yPosition = yPosition + yMovement; helloTextField.setText(direction); //set the text to this.. mlayout.wallColor[xPosition][yPosition] = Color.blue; mlayout.repaint(); }
New Maze Code:
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; // JPanel class import java.awt.event.*; public class maze extends JFrame implements ActionListener { Container yourContainer; //declare container JMenuItem exitItem, fontItem, forward, rotate, room1, room2, room3, clear, helpItem, aboutItem; JPanel eastPanel = new JPanel(); JPanel menuArea = new JPanel(); JPanel mazeArea = new JPanel(); JPanel compassArea = new JPanel(); //declare JPanels JButton forwardButton; //declare JButton JButton rotateButton; //declare JButton JButton room1Button; //declare JButton JButton room2Button; //declare JButton JButton room3Button; //declare JButton JButton resetButton; //declare JButton JButton solveButton; //declare JButton JButton exitButton; //declare JButton JTextField helloTextField; //declare JTextField mazeLayout mlayout; // put this as part of maze:VD int[][] imazePlan = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0}, {0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0}, {0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0}, {0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; JPanel [][]wall = new JPanel[imazePlan.length][imazePlan[0].length]; int xPosition = 19; int yPosition = 1; int xMovement = -1; int yMovement = 0; int rotation = 0; String direction = "North"; public maze() //could use (String title) then super(title) and declare title. { super("Java 2D/3D Graphics Application"); //set the JFrame title mlayout = new mazeLayout(); yourContainer = getContentPane(); // get content pane and name it yourContainer.setLayout(new BorderLayout()); // use border layout eastPanel.setLayout(new BorderLayout()); yourContainer.add(eastPanel, BorderLayout.EAST); controlSetup(); setExtendedState(MAXIMIZED_BOTH); setVisible(true); //display the JFrame } public class mazeLayout extends JPanel { //code to create the maze layout Color[][] wallColor; // added this:VD JPanel[][] wall; // placed this outside of constructor:VD mazeLayout() { setLayout(new GridLayout(imazePlan.length, imazePlan[0].length)); wall = new JPanel[imazePlan.length][imazePlan[0].length]; wallColor = new Color[imazePlan.length][imazePlan[0].length]; for (int i = 0; i < imazePlan.length; i++) { for (int j = 0; j < imazePlan[0].length; j++) { wall[i][j] = new JPanel(); if (imazePlan[i][j] == 0) { wallColor[i][j] = Color.darkGray; // wall[i][j].setBackground(Color.darkGray); } else { if (imazePlan[i][j] == 2) { wallColor[i][j] = Color.GREEN; // wall[i][j].setBackground(Color.GREEN); } else { if (imazePlan[i][j] == 3) { wallColor[i][j] = Color.RED; // wall[i][j].setBackground(Color.RED); } else { if (imazePlan[i][j] == 1) { wallColor[i][j] = Color.lightGray; // wall[i][j].setBackground(Color.lightGray); } } } } add(wall[i][j]); } } } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for (int i = 0; i < imazePlan.length; i++) { for (int j = 0; j < imazePlan[0].length; j++) { wall[i][j].setBackground(wallColor[i][j]); } } } } public void controlSetup() //Code that creates the east Panel layout { menuArea.setLayout(new GridLayout(0, 2)); //set JPanel Layout forwardButton = new JButton("Forward"); // create button forwardButton.setToolTipText("Move Forward"); // gives a mouse over help / tip. menuArea.add(forwardButton); forwardButton.addActionListener(this); rotateButton = new JButton("Rotate"); // create button rotateButton.setToolTipText("Rotate 90 degrees to the right."); // gives a mouse over help / tip. menuArea.add(rotateButton); rotateButton.addActionListener(this); room1Button = new JButton("Room 1"); // create button room1Button.setToolTipText("Room 1"); // gives a mouse over help / tip. menuArea.add(room1Button); room2Button = new JButton("Room 2"); // create button room2Button.setToolTipText("Room 2"); // gives a mouse over help / tip. menuArea.add(room2Button); room3Button = new JButton("Room 3"); // create button room3Button.setToolTipText("Room 3"); // gives a mouse over help / tip. menuArea.add(room3Button); solveButton = new JButton("Solve"); // create button solveButton.setToolTipText("Solves the maze."); // gives a mouse over help / tip. menuArea.add(solveButton); resetButton = new JButton("Reset"); // create button resetButton.setToolTipText("Resets the maze."); // gives a mouse over help / tip. menuArea.add(resetButton); exitButton = new JButton("Exit"); // create button exitButton.setToolTipText("Exits the program."); // gives a mouse over help / tip. exitButton.addActionListener(this); menuArea.add(exitButton); mazeArea.add(mlayout); helloTextField = new JTextField("", 12); //create text field to display button response helloTextField.setEditable(false); //prevent text field editing compassArea.add(helloTextField, BorderLayout.CENTER); //add text field to container eastPanel.add(compassArea, BorderLayout.CENTER); eastPanel.add(mazeArea, BorderLayout.NORTH); eastPanel.add(menuArea, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent event) { if (event.getSource() == exitItem) { System.exit(0); } if (event.getSource() == exitButton) { System.exit(0); } if (event.getSource() == rotateButton) //"Press") could also be used { rotation = ++rotation; { if (rotation == 0) //the following is the logic for moving around the maze { xMovement = -1; yMovement = 0; direction = "North"; helloTextField.setText(direction); //set the text to this.. } else { if (rotation == 1) { xMovement = 0; yMovement = +1; direction = "East"; helloTextField.setText(direction); //set the text to this.. } else { if (rotation == 2) { xMovement = +1; yMovement = 0; direction = "South"; helloTextField.setText(direction); //set the text to this.. } else { if (rotation == 3) { xMovement = 0; yMovement = -1; direction = "West"; helloTextField.setText(direction); //set the text to this.. } else { if (rotation > 3) { rotation = 0; xMovement = -1; yMovement = 0; direction = "North"; helloTextField.setText(direction); //set the text to this.. } } } } } } } if (event.getSource() == forwardButton) xPosition = xPosition + xMovement; yPosition = yPosition + yMovement; helloTextField.setText(direction); //set the text to this.. mlayout.wallColor[xPosition][yPosition] = Color.blue; mlayout.repaint(); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.err.println("Couldn't use the system look and feel: " + e); } maze C2D3DGraphicsApp = new maze(); //C2D3DGraphicsApp("Java 2D/3D Graphics Application"); C2D3DGraphicsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame Swing way } }// end class C2D3DGraphicsApp
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
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:
Java Syntax (Toggle Plain Text)
if (event.getSource() == forwardButton) xPosition = xPosition + xMovement; yPosition = yPosition + yMovement; helloTextField.setText(direction); //set the text to this.. mlayout.wallColor[xPosition][yPosition] = Color.blue; 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.
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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!!
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Solved Threads: 0
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!
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!
Java Syntax (Toggle Plain Text)
if (event.getSource() == solveButton) { int i=1; if (mlayout.wallColor[xPosition][yPosition] != Color.RED){ while (mlayout.wallColor[xPosition-i][yPosition] == Color.LIGHT_GRAY) { newXPosition=xPosition-1; mlayout.wallColor[newXPosition][yPosition] = Color.BLUE; xPosition=newXPosition; mlayout.repaint(); } while (mlayout.wallColor[xPosition][yPosition+i] == Color.LIGHT_GRAY) { newYPosition=yPosition+1; mlayout.wallColor[xPosition][newYPosition] = Color.BLUE; yPosition=newYPosition; mlayout.repaint(); } while (mlayout.wallColor[xPosition+i][yPosition] == Color.LIGHT_GRAY) { newXPosition=xPosition+1; mlayout.wallColor[newXPosition][yPosition] = Color.BLUE; xPosition=newXPosition; mlayout.repaint(); } while (mlayout.wallColor[xPosition][yPosition-i] == Color.LIGHT_GRAY) { newYPosition=yPosition-1; mlayout.wallColor[xPosition][newYPosition] = Color.BLUE; yPosition=newYPosition; mlayout.repaint(); } } } }
Last edited by mousey182; Mar 28th, 2008 at 2:41 pm.
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
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:
Two, and more important, are your four "while" conditions within this larger "if" statement above ("while" after you change it):
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!
Java Syntax (Toggle Plain Text)
while (mlayout.wallColor[xPosition][yPosition] != Color.RED)
Java Syntax (Toggle Plain Text)
while (mlayout.wallColor[xPosition + i][yPosition] == Color.LIGHT_GRAY)
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!
•
•
Join Date: Mar 2008
Posts: 12
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- Data Structure help (stacks) (Java)
Other Threads in the Java Forum
- Previous Thread: How to check the dates that will be three months from now
- Next Thread: print from doubly linked list
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






