import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.geom.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import java.awt.print.*;

/**
   BoardPanel.java
   This class implements board panel of the userinterface.  This is where
   the game will be played on.
   @author Sugiharto Widjaja  Contribution from Toan with printing, scoremode, draw turn
   				 testing and debugging.
   @version November 30, 2001
   */

class BoardPanel extends JPanel implements Printable
{
	// The downPanel panel
	private DownPanel downPanel;
	// The upPanel panel
	private UpPanel upPanel;
	// The size of the font used
	private static final int FONTSIZE = 18;
	// The size of the font used for labeling the coordinates
	private static final int COORD_SIZE = 12;
    // The size of the each square of the board
    private static final int SIZE = 20;
    // The number of squares for each row
    private static final int SQUARES = 20;
    // The radius of the stone
    private static final int RADIUS = 8;
    // The starting x coordinate for drawing the board
    private static final int xCoord = 50;
    // The starting y coordinate for drawing the board
    private static final int yCoord = 50;
    // The role of the two players
	private String black_role, white_role;
	// Tell if game is in progress
	private boolean gameInProgress;
	// Is player losing now
	private boolean isLosing;
	// Has the player received choice of dead or alive stone from opponent?
	private boolean receive;
	// Display start message
	private int start_m;
	// Scoring mode
	private boolean scoring;
	// Number of passes
	private int num_pass;
	// Number of KO to display error message
	int numOfKO;
	// Use in score mode player's choice of dead or alive
	private int player, other;
    // The game object
	private Game aGame;

    /**
	       Creating the board for the game, and initializing the data structure
	       needed to play the game.
	       @param none
	       <dt><b>Postcondition:</b><dd>
	       The board will be drawn, and the needed data structure is initialized.
       */

    public BoardPanel()
    {
	  // Create a new game
	  aGame = new Game();
      // Detecting clicks on the board
      addMouseListener(new MouseHandler());
      black_role = "Black";
      white_role = "White";
      stopGame();
      numOfKO = 0;
      start_m = 0;
      num_pass = 0;
      receive = false;
	  other = -1;
	  // Not currently in scoring mode
	  scoring = false;
	}

	/**
	   Return the current game
	   @param none
	   @return current game in progress
	   */

	public Game getCurrentGame()
	{
		return aGame;
	}

	/**
		   Establishing communication with the DownPanel panel
		   @param thePanel - the DownPanel panel
		   <dt><b>Precondition:</b><dd>
		   thePanel is a valid DownPanel panel
		   <dt><b>Postcondition:</b><dd>
		   Communication between BoardPanel and DownPanel panel is established.
	   */

	public void addDownPanel(DownPanel thePanel)
	{
		downPanel = thePanel;
	}

	/**
		   Establishing communication with the UpPanel panel
		   @param thePanel - the UpPanel panel
		   <dt><b>Precondition:</b><dd>
		   thePanel is a valid UpPanel panel
		   <dt><b>Postcondition:</b><dd>
		   Communication between BoardPanel and UpPanel panel is established.
	   */

	public void addUpPanel(UpPanel thePanel)
    {
		upPanel = thePanel;
	}

	/**
		   Declare that this user is losing the game
		   @param none
		   <dt><b>Postcondition:</b><dd>
		   The current game is stopped.
	   */

	public void losingTrue()
	{
		isLosing = true;
	}

	/**
		   Declare that this user is winning the game
		   @param none
		   <dt><b>Postcondition:</b><dd>
		   The current game is stopped.
	   */

	public void losingFalse()
	{
	    isLosing = false;
	}

	/**
		   Stopping the current game
		   @param none
	   */

	public void stopGame()
	{
		gameInProgress = false;
    }

	/**
		   Starting the current game
		   @param none
	  */

	public void continueGame()
	{
		gameInProgress = true;
		scoring = false;
    }

    /**
       User has received the choice of alive or dead stone(s) from the opponent
       @param none
       <dt><b>Postcondition:</b><dd>
       User can now decide whether the stone(s) are dead or alive
       */

    public void receiveMessage()
    {
		receive = true;
	}

