Java-Checkers Program

Off topic chat. Basically anything that doesn't concern halo or halo modding can go here.
Post Reply
User avatar
noxiousraccoon




Wordewatician 250

Posts: 441
Joined: Wed May 17, 2006 2:54 pm

Java-Checkers Program

Post 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;
	}
	
}
User avatar
[cc]z@nd!




Literarian 500

Posts: 2297
Joined: Tue May 04, 2004 1:52 pm
Location: michigan

Post by [cc]z@nd! »

check out the programmer's paradise forum at maximumpc.com.

they WILL be able to help you.
ASPARTAME: in your diet soda and artificial sweeteners. also, it's obviously completely safe. it's not like it will cause tumors or anything. >.>
always remember: guilty until proven innocent
The_Hushed_Casket





Posts: 1698
Joined: Sun Nov 07, 2004 12:13 pm

Post 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
Image
User avatar
noxiousraccoon




Wordewatician 250

Posts: 441
Joined: Wed May 17, 2006 2:54 pm

Post 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)
User avatar
noxiousraccoon




Wordewatician 250

Posts: 441
Joined: Wed May 17, 2006 2:54 pm

Post by noxiousraccoon »

Ok, guys, sorry for the long bump. Im still having problems with this though.
Post Reply