Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-26-2008, 07:48 PM
Member
 
Join Date: Mar 2008
Posts: 1
mousey182 is on a distinguished road
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

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2008, 08:55 AM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 08:29 PM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in java saytri New To Java 4 01-17-2008 12:09 AM
Maze Help Soda Advanced Java 1 12-22-2007 05:26 AM
maze problem (file to a 2d array) rnavarro9 New To Java 4 11-09-2007 09:36 AM
JAVA if problem toby New To Java 2 07-25-2007 09:58 PM
java SE 6 problem techlance Java Applets 1 06-28-2007 12:10 PM


All times are GMT +3. The time now is 04:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org