	/**
	   Used in score mode. This will set the player's opponent as player
	   who will decide whether stone(s) are alive.
	   @param other1 - The other player
	   @param none
	   */

	public void setOther(int other1)
	{
		other = other1;
	}

    /**
       Method to start the actual new game. (not the scoring mode)
       The history list will be updated to reflect the new game.
       @param none
       */

    public void startGame()
    {
		Game temp;
		temp = new Game();
		aGame.copyGame(temp);
	    gameInProgress = true;
	    scoring = false;
	    start_m = 0;
	    (upPanel.getHisList()).update_t();
    }

    /**
       This will set the variable start_m to be 1. If start_m equals 1,
       then the message "click the start button to start" will come out
       on the board.
       @param none
       */

	public void start_message()
	{
		start_m = 1;
	}

    /**
       Check whether a game is currently in progress.
       @param none
       @return true if game is currently in progress, otherwise, return false
       */

    public boolean gameProgress()
	{
		return gameInProgress;
    }

	/**
	   This method will set the username of players who play black stone and white stone
	   @param black1 - the username of the player who plays black stone
	          white1 - the username of the player who plays white stone
	   <dt><b>Postcondition:</b><dd>
	   The username of player who plays black stone and username of player who plays
	   white stone will be displayed on the board.
	   */

	public void setRole(String black1, String white1)
	{
		black_role = black1;
		white_role = white1;
	}

    /**
		   To do the takeback
		   @param tBackReq - number of takeback requests
		   @see doTakeBack1(int tBackReq)
	   */

	public void doTakeBack2(int tBackReq)
	{
		// Multiply tBackReq by 2 so that we won't take back opponen't
		// move instead.
		if (downPanel.returnTurn() == true)
			aGame.TakeBack(2 * tBackReq);
		else
		{
			aGame.TakeBack(2 * tBackReq - 1);
			downPanel.setTurn();
			repaint();
		}
	}

	/**
		   To do the takeback.
		   @param tBackReq - number of takeback requests
		   @see doTakeBack2(int tBackReq)
	   */

	public void doTakeBack1(int tBackReq)
	{
		// Multiply tBackReq by 2 so that we won't take back opponen't
		// move instead.
		if (downPanel.returnTurn() == false)
			aGame.TakeBack(2 * tBackReq);
		else
		{
			aGame.TakeBack(2 * tBackReq - 1);
			downPanel.setTurn();
			repaint();
		}
	}

    /**
	       Detecting the mouse clicked event and draw a stone on the edge
	       if none of the restrictions is violated
	       @param event - The mouse clicked event
	       <dt><b>Postcondition:</b><dd>
	       A stone will be drawn on an edge if none of these restrictions is violated:
	       1. The click must be performed on an edge within the board. (The (7,7)
	          coordinate tolerance is used)
	       2.Edge is not already occupied by other stone
	       3.The move did not result in KO.
       */

