Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can't Find what is wrong with my Code

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't Find what is wrong with my Code

    import java.awt.*;
    import java.applet.*;
    import java.util.Random;
    import javax.swing.*;
     
    public class Unit04Lab extends Applet
    {
     
    //	Rectangle r11,r12,r13,r21,r22,r23,r31,r32,r33,r44,r34,r24,r14,r41,r42,r43;
    	boolean scramble[];
    	String matrix[][];
    	Random rnd;
    	int blankR;
    	int blankC;
    	int playLevel;
    	Rectangle rect[][];
    	int wid = 0;
    	int gridSize = 0;
    //	int grid = 0;
    	int letterCount;
     
     
    	public void init()
    	{
    		String prompt = "Enter Play Level \n 3 - 3x3 Grid \n 4 - 4x4 Grid \n 5 - 5x5 Grid";
    		String gridLevel = JOptionPane.showInputDialog(prompt);
    		gridSize = Integer.parseInt(gridLevel);
    		wid =(600/gridSize);
     
    		int x= 100;
    		int y = 100;
    		matrix =new String [gridSize+2][gridSize+2];
    		rect= new Rectangle[gridSize+2][gridSize+2];
     
    		for (int r=1; r<=gridSize; r++)
    		{
    			y=100;
    			for (int c=1; c<=gridSize; c++)
    			{
    				rect[r][c] = new Rectangle(100+((r-1)*wid),100+((c-1)*wid),wid,wid);
    				x +=wid;
    			}
    			y+=wid;
    		}
     
     
    		/*r11 = new Rectangle(100,100,200,200);
    		r12 = new Rectangle(300,100,200,200);
    		r13 = new Rectangle(500,100,200,200);
    		r21 = new Rectangle(100,300,200,200);
    		r22 = new Rectangle(300,300,200,200);
    		r23 = new Rectangle(500,300,200,200);
    		r31 = new Rectangle(100,500,200,200);
    		r32 = new Rectangle(300,500,200,200);
    		r33 = new Rectangle(500,500,200,200);
    		r34 = new Rectangle(700,500,200,200);
    		r24 = new Rectangle(700,300,200,200);
    		r14 = new Rectangle(700,100,200,200);
    		r41 = new Rectangle(100,700,200,200);
    		r42 = new Rectangle(300,700,200,200);
    		r43 = new Rectangle(500,700,200,200);
    		r44 = new Rectangle(700,700,200,200);*/
     
    		letterCount= gridSize * gridSize;
    		scramble = new boolean[letterCount+1];
    		for (int k = 1; k <letterCount+1; k++)
    			scramble[k] = false;
    		rnd = new Random();
     
    		for (int r = 0; r <= gridSize+1; r++)
    			for (int c = 0; c <= gridSize+1; c++)
    				matrix[r][c] = "Y";
     
    		for (int r = 1; r <= gridSize; r++)
    		{
    			for (int c = 1; c <= gridSize; c++)
    			{
    				matrix[r][c] = getLetter();
    				if (matrix[r][c].equals("Y"))
    				{
    					blankR = r;
    					blankC = c;
    				}
    			}
     
    		}
    	}
     
     
    	public String getLetter()
    	{
    		String letter = "Y";
    		boolean Done = false;
    		int rndNum;
     
    		while(!Done)
    		{
    			rndNum = rnd.nextInt(letterCount) + 1;
    			if (scramble[rndNum] == false)
    			{
    				letter = String.valueOf((char) (rndNum+64));
    				scramble[rndNum] = true;
    				Done = true;
    			}
    System.out.println(letter);
    		}
    		return letter;
    	}
     
     
    	public void paint(Graphics g)
    	{
    		drawGrid(g);
    		int y = 100;
    		int x = 100;
    			for( int r = 1; r <= gridSize; r++)
    			{
    				for( int c = 1; c <= gridSize; c++)
    				{
    					drawLetter(g,matrix[c][r],x,y);
    					x += 200;
    				}
    			x = 100;
    			y += 200;
    			}
    	}
     
     
    	public void drawGrid(Graphics g)
    	{
    		int x = 100;
    		int y = 100;
    		int vert = 0;
     
    		g.drawRect(100,100,600,600);
    		for( wid =100; wid<= 700; x++);
    			g.drawLine(x,y,x,y+100);
    		for( y =100; y<=700; y++);
    		{
    			g.drawLine(vert,100,vert,700);
    			g.drawLine(100,vert,700,vert);
    		}
    	}
     
     
    /*	{
     
    		g.drawRect(100,100,800,800);
    		g.drawLine(300,100,300,900);
    		g.drawLine(500,100,500,900);
    		g.drawLine(100,300,900,300);
    		g.drawLine(100,500,900,500);
    		g.drawLine(700,100,700,900);
    		g.drawLine(100,700,900,700);
    	}*/
     
     
    	public void drawLetter(Graphics g, String letter, int x, int y)
    	{
    		int offSetX = x + (int)(wid+15);
    		int offSetY = y + (int)-(wid+15);
    		g.setFont(new Font("Arial",Font.BOLD,wid));
    		if (letter.equals("Y"))
    		{
    			g.setColor(Color.white);
    			g.fillRect(x+1,y+1,wid+2,wid+2);
    		}
    		else
    		{
    			g.setColor(Color.black);
    			g.drawString(letter,offSetX,offSetY);
    		}
    	}
     
     
    	public boolean mouseDown(Event e, int x, int y)
    	{
    		int r=1;
    		int c=1;
    		for(r=1; r<=gridSize; r++)
    		{
    			for (c=1; c<=gridSize; c++)
    			{
    				if (rect[r][c]. contains(x,y) && okSquare(r,c)) {swap(c,r);}
    			}
    		}
    		return true;
    	}
    	/*{
    		if(r11.inside(x,y) && okSquare(1,1))
    			swap(1,1);
    		else if(r12.inside(x,y) && okSquare(1,2))
    			swap(1,2);
    		else if(r13.inside(x,y) && okSquare(1,3))
    			swap(1,3);
    		else if(r14.inside(x,y) && okSquare(1,4))
    			swap(1,4);
    		else if(r21.inside(x,y) && okSquare(2,1))
    			swap(2,1);
    		else if(r22.inside(x,y) && okSquare(2,2))
    			swap(2,2);
    		else if(r23.inside(x,y) && okSquare(2,3))
    			swap(2,3);
    		else if(r24.inside(x,y) && okSquare(2,4))
    			swap(2,4);
    		else if(r31.inside(x,y) && okSquare(3,1))
    			swap(3,1);
    		else if(r32.inside(x,y) && okSquare(3,2))
    			swap(3,2);
    		else if(r33.inside(x,y) && okSquare(3,3))
    			swap(3,3);
    		else if(r34.inside(x,y) && okSquare(3,4))
    			swap(3,4);
    		else if(r41.inside(x,y) && okSquare(4,1))
    			swap(4,1);
    		else if(r42.inside(x,y) && okSquare(4,2))
    			swap(4,2);
    		else if(r43.inside(x,y) && okSquare(4,3))
    			swap(4,3);
    		else if(r44.inside(x,y) && okSquare(4,4))
    			swap(4,4);
    		return true;
    	}*/
     
     
    	public boolean okSquare(int r, int c)
    	{
    		boolean temp = false;
    		if (matrix[r-1][c].equals("Y"))
    			temp = true;
    		else if (matrix[r+1][c].equals("Y"))
    			temp = true;
    		else if (matrix[r][c-1].equals("Y"))
    			temp = true;
    		else if (matrix[r][c+1].equals("Y"))
    			temp = true;
    		return temp;
    	}
     
     
    	public void swap(int r, int c)
    	{
    		matrix[blankR][blankC] = matrix[r][c];
    		matrix[r][c] = "Y";
    		blankR = r;
    		blankC = c;
    		repaint();
    	}
     
     
    	public void update(Graphics g)
    	{
    		paint(g);
    	}
     
     
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Can't Find what is wrong with my Code

    Welcome to the Psychic-Java Forums Raptorman!
    On a serious note, please specify what issues you're getting.. what exceptions are being thrown etc and where you think the error might be occuring.
    Also edit your code to include highlight tags as seen in my signature so the code is readable.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  2. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  3. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  4. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 09:57 AM
  5. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM