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
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