    private class MouseHandler extends MouseAdapter
    {
		public void mouseClicked(MouseEvent event)
		{
			int color = 0;
            // If game is not in progress, do nothing
			if (!gameInProgress)
			   ;
			else
			{
			   if (!downPanel.returnTurn())
			      ;
			   else
			   {
			      if(downPanel.getColorRole() == 0)
			         color = 0;
			      else
			         color = 1;

			      System.out.println("Mouse  clicked at " + event.getPoint());
			      //String handle = downPanel.getUser();
			      //downPanel.sendMsg("Clicked on " + event.getPoint());
			      Point2D point = event.getPoint();
			      int x = (int) point.getX();
			      int y = (int) point.getY();
			      int xMax = xCoord + (SIZE * SQUARES);
			      int yMax = yCoord + (SIZE * SQUARES);
			      // Testing whether the clicked coordinate is outside the board
			      if (x < xCoord || x > xMax || y < yCoord || y > yMax)
			         System.out.println("Still at if");
			      else
			      {
				     // Testing whether the clicked coordinate is within the tolerance
					//range.
				     int s = ( x - xCoord ) % SIZE;
				     int t = ( y - yCoord ) % SIZE;

				     if ( (s < 7 && t < 7) || (s > 11 && t > 11) || (s < 7 && t > 11) ||
							(s > 11 && t < 7))
				     {
				         // Conversion from actual coordinates to index of array.
					     int aX;
					     int aY;

					     if (s < 7 && t < 7)
					     {
					        aX = ( x - xCoord ) / SIZE;
						    aY = ( y - yCoord ) / SIZE;
					     }
					     else if(s > 11 && t > 11)
					     {
						    aX = ( x - xCoord ) / SIZE + 1;
						    aY = ( y - yCoord ) / SIZE + 1;
				         }
					     else if(s < 7 && t > 11)
					     {
						    aX = ( x - xCoord ) / SIZE;
						    aY = ( y - yCoord ) / SIZE + 1;
					     }
					     else
					     {
						    aX = ( x - xCoord ) / SIZE + 1;
						    aY = ( y - yCoord ) / SIZE;
					     }

						int k = aGame.move(aX, aY, color);
						// Move resulted in KO

		               if(x == -1 || y == -1)
		               {
			              num_pass++;
			              if (num_pass == 2)
			              {
				             aGame.clear_marked();
				             stopGame();
				             if (downPanel.isHost() == true)
					            scoreMode_message();
				             else
					            scoring = true;
			              }
		               }
		               else
		               {
			              num_pass = 0;
		               }
		               if (k == 3)
		                  upPanel.appendMoves("Pass", color);
		               else if (k == 2)
		               {
			              // Two consecutive moves resulted in KO
			              if(numOfKO >= 1)
			              JOptionPane.showMessageDialog(null,
							   "Move resulted in KO is not allowed!",
							   "Excessive KO", JOptionPane.INFORMATION_MESSAGE);
				          numOfKO++;
		               }
		               else if (k == 1)
		               {
		                  ;
		               }
		               else
		               {
			              // Set coordinate becomes occupied.
			              if (color == 0)
			              {
			                 downPanel.sendMsg("06", aX + "," + aY);
			                 upPanel.appendMoves(aX + "," + aY, 0);
			              }
			              else
			              {
			 	             downPanel.sendMsg("07", aX + "," + aY);
				             upPanel.appendMoves(aX + "," + aY, 1);
			              }
			              repaint();
		                  // Change turn
		                  downPanel.setTurn();
		               }
	                 }
                  }
               }
		    }
        }
    }

    /**
	       Put a stone at specified coordinate
	       @param x - The x coordinate
	       @param y - The y coordinate
	       @param color - Which player do the move ?
	       <dt><b>Precondition:</b><dd>
		   The x and y coordinate are valid positions
		   <dt><b>Postcondition:</b><dd>
		   A stone with color "color" will be drawn on the board
		   provided that no restriction is violated.
	   */

	public void move(int x, int y, int color)
	{
	    int k = aGame.move(x, y, color);
	   						// Move resulted in KO
		if (k == 3)			// This is a pass
		{
		   upPanel.appendMoves("Pass", color);
	    }
		else if (k == 2)	// This is ko
		{
	       ;
	    }
		else
		{
	       upPanel.appendMoves(x + "," + y, color);
		}
		repaint();
		// Change turn
		downPanel.setTurn();

		if(x == -1 || y == -1)
		{
			num_pass++;
			if (num_pass == 2)
			{
				aGame.clear_marked();
				stopGame();
				if (downPanel.isHost() == true)
					scoreMode_message();
				else
					scoring = true;
			}
		}
		else
		{
			num_pass = 0;
		}
		repaint();
	}

	/**
	   When the game is in score mode, each stone or group of stones will be highlighted.
	   Both users will be asked whether the stone or group of stones is/are dead or alive.
	   @param none
	   <dt><b>Precondition:</b><dd>
	   The game is currently in score mode
	   @see scoreMode_message1
	   */

	public void scoreMode_message()
	{
		int x, y;
		boolean exit = false;
		scoring = true;
		for(x = 0; x < 19 && !exit; x++)
		{	for(y = 0; y < 19 && !exit; y++)
			{
				if((((aGame.getBoard()).getStone(x, y)).getColor() != -1)
					&& (aGame.marked[x][y] == false))
				{
					aGame.clear();
					aGame.maximal_C(x, y);
					repaint();
					player = JOptionPane.showOptionDialog(null, "These stone(s) are",
									   "Dead or Alive", JOptionPane.DEFAULT_OPTION,
									   JOptionPane.QUESTION_MESSAGE, null,
									   new String[]
									   {
										  "Dead",
										  "Alive"
									   },
									   "Dead");
					downPanel.sendMsg("30", "" + player);  // Send status
					exit = true;

				}
			}
		}

		if (exit == false)
		{
			endgame();
			downPanel.sendMsg("50");
		}
		repaint();
	}

	/**
	   When the game is in score mode, each stone or group of stones will be highlighted.
	   Both users will be asked whether the stone or group of stones is/are dead or alive.
	   @param none
	   <dt><b>Precondition:</b><dd>
	   The game is currently in score mode
	   @see scoreMode_message
	   */
	public void scoreMode_message1()
	{
		int x, y;
		boolean exit = false;

		scoring = true;
	    for(x = 0; x < 19 && !exit; x++)
		{	for(y = 0; y < 19 && !exit; y++)
			{
				if((((aGame.getBoard()).getStone(x, y)).getColor() != -1)
					&& (aGame.marked[x][y] == false))
				{
					aGame.clear();
					aGame.maximal_C(x, y);
					repaint();
					player = JOptionPane.showOptionDialog(null, "These stone(s) are",
									   "Dead or Alive", JOptionPane.DEFAULT_OPTION,
									   JOptionPane.QUESTION_MESSAGE, null,
									   new String[]
									   {
										  "Dead",
										  "Alive"
									   },
									   "Dead");
					downPanel.sendMsg("40", "" + player);  // Send status
					exit = true;

				}
			}
		}
		if (exit == false)
		{
			endgame();
		}
		repaint();
	}

	/**
	   The score mode of the game.
	   If both players agree on the status of stone(s), then the game will continue
	   to highlight other stone(s). If there is a disagreement between players,
	   the game will start from where it left.
	   @param none
	   @return true if both players have agreement on the stone(s), false if there is a
	   disagreement between the players
	   */

	public boolean scoreMode()
	{
		boolean success = false;
		if (receive == true && player != -1)
		{
			if (player == 0 && other == 0)	// if both agree
			{
				aGame.Capture();
				aGame.clear();
				success = true;
			}
			else if (player == 1 && other == 1)
			{
				aGame.clear();
				success = true;
			}
			else
			{
				aGame.TakeBack(0);
				continueGame();
				num_pass = 0;
				aGame.clear();
			}
			receive = false;
			player = -1;
			other = -1;
			repaint();
		}
		return success;
	}

	/**
	   When players have finished deciding on the status of all stones, this method
	   will be called. All buttons will be disabled, except the enable assign option.
	   A dialog box will occur displaying number of captured stones of each player.
	   @param none
	   */

	public void endgame()
	{
	    upPanel.disableButtons();        // Disable all buttons
	    downPanel.enableAssign(); // Enable assign option is enabled
	    int white_c = aGame.getBoard().getNumofCapturedWStone();
	    int black_c = aGame.getBoard().getNumofCapturedBStone();
	    int white = aGame.getBoard().getNumofWStone();
	    int black = aGame.getBoard().getNumofBStone();
	    JOptionPane.showMessageDialog(null, "Black has " + black + " stones on the board, captured " + white_c + " white stones  " + "\nWhite has " + white + " stones on the board, captured " + black_c + " black stones  ",
				   "Game Stats", JOptionPane.INFORMATION_MESSAGE);

	}

    /**
	   The method to draw the graphics on the frame
	   @param g - The Graphics object
	   @see drawBackground(Graphics g, Graphics2D g2)
	   @see drawBoard(Graphics g, Graphics2D g2)
	   @see drawPlayers(Graphics g, Graphics2D g2)
	   @see drawCoord(Graphics g, Graphics2D g2)
	   @see drawStones(Graphics g, Graphics2D g2)
       */

