hi, i got class A which class got some graphics to display. when i run the program in class A its fine, but i when run from class B it display like 40 sec delay. any one who can help me?

really need help!

Recommended Answers

All 2 Replies

You really need to post your code. We're working blind!

here is my code, when i run this class it is fine but if i run from another class under button click event it load little delay when maze visited finis then it load. pls help me. thx

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;
import javax.swing.*;




public class Maze2 extends JFrame
  {

         public  static final int TILE_SIZE = 20;

	public static void main(String [] args)
        {


                Maze2 maze = new Maze2();
        	Screen screen = new Screen();
                Button sm = new Button("  Solve Maze  ");
                JButton exit = new JButton("  Exit  ");
		TextField tb = new TextField(20 );
		Label button = new Label("  Algorithm: ");
		sm.setBackground(Color.green);
		exit.setBackground(Color.red);
		button.setBackground(Color.pink);
                screen.add(button);
                screen.add(tb );
                screen.add(sm );
                screen.add(exit );
                screen.setLayout(new FlowLayout());
                screen.setVisible(true);


                int milliseconds = 4000;
		maze.searchExit(screen, milliseconds);


   }

	String maze = "POOPPP" +
		"PPPPWP" +
		"OOPOPP" +
		"SPPPOP"+
		"OPOPOP"+
		"PPPPOE";


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

	Screen screen;

	void searchExit(Screen screen, int milliseconds) {
		this.screen = screen;
		int[] 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';
	}

	static 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);
	}

	static class Screen extends Frame {

		int WIDTH = 500;

		String maze = "";

		public Screen() {
			super("Maze algorithm program");

                        setSize(WIDTH, WIDTH);
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);
				}
			});
  }



  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.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);
			g.setColor(Color.WHITE);
			if (type == 'S')
				g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
			if (type == 'E'){
				g.fillArc(x + r / 4, y + r / 4, width / 2, height / 2, 0, 360);
			g.setColor(Color.black); }

		}


		void showMaze(String maze, int milliseconds) {
			int a =  milliseconds;
			a = 1500;
                        this.maze = maze;
			repaint();
			try {
				Thread.sleep(a);
			} catch (Exception e) {
			}
		}

		public void update(Graphics g) {
			paint(g);
		}
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.