2 Replies - 6188 Views - Last Post: 24 July 2011 - 12:08 PM Rate Topic: -----

#1 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

How to add a Pieces of Checkers to 2 D grid

Posted 21 July 2011 - 05:20 AM

Hi Everyone, Am currently working on a Checkers game (English draughts) to upgrade my java skills, But am having Slight difficulties placing the pieces into the Board and I am unsure of how i could go about it, i know i would have to store all the pieces into an array then loop through the array and add it to the Board But since the pieces are all JComponent (Filloval) i dont know how i could store Jcomponents into an array, Is it possible to add a number of fillovals into an Arraylist?. i tried this but it doesn't work, I would appreciate any Advice of how i could go about it?

cheers

   //Pieces Class
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class Pieces extends JComponent{

	ArrayList A;
	int k[][];
	int x;
	int y;
	Color color;

	public Pieces(int cx, int cy, Color Piececolor)
	{
		this.x = cx;
		this.y = cy;		  
		this.color = Piececolor;
		A = new ArrayList();
	}

	public void paint(Graphics g)
	{
		int mWidth = getWidth();
		g.setColor(Color.red);
		
		for(int i = 0; i <g.hashCode();i++){
			g.fillOval(0, 1, mWidth/8, mWidth/8); //THE PIECES
			g.fillOval(0, 2, mWidth/8, mWidth/8);
			g.fillOval(0, 3, mWidth/8, mWidth/8);
			g.fillOval(0, 4, mWidth/8, mWidth/8);
			g.fillOval(0, 5, mWidth/8, mWidth/8);
			g.fillOval(0, 6, mWidth/8, mWidth/8);
			g.fillOval(0, 7, mWidth/8, mWidth/8);
			g.fillOval(0, 8, mWidth/8, mWidth/8);
			A.add(g);
		}
	}
}




//Board Class 

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JComponent;

public class Board extends JComponent {

	int k = 0;
	int row; 
	int col; 

	public Board(int x, int y)
	{
		this.row = x;
		this.col = y;	
	}
	/*...........................................................................*/

	void drawColor()
	{
		k = (k + 1) % 2; 
	}	

	public void paint(Graphics g)
	{
		int mWidth = getWidth();
		int kHeight = getHeight();

		for (int m = 0; m <= row; m++)
		{
			g.drawLine(0, m * 60, col * 60, m * 60);

			for (int s = 0; s <= col; s++)
			{
				g.drawLine(s * 60, 0, s * 60, row * 60);
			}	
			/*...........................................................................*/

			for (int i=0; i<row; i=i+2)
			{
				for (int j=0; j<col; j=j+2)
				{
					if(k == 0)
					{
						g.setColor(Color.black);
						this.repaint();
					}	

					if(k==1)
					{
						g.setColor(Color.orange);
						this.repaint();			
					}
					g.fillRect(j*mWidth/8,(1+i)*mWidth/8,mWidth/8,kHeight/8);
					g.fillRect((1+j)*mWidth/8,i*mWidth/8,mWidth/8,kHeight/8);    
				}
			}
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to add a Pieces of Checkers to 2 D grid

#2 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: How to add a Pieces of Checkers to 2 D grid

Posted 21 July 2011 - 07:20 AM

Either JComponent[][] or ArrayList<JComponent> if that's how you want to do it. Considering you aren't using it, I'm going to guess you haven't touched on generics. Basically, you would say ArrayList<JComponent> list = new ArrayList<JComponent>(); when you want a new ArrayList that can hold only Objects of type JComponent which is defined inside of the less than and greater than signs. That's the bare minimum of generics anyway.

This post has been edited by TFoSSDQ: 21 July 2011 - 07:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 sultan144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 24-June 10

Re: How to add a Pieces of Checkers to 2 D grid

Posted 24 July 2011 - 12:08 PM

Cheers I done it :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1