    public void paintComponent(Graphics g)
    {
       Graphics2D g2 = (Graphics2D)g;
       super.paintComponent(g2);

       drawbackground(g, g2);
       drawboard(g, g2);
       drawplayers(g, g2);
       drawcoord(g, g2);
       drawstones(g, g2);
       drawturn(g, g2);
    }

    /**
	   Set the color of the background
	   @param g - the Graphics object
	   @param g2 - the Graphics2D object
       */

    private void drawbackground(Graphics g, Graphics2D g2)
    {
       Color color = new Color(200, 200, 200);
	   g2.setBackground(color);
    }

    /**
	   Draw the Go! board
	   @param g - the Graphics object
	   @param g2 - the Graphics2D object
       */

    private void drawboard(Graphics g, Graphics2D g2)
    {
	   int x_coord = xCoord;
	   int y_coord = yCoord;
	   Rectangle2D.Double Rec;

	   Color color = new Color(122, 89, 26);
	   g.setColor(color);
	   Rec = new Rectangle2D.Double( (int)x_coord - RADIUS, (int)y_coord - RADIUS, (int)SIZE * 18 + 2*RADIUS, (int)SIZE * 18 + 2*RADIUS);
	   g2.fill(Rec);

	   if (scoring)
	   {
		   int diff = SIZE / 2;
		   x_coord = xCoord - diff;
		   y_coord = yCoord - diff;
		   Color color_highlight = new Color(200, 97, 34);
		   g.setColor(color_highlight);
		   g2.setFont(new Font("Serif", Font.BOLD, FONTSIZE));

		   g2.drawString("Scoring Mode", xCoord*3, yCoord - 30);
		   for (int i = 0; i < 19; i++)
		   {
			  for (int j = 0; j < 19; j++)
			  {
				 if (aGame.visited[i][j] == true)
				 {
					 Rec = new Rectangle2D.Double( (int)x_coord, (int)y_coord, (int)SIZE, (int)SIZE);
				     g2.fill(Rec);
				 }
				 y_coord += SIZE;

			  }
			  y_coord = yCoord - diff;
			  x_coord += SIZE;
		   }
	   }
	   x_coord = xCoord;
	   y_coord = yCoord;
	   Color color1 = new Color(0, 0, 0);
	   g.setColor(color1);
	   for (int i = 0; i < 18; i++)
	   {
	      for (int j = 0; j < 18; j++)
		  {
		     Rec = new Rectangle2D.Double( (int)x_coord, (int)y_coord, (int)SIZE, (int)SIZE);
			 g2.draw(Rec);
			 x_coord += SIZE;
		  }
	      x_coord = xCoord;
	      y_coord += SIZE;
	   }

	   if (start_m == 1)
	   {
		   Color white = new Color(250, 250, 250);
		   g.setColor(white);
		   g2.setFont(new Font("Serif", Font.BOLD, COORD_SIZE));
		   g2.drawString("Click Start button to begin", 170, 200);
	   }

	}

    /**
	   Draw the handle of each players in a specified locations
	   @param g - the Graphics object
	   @param g2 - the Graphics2D object
       */

	private void drawplayers(Graphics g, Graphics2D g2)
	{
		g2.setFont(new Font("Serif", Font.BOLD, FONTSIZE));
    	Color black = new Color(0, 0, 0);
		g.setColor(black);
		g2.drawString(black_role, xCoord, 20);
		Color white = new Color(250, 250, 250);
		g.setColor(white);
		g2.drawString(white_role, xCoord, 440);
    }

	/**
	   Draw the coordinates number around the game board
	   @param g - the Graphics object
	   @param g2 - the Graphics2D object
       */

	private void drawcoord(Graphics g, Graphics2D g2)
	{
		g2.setFont(new Font("Serif", Font.PLAIN, COORD_SIZE));

		Color coord = new Color(40, 40, 40);
		g.setColor(coord);

		int k;
		for (k = 0; k < 19; k++)
			g2.drawString(""+k, k*SIZE + xCoord - 3, yCoord - 10);
		for (k = 0; k < 19; k++)
			g2.drawString(""+k, xCoord - 22, k*SIZE + yCoord + 5);

	}

    /**
	   Draw the stones in a clicked edge
	   @param g - the Graphics object
	   @param g2 - the Graphics2D object
	   <dt><b>Precondition:</b><dd>
	   edge is not occupied
       */
    private void drawstones(Graphics g, Graphics2D g2)
    {
		Board aBoard = aGame.getBoard();


		  for (int i = 0; i < 19; i++)
		  {
			 for (int j = 0; j < 19; j++)
			 {

				 if (aBoard.getStone(i, j).getColor() != -1)
				 {
					int xC = (i * SIZE) + xCoord;
					int yC = (j * SIZE) + yCoord;
					int diam = 2 * RADIUS;
					Ellipse2D.Double ells = new Ellipse2D.Double(xC - RADIUS, yC - RADIUS, diam, diam);

					 int color = (aBoard.getStone(i, j)).getColor();
					 if(color == 0)
						g2.setPaint(Color.black);
					 else
						g2.setPaint(Color.white);

					g2.fill(ells);
				}
			 }
		 }
	}

	/**
	   Draw the handle of the user who has the current turn.
	   @param g - the Graphics object
	   @param g2 - the Graphics2D object
       */

	private void drawturn(Graphics g, Graphics2D g2)
	{
		int color;
		boolean turn;
		int diam = 2 * RADIUS;


		if(downPanel.getColorRole() == 0)
		   color = 0;
		else if(downPanel.getColorRole() == 1)
		   color = 1;
		else
			color = -1;

		if(downPanel.returnTurn() == false)
			turn = false;
		else
			turn = true;

		if (color == 0 && turn == true)
		{
			Ellipse2D.Double ells = new Ellipse2D.Double(20, 8, diam, diam);
			g2.setPaint(Color.black);
			g2.fill(ells);
		}

		else if (color == 0 && turn == false)
		{
			Ellipse2D.Double ells = new Ellipse2D.Double(20, 426, diam, diam);
			g2.setPaint(Color.white);
			g2.fill(ells);
		}

		else if (color == 1 && turn == true)
		{
			Ellipse2D.Double ells = new Ellipse2D.Double(20, 426, diam, diam);
			g2.setPaint(Color.white);
			g2.fill(ells);
		}

		else if (color == 1 && turn == false)
		{
			Ellipse2D.Double ells = new Ellipse2D.Double(20, 8, diam, diam);
			g2.setPaint(Color.black);
			g2.fill(ells);
		}

		else {}
	}

    /**
       Below this point are codes for printing the board.
       Codes are taken follow from Sun's Java Tutorial
       @param none
       <dt><b>Postcondition:</b><dd>
	   It will print the current board
	   */

	public void print_board()
	{
			PrinterJob printJob = PrinterJob.getPrinterJob();
			printJob.setPrintable(this);
			if (printJob.printDialog()) {
				try {
					printJob.print();
				} catch (Exception PrintException) {
					PrintException.printStackTrace();
				}
			}
	}


