hi i got problem in ma program. in my pro i got main window when i click on "Maze 1" button it create maze. it worked fine. when maze is created i slove maze. then i close to return main window. now when i hit the "maze 1" button it just display solved maze. how can i create maze like fist hit on the "maze 1 " button? i m including ma code here

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;


public class Main extends JFrame implements ActionListener
{
    public String str="";
    public static String a, b, c, d,e,f;
    JButton maze1 = new JButton("Maze 1");
    JButton maze2 = new JButton("Maze 2");
    JButton maze3 = new JButton("Maze 3");
    JButton maze4 = new JButton("Maze 4");
    JButton exit = new JButton("Exit");
    JRadioButton beg = new JRadioButton( "Begginers", true );
    JRadioButton dif = new JRadioButton( "Difficult", false );
    JRadioButton cg = new JRadioButton( "Computer generate", false );
    Border border = LineBorder.createGrayLineBorder();
    JLabel level = new JLabel("Maze level");
    JLabel info = new JLabel("Note: Select Maze level then click on the maze button!");
    JLabel info1 = new JLabel("Enter no of rows and columns for maze and tap generate maze button!");
    ButtonGroup group = new ButtonGroup();
    JTextField txtCols = new JTextField(2 );
    JTextField txtRows = new JTextField(2);
    JLabel row = new JLabel("Rows");
    JLabel col = new JLabel("Columns");
    JButton genMaze = new JButton("Generate");
    public  static final int TILE_SIZE = 20;
     public int[] startCoordinates;
     public Button sm = new Button("Solve maze");
      Button exit1 = new Button("Exit");
      TextField tb = new TextField(20 );


     static Main frame;
     Main.Screen screen;



     public Main()
    {
     super("Welcome to Maze program");
     maze1.addActionListener(this);
     maze2.addActionListener(this);
     maze3.addActionListener(this);
     maze4.addActionListener(this);
     exit.addActionListener(this);
     exit1.addActionListener(this);
     beg.addActionListener(this);
     dif.addActionListener(this);
     cg.addActionListener(this);
     sm.addActionListener(this);
     genMaze.addActionListener(this);

     getContentPane().setLayout(null);
      group.add(beg);
      group.add(dif);
      group.add(cg);
     //image
     level.setLocation(10,10);
     level.setSize(180,20);
     level.setForeground(Color.blue);
     getContentPane().add( level);

    //begginers
    beg.setLocation(10,40);
    beg.setSize(180,20);
    getContentPane().add( beg);

     //difficult
     dif.setLocation(10,70);
     dif.setSize(180,20);
     getContentPane().add( dif);

     //Computer generated
     cg.setLocation(10,100);
     cg.setSize(180,20);
     getContentPane().add( cg);




     //exit Button
     exit.setLocation(375,170);
     exit.setSize(75,30);
     exit.setForeground(Color.blue);
     exit.setBackground(Color.red);
     getContentPane().add(exit);

     //info levle
      //image
     info.setLocation(110,140);
     info.setSize(380,20);
     getContentPane().add( info);

     //info1 levle

     info1.setLocation(50,140);
     info1.setSize(400,20);
     getContentPane().add( info1);
     
     

         //Maze 1 Button
     maze1.setLocation(15,170);
     maze1.setSize(75,30);
     maze1.setForeground(Color.blue);
     getContentPane().add( maze1);

      //Maze 2 Button
     maze2.setLocation(105,170);
     maze2.setSize(75,30);
     maze2.setForeground(Color.blue);
     getContentPane().add( maze2);
     //maze 3 Button
     maze3.setLocation(195,170);
    maze3.setSize(75,30);
    maze3.setForeground(Color.blue);
     getContentPane().add( maze3);
    //maze 4 Button
     maze4.setLocation(285,170);
    maze4.setSize(75,30);
    maze4.setForeground(Color.blue);
     getContentPane().add( maze4);
       



     //Generate Button
     genMaze.setLocation(265,170);
    genMaze.setSize(95,30);
    genMaze.setForeground(Color.blue);
     getContentPane().add( genMaze);

             row.setLocation(15,170);
             row.setSize(40,30);
             row.setForeground(Color.blue);
             getContentPane().add( row);

               txtRows.setLocation(50,170);
               txtRows.setSize(50,30);
               txtRows.setForeground(Color.blue);
               getContentPane().add( txtRows);

                 col.setLocation(110,170);
                 col.setSize(50,30);
                 col.setForeground(Color.blue);
                 getContentPane().add( col);

                txtCols.setLocation(163,170);
                 txtCols.setSize(50,30);
                 txtCols.setForeground(Color.blue);
                 getContentPane().add( txtCols);
                 
                 txtCols.setVisible(false);
                 txtRows.setVisible(false);
                 col.setVisible(false);
                 row.setVisible(false);
                 info1.setVisible(false);
                 genMaze.setVisible(false);


    }
    public static void main(String[] args)
    {



                RunDB runDB = new RunDB();

      try{
         runDB.loadDriver();
         runDB.makeConnection();
         runDB.buildStatement();
         runDB.executeQuery();
         String x;
         x = runDB.text;
         f = x.substring(30, 36);
         e = x.substring(24, 30);
         d = x.substring(18, 24);
         c = x.substring(12, 18);
          b = x.substring(6, 12);
          a = x.substring(0, 6);
      
      }catch(Exception e){
         e.printStackTrace();
      }

            frame = new Main();
           frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
           frame.setSize(500,700);
           frame.setResizable(false);
           frame.setVisible(true);

    }



