Page 1 of 1

Java-Checkers Program

Posted: Thu May 25, 2006 9:59 am
by noxiousraccoon
Does anyone here know Java? If so, for some reason I cant get anything to pop up on the command prompt. If someone is willing to help, I would be very happy.
The script isnt very long, but its 2 files

First File:

Code: Select all

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUI extends JFrame implements ActionListener
{
	private int row_height=0;
	private int col_height=0;
	private int col=0;
	private int row=0;
	private boolean xturn=true;
	private JButton exit=new JButton("exit");
	private CheckersButton[][] checkers=new CheckersButton[8][8];
	private int t=0;
	
	public GUI()
	{
		super("My Game");
		setSize(750,800);
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Container contentPane= getContentPane();
		contentPane.setLayout(null);
		contentPane.setBackground(Color.blue);
		
		exit.addActionListener(this);
		exit.setBounds(5,5,100,100);
		contentPane.add(exit);
		
		for(row=0; row<8; row++)
		{
			row_height+=75;
			col_height=0;
			for(col=0; col<8; col++)
			{
				col_height+=75;
				checkers[col][row]=new CheckersButton(col,row);
				checkers[col][row].setBounds(col_height,row_height,75,75);
				checkers[col][row].setBackground(Color.black);
				contentPane.add(checkers[col][row]);
				
				if(col+row%2==0 || col+row%2==2 || col+row%2==4
				|| col+row%2==6 || col+row%2==8)
				{
					checkers[col][row].setColor(0);
					checkers[col][row].setBackground(Color.red);
					
				}
				if(col+row%2==0 || col+row%2==2)
				{
					checkers[col][row].setColor(1);
					checkers[col][row].setBackground(Color.yellow);
					
				}
				if(col+row%2==6 || col+row%2==8)
				{
					checkers[col][row].setColor(2);
					checkers[col][row].setBackground(Color.gray);
				}
				if(col+row%2==1 || col+row%2==3 || col+row%2==5
				|| col+row%2==7)
				{
					checkers[col][row].setEnabled(false);	
				}
				
			}
		}
		
	}
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource() instanceof CheckersButton)
		{
			CheckersButton clicked=(CheckersButton)event.getSource();
			
			System.out.println(clicked.getColumn()+" , "+clicked.getRow());
		}
		
		if(event.getSource() instanceof JButton)
		{
			JButton click=(JButton)event.getSource();
			System.exit(0);
		}
	}
	public static void main(String args[])
	{
		GUI thegame=new GUI();
		thegame.setVisible(true);
	}
}

Second File:

import javax.swing.*;

public class CheckersButton extends JButton
{
	private int column;
	private int row;
	private int value=0;
	
	public CheckersButton(int col, int row2)
	{
		column=col;
		row=row2;
	}
	public int getColumn()
	{
		return column;
	}
	public int getRow()
	{
		return row;
	}
	public void setColor(int value)
	{
		this.value=value;
	}
	public int getColor()
	{
		return value;
	}
	
}

Posted: Thu May 25, 2006 11:01 am
by [cc]z@nd!
check out the programmer's paradise forum at maximumpc.com.

they WILL be able to help you.

Posted: Thu May 25, 2006 8:54 pm
by The_Hushed_Casket
Hey, something I actually know. I'm not sure what you mean though, like your System.out.print's aren't printing?

And by the way, this will be a problem:

Code: Select all

if(event.getSource() instanceof CheckersButton)
      {
         CheckersButton clicked=(CheckersButton)event.getSource();
         
         System.out.println(clicked.getColumn()+" , "+clicked.getRow());
      }
      
      if(event.getSource() instanceof JButton)
      {
         JButton click=(JButton)event.getSource();
         System.exit(0);
      } 
Since your CheckerButton extends JButton, the second if statement will return true because your CheckerButton is also an instance of JButton, and this will make your application exit on the System.exit

Posted: Thu May 25, 2006 11:14 pm
by noxiousraccoon
The_Hushed_Casket wrote:Hey, something I actually know. I'm not sure what you mean though, like your System.out.print's aren't printing?
Yea, for some reason inside of the:

Code: Select all

if(event.getSource() instanceof CheckersButton) 
      { 
         CheckersButton clicked=(CheckersButton)event.getSource(); 
          
         System.out.println(clicked.getColumn()+" , "+clicked.getRow()); 
      }
No printlns will appear on the command prompt, but if I put them inside:

Code: Select all

if(event.getSource() instanceof JButton) 
      { 
         JButton click=(JButton)event.getSource(); 
         System.exit(0); 
      } 
They will print out, I dont know why. The problem you were talking about works, both the if statements work, my teacher showed me it at school, but I still get this system.out.println problem. (I use JCreator by the way)

Posted: Fri Jul 28, 2006 1:27 am
by noxiousraccoon
Ok, guys, sorry for the long bump. Im still having problems with this though.