Making a grid with square class help

Need help making grid with square class

Page 1 of 1

2 Replies - 1253 Views - Last Post: 02 December 2009 - 07:49 PM Rate Topic: -----

#1 fallendrako  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 09

Making a grid with square class help

Post icon  Posted 01 December 2009 - 07:47 PM

Ok, So I'm working on a battleship at school and originally just used a regular grid class but then found out I had to use this Square class to make the grid
 import java.awt.*;

public class Square
{
//**********the location of the square on the grid**********
	//*** Does a square need to know this about itself?... 
	private int gridX;
	private int gridY;
//**********the size of the square, which can be chosen by another class**********
	//*** All squares must have the same width, so this is a class variable (static)
	private static int width;
//**********the status of the square**********
	private int status;
	private final int EMPTY = 0;
	private final int MISS = 1;
	private final int HIT = 2;
	
	public Square()
	{
		//instance variables for square
	}

	//=============================================================
	//  Accessors and modifiers
	//=============================================================
	

//**********methods for receiving the grid location of the square from another class**********
	public void setX(int x)
	{
		gridX=x;
	}
	public void setY(int y)
	{
		gridY=y;
	}
//**********method for receiving the width choice of the square from another class**********
	public void setWidth (int widthChoice)
	{
		width=widthChoice;
	}
//**********method for receiving the status of the square from another class**********
	public void setStatus (int squareStatus)
	{
		status=squareStatus;
	}
//**********method for sending the grid location of the square to another class**********
	public int getX()
	{
		return gridX;
	}
	public int getY()
	{
		return gridY;
	}
//**********method for sending the square's width to other classes**********
	public int getSquareWidth()
	{
		return width;
	}
//**********method for sending the status of the square to other classes**********
	public int getStatus()
	{
		return status;
	}
//**********method for drawing each indiviual square**********
	public void drawSquare(int cornerX, int cornerY, Graphics g)
	{
		//*** You need to recieve the location to draw the square! (cornerX, cornerY)
		//*** Squares exist on more than one grid - PlayerGrid class organizes this.
		
		//*** Do you need to set the color here as well?  
		g.drawRect(cornerX, cornerY, width, width);
		g.setColor(Color.WHITE);
		//*** Fix this with cornerX,Y
		g.drawRect(gridX - (gridX/10), gridY - (gridY/10), width - (width/10), width - (width/10));
		
	}	
		
}


And then they proceeded to tell me to make the squares place themselves using the cornerX,Y and i'm lost, Any hints on how to do this would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Making a grid with square class help

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1155
  • View blog
  • Posts: 7,167
  • Joined: 07-September 06

Re: Making a grid with square class help

Posted 02 December 2009 - 08:46 AM

Moved to Java.
Was This Post Helpful? 0
  • +
  • -

#3 fallendrako  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 09

Re: Making a grid with square class help

Posted 02 December 2009 - 07:49 PM

I might have gotten it, I was wondering if someone would check this for me. The Square class was edited to this
 import java.awt.*;

public class Square
{
//**********the location of the square on the grid**********
	//*** Does a square need to know this about itself?... 
	private int gridX;
	private int gridY;
//**********the size of the square, which can be chosen by another class**********
	//*** All squares must have the same width, so this is a class variable (static)
	private static int width;
//**********the status of the square**********
	private int status;
	private final int EMPTY = 0;
	private final int MISS = 1;
	private final int HIT = 2;
	
	public Square()
	{
		//instance variables for square
	}

	//=============================================================
	//  Accessors and modifiers
	//=============================================================
	

//**********methods for receiving the grid location of the square from another class**********
	public void setX(int x)
	{
		gridX=x;
	}
	public void setY(int y)
	{
		gridY=y;
	}
//**********method for receiving the width choice of the square from another class**********
	public void setWidth (int widthChoice)
	{
		width=widthChoice;
	}
//**********method for receiving the status of the square from another class**********
	public void setStatus (int squareStatus)
	{
		status=squareStatus;
	}
//**********method for sending the grid location of the square to another class**********
	public int getX()
	{
		return gridX;
	}
	public int getY()
	{
		return gridY;
	}
//**********method for sending the square's width to other classes**********
	public int getSquareWidth()
	{
		return width;
	}
//**********method for sending the status of the square to other classes**********
	public int getStatus()
	{
		return status;
	}
//**********method for drawing each indiviual square**********
	public void drawSquare(int cornerX, int cornerY, Graphics g)
	{
		//*** You need to recieve the location to draw the square! (cornerX, cornerY)
		//*** Squares exist on more than one grid - PlayerGrid class organizes this.
		
		g.drawRect(cornerX, cornerY, width, width);
		
	}	
//**********method for testing the drawing each indiviual square**********	
	public void drawSquareTest(Graphics g, int gridXx, int gridYy, int widthw)
	{   
		g.drawRect(gridXx, gridYy, widthw, widthw);	   
	}	   

}


And so now I have it doing this to make a 10x10 grid with 30 pixel sized squares

   for(int a= 100;a <400; a = a +30)
		{
			for(int b = 100; b < 400; b= b +30)
			{
			
				tod.drawSquareTest(g,a,b,30);
			}
		}


Would this work?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1