Java-Checkers Program
Posted: Thu May 25, 2006 9:59 am
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:
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;
}
}