	public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
		if (pi >= 1) {
			return Printable.NO_SUCH_PAGE;
	}
		Graphics2D g2 = (Graphics2D)g;
		drawboard_p(g, g2);
		drawplayers_p(g, g2);
		drawcoord_p(g, g2);
		drawstones_p(g, g2);
		//      drawturn(g, g2);
		return Printable.PAGE_EXISTS;
    }

    // Need to change the starting coordinates x_coord and y_coord so that the top corner
    // will show on print page
    private void drawboard_p(Graphics g, Graphics2D g2)
    {
	   int x_coord = xCoord;
	   int y_coord = yCoord;
	   Rectangle2D.Double Rec;

	   Color color = new Color(122, 89, 26);
	   g.setColor(color);
	   Rec = new Rectangle2D.Double( (int)x_coord + 5*RADIUS, (int)y_coord + 7*RADIUS, (int)SIZE * 18 + 2*RADIUS, (int)SIZE * 18 + 2*RADIUS);
	   g2.fill(Rec);

	   if (scoring)
	   {
		   int diff = SIZE / 2;
		   x_coord = xCoord - diff + 6*RADIUS;
		   y_coord = yCoord - diff + 8*RADIUS;
		   Color color_highlight = new Color(200, 97, 34);
		   g.setColor(color_highlight);
		   g2.setFont(new Font("Serif", Font.BOLD, FONTSIZE));

		   g2.drawString("Scoring Mode", xCoord*3, yCoord - 30);
		   for (int i = 0; i < 19; i++)
		   {
			  for (int j = 0; j < 19; j++)
			  {
				 if (aGame.visited[i][j] == true)
				 {
					 Rec = new Rectangle2D.Double( (int)x_coord, (int)y_coord, (int)SIZE, (int)SIZE);
				     g2.fill(Rec);
				 }
				 y_coord += SIZE;

			  }
			  y_coord = yCoord - diff + 8*RADIUS;
			  x_coord += SIZE;
		   }
	   }
	   x_coord = xCoord + 6*RADIUS;
	   y_coord = yCoord + 8*RADIUS;
	   Color color1 = new Color(0, 0, 0);
	   g.setColor(color1);

	   Line2D line;
	  for (int j = 0; j < 19; j++)
	  {
		 line = new Line2D.Double( (int)x_coord, (int)y_coord, (int)x_coord, (int)y_coord + (int)SIZE * 18);
		 g2.draw(line);
		 x_coord += SIZE;
	  }

	   x_coord = xCoord + 6*RADIUS;
	   y_coord = yCoord + 8*RADIUS;
	  for (int j = 0; j < 19; j++)
	  {
		 line = new Line2D.Double( (int)x_coord, (int)y_coord, (int)x_coord + (int)SIZE * 18, (int)y_coord);
		 g2.draw(line);
		 y_coord += SIZE;
	  }
  }

	private void drawcoord_p(Graphics g, Graphics2D g2)
	{
		g2.setFont(new Font("Serif", Font.PLAIN, COORD_SIZE));

		Color coord = new Color(40, 40, 40);
		g.setColor(coord);

		int k;
		for (k = 0; k < 19; k++)
			g2.drawString(""+k, k*SIZE + xCoord + 6*RADIUS - 3, yCoord + 8*RADIUS - 10);
		for (k = 0; k < 19; k++)
			g2.drawString(""+k, xCoord + 6*RADIUS - 22, k*SIZE + yCoord + 8*RADIUS + 5);

	}

	private void drawplayers_p(Graphics g, Graphics2D g2)
	{
		g2.setFont(new Font("Serif", Font.BOLD, FONTSIZE));
    	Color black = new Color(0, 0, 0);
		g.setColor(black);
		g2.drawString(black_role, xCoord + 6*RADIUS, 20 + 8*RADIUS);
		Color white = new Color(200, 200, 200);
		g.setColor(white);
		g2.drawString(white_role, xCoord + 6*RADIUS, 440 + 8*RADIUS);
    }

    private void drawstones_p(Graphics g, Graphics2D g2)
    {
		Board aBoard = aGame.getBoard();


		  for (int i = 0; i < 19; i++)
		  {
			 for (int j = 0; j < 19; j++)
			 {

				 if (aBoard.getStone(i, j).getColor() != -1)
				 {
					int xC = (i * SIZE) + xCoord + 6*RADIUS;
					int yC = (j * SIZE) + yCoord + 8*RADIUS;
					int diam = 2 * RADIUS;
					Ellipse2D.Double ells = new Ellipse2D.Double(xC - RADIUS, yC - RADIUS, diam, diam);

					 int color = (aBoard.getStone(i, j)).getColor();
					 if(color == 0)
						g2.setPaint(Color.black);
					 else
						g2.setPaint(Color.white);

					g2.fill(ells);
				}
			 }
		 }
	}
}