     public void actionPerformed(ActionEvent event)
     {
       Object src = event.getSource();
       if(src == exit)
        {
          int exit = JOptionPane.showConfirmDialog(this, " Are you sure? You want to close the program.");
            if (exit == JOptionPane.YES_OPTION)

                System.exit(0);


        }


        else if(src == cg)
        {
          maze1.setVisible(false);
          maze2.setVisible(false);
          maze3.setVisible(false);
          maze4.setVisible(false);
          info.setVisible(false);

          txtCols.setVisible(true);
          txtRows.setVisible(true);
          col.setVisible(true);
          row.setVisible(true);
          info1.setVisible(true);
          genMaze.setVisible(true);
          txtRows.requestFocus();




        }

         else if(src == beg)
        {
          maze1.setVisible(true);
          maze2.setVisible(true);
          maze3.setVisible(true);
          maze4.setVisible(true);
          info.setVisible(true);

          txtCols.setVisible(false);
          txtRows.setVisible(false);
          col.setVisible(false);
          row.setVisible(false);
          info1.setVisible(false);
          genMaze.setVisible(false);

        }
        
        else if(src == dif)
        {
          maze1.setVisible(true);
          maze2.setVisible(true);
          maze3.setVisible(true);
          maze4.setVisible(true);
          info.setVisible(true);

          txtCols.setVisible(false);
          txtRows.setVisible(false);
          col.setVisible(false);
          row.setVisible(false);
          info1.setVisible(false);
          genMaze.setVisible(false);

        }
        else if(src==maze1)
        {


        	 screen = new Main.Screen();
                 frame.setVisible(false);
		Label button = new Label("  Algorithm: ");
		sm.setBackground(Color.green);
		exit1.setBackground(Color.red);
		button.setBackground(Color.pink);
                screen.add(button);
                screen.add(tb );
                screen.add(sm );
                screen.add(exit1 );
                screen.setLayout(new FlowLayout());
                screen.setSize(600,600) ;
                screen.setVisible(true);
               int milliseconds = 10;
                frame.display(screen, milliseconds);

        }
        else if(src==sm)
        {
          /*String myID = tb.getText();
            FileReader fread=null;
            File myFile = new File( myID+".txt");
            if (myFile.exists())
            { */
           screen.setVisible(false);

             int milliseconds = 0;
       	    screen = new Main.Screen();
       	    screen.setSize(600,600) ;
            screen.setVisible(true);
          frame.searchExit1(screen, milliseconds); 
          JOptionPane.showMessageDialog(null,"MAZE SLOVED");
          screen.dispose();
          frame.setVisible(true);
            /*}
            else
            JOptionPane.showMessageDialog(null,"Wrong ALGORITHM");
            tb.setText("");
            tb.requestFocus(); */

        }
        else if(src==genMaze)
        {
             


        } 
        else if(src==exit1)
        {
          
          screen.dispose();
          frame.setVisible(true);
        }



    }



	String maze = "PWWPPP" +
		"PPPPOP" +
		"OOPOPP" +
		"SPPPOP"+
		"OPOPOP"+
		"PPPPOE";
        //a+b+c+d+e+f;


	int width = (int)Math.sqrt(maze.length());

	//Screen screen;


	void display(Screen screen, int milliseconds) {
		screen.showMaze(maze,0);

	}
	void searchExit1(Screen screen, int milliseconds) {
		this.screen = screen;
		startCoordinates = getCoordinates(maze.indexOf("S"));
		maze = visit(maze, startCoordinates[0], startCoordinates[1]);


	}

	String visit(String maze, int x, int y) {
                 screen.showMaze(maze,0);

		if (foundExit(maze))
			return maze;

		if (isEmpty(maze, x - 1, y)) {
			String newMaze = moveLeft(maze, x, y);
			String visitedNewMaze = visit(newMaze, x - 1, y);
			if (foundExit(visitedNewMaze))
				return visitedNewMaze;
		}
		if (isEmpty(maze, x, y - 1)) {
			String newMaze = moveUp(maze, x, y);
			String visitedNewMaze = visit(newMaze, x, y - 1);
			if (foundExit(visitedNewMaze))
				return visitedNewMaze;
		}
		if (isEmpty(maze, x + 1, y)) {
			String newMaze = moveRight(maze, x, y);
			String visitedNewMaze = visit(newMaze, x + 1, y);
			if (foundExit(visitedNewMaze))
				return visitedNewMaze;
		}
		if (isEmpty(maze, x, y + 1)) {
			String newMaze = moveDown(maze, x, y);
			String visitedNewMaze = visit(newMaze, x, y + 1);
			if (foundExit(visitedNewMaze))
				return visitedNewMaze;

		}

		return maze;

	}

	int[] getCoordinates(int pos) {
		return new int[] { pos % width, pos / width };
	}

	int getPosition(int x, int y) {
		return y * width + x;
	}

	boolean isInsideMaze(String maze, int x, int y) {
		if (x < 0 || x >= width)
			return false;
		if (y < 0 || y >= width)
			return false;
		int pos = getPosition(x, y);
		return pos >= 0 && pos < maze.length();
	}

	char look(String maze, int x, int y) {
		return maze.charAt(getPosition(x, y));
	}

	boolean isEmpty(String maze, int x, int y) {
		if (!isInsideMaze(maze, x, y))
			return false;
		return look(maze, x, y) == 'P';
	}

	int cnt = 0;
	boolean isExit(String maze, int x, int y) {
		if (!isInsideMaze(maze, x, y))
			return false;
		return look(maze, x, y) == 'E';
	}

	boolean isStone(String maze, int x, int y) {
		if (!isInsideMaze(maze, x, y))
			return false;
		return look(maze, x, y) == '.';
	}

	boolean foundExit(String maze) {
		cnt++;
		if (cnt % 100 == 0)
			System.out.println(cnt);
		int[] exitCoordinates = getCoordinates(maze.indexOf('E'));
		if (isStone(maze, exitCoordinates[0] - 1, exitCoordinates[1])) {
			return true;
		}
		if (isStone(maze, exitCoordinates[0], exitCoordinates[1] - 1)) {
			return true;
		}
		if (isStone(maze, exitCoordinates[0] + 1, exitCoordinates[1])) {
			return true;
		}
		if (isStone(maze, exitCoordinates[0], exitCoordinates[1] + 1)) {
			return true;
		}
		return false;
	}

	String setStone(String maze, int x, int y) {
		int pos = getPosition(x, y);
		return maze.substring(0, pos) + '.'
				+ maze.substring(pos + 1, maze.length());
	}

	String moveUp(String maze, int x, int y) {
		return setStone(maze, x, y - 1);
	}

	String moveDown(String maze, int x, int y) {
		return setStone(maze, x, y + 1);
	}

	String moveLeft(String maze, int x, int y) {
		return setStone(maze, x - 1, y);
	}

	String moveRight(String maze, int x, int y) {
		return setStone(maze, x + 1, y);
	}


	 class Screen extends JFrame{
          int WIDTH = 600;

		String maze = "";



  public void addComponent(String region, Component component) {
	  Panel panel = new Panel();
	  panel.add(component);
	  add(region, panel); // make sure you add the panel!
    }
		public void paint(Graphics g) {
			BufferedImage image = (BufferedImage) createImage(WIDTH, WIDTH);
			Graphics g2 = image.getGraphics();
			g2.setColor(Color.GRAY);
			g2.fillRect(0, 0, WIDTH, WIDTH);
			int n = (int) Math.sqrt(maze.length());
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					char type = maze.charAt(i + j * n);
					drawMazeArea(g2, i, j, n, type);

				g.setColor(g.getColor().darker());

				}
			}
			g.drawImage(image, 0, 0, this);
		}

		void drawMazeArea(Graphics g, int i, int j, int n, char type) {
			if (type == 'O')
                             {
				g.setColor(Color.BLUE);

                             }
			if (type == 'P')
			{
				g.setColor(Color.WHITE);


			}
			if (type == 'S')
			{
			      g.setColor(Color.RED);

			}
                         if( type == '.')
                         {
				g.setColor(Color.YELLOW);

                         }

			if (type == 'E')
			{
				g.setColor(Color.GREEN);

			}
			int r = WIDTH / (n + 3);
			int x = i * r + r, y = j * r + r, width = r, height = r;
			g.fillRect(x, y, width, height);

			if (type == 'S')
			{
                                 g.setColor(Color.WHITE);
				g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
                                 g.setColor(Color.BLACK);
                               g.fillArc(x + r / 3, y + r / 3, width / 3, height / 3, 0, 360);
                          }
			if (type == 'E')
                        {     g.setColor(Color.WHITE);
				g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
			 g.setColor(Color.RED);
                               g.fillArc(x + r / 3, y + r / 3, width / 3, height / 3, 0, 360);
                         }
		}


		void showMaze(String maze, int milliseconds) {
			int a =  milliseconds;
			a = 0;
                        this.maze = maze;
			repaint();

			try {
				Thread.sleep(a);
			} catch (Exception e) {
			}
		}

		public void update(Graphics g) {

                        paint(g);
		}
	}




}

i am sorry I do not have the time to go true all your code but I think you should look at the part when you close and returns to main window.
My guess is that you have not made any method to clear out the solved maze so it is still there when you returns to main window and want to try and solve a new one. That is why you can solve one maze every time you open up your application because your program then initiates some empty area for the maze to be solved on. So it is probably only this tiny little method you are missing.

Hope this gave you some help.
Good luck

Edited 3 Years Ago by happygeek: fixed formatting

Thank you for ur time and replay. i will try to create method to clear solved maze. thx

This article has been dead for over six months. Start a new discussion instead.