Results 1 to 3 of 3
Thread: Java Maze Problem
- 03-26-2008, 05:48 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 1
- Rep Power
- 0
Java Maze Problem
Hey Guys,
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
Java Code: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
- 03-28-2008, 06:55 AM #2
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
hey dude, I had a go at getting your application to work. I can't get the maze to repaint yet, but I think I have worked out a way to update the array so that numbers change properly(you will need a statement that stops you from picking a square outside the maze!):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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=0;
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);
setBounds(300,100,210,400); // I prefer working with a smaller screen!
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
{
direction="North";
}
else if (rotation==1)
{
direction="East";
}
else if (rotation==2)
{
direction="South";
}
else if (rotation==3)
{
direction="West";
}
else if (rotation > 3)
{
rotation=0;
direction="North";
}
}
}
if (event.getSource() == forwardButton) //"Press") could also be used
{
helloTextField.setText(direction); //set the text to this..
if (direction =="North")
{
if(imazePlan[(xPosition +1)] [yPosition] ==1)
{
imazePlan[(xPosition +1)] [yPosition] =2;
imazePlan[xPosition] [yPosition]= 1;
xPosition = (xPosition+1);
}
}
if (direction =="East")
{
if(imazePlan[xPosition] [(yPosition +1)] ==1)
{
imazePlan[xPosition] [(yPosition +1)] =2;
imazePlan[xPosition] [yPosition]= 1;
yPosition = (yPosition+1);
}
}
if (direction =="South")
{
if(imazePlan[(xPosition -1)] [yPosition] ==1)
{
imazePlan[(xPosition -1)] [yPosition] =2;
imazePlan[xPosition] [yPosition]= 1;
xPosition = (xPosition-1);
}
}
if (direction =="West")
{
if(imazePlan[xPosition] [(yPosition -1)] ==1)
{
imazePlan[xPosition] [(yPosition -1)] =2;
imazePlan[xPosition] [yPosition]= 1;
yPosition = (yPosition-1);
}
}
repaint();// It's allmost there you just have to work out a way of repainting the maze with the new values
System.out.println("x = " + xPosition);
System.out.println("y = " + yPosition);
}
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
}
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.E XIT_ON_CLOSE); //close frame Swing way
}
}// end class C2D3DGraphicsApp
- 03-28-2008, 06:29 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
repaint?
I've never used the setBackground command. Not sure how you would repaint the maze? If you could work it out the rest should be easy.
I've moved the setText so it updates the textField when you press the rotate button:
else if (rotation > 3)
{
rotation=0;
direction="North";
}
helloTextField.setText(direction);
If you pop it in with the part that deals with the movement logic. I think it makes the user interface much more intuitive.
I had a go at putting in a paint (Graphics g) method, it works when you call the repaint. However it just wipes the maze, is there a method call to paint the maze? :{p
Similar Threads
-
Problem in java
By saytri in forum New To JavaReplies: 4Last Post: 01-16-2008, 10:09 PM -
Maze Help
By Soda in forum Advanced JavaReplies: 1Last Post: 12-22-2007, 03:26 AM -
maze problem (file to a 2d array)
By rnavarro9 in forum New To JavaReplies: 4Last Post: 11-09-2007, 07:36 AM -
JAVA if problem
By toby in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:58 PM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks