هذا هو شفرة البرنامج لتعم الفائدة.... مكون من صفان أساسيان لشغيل البرنامج وصف آخر بسيط لجعله بريمج.....
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class ButtonBoard extends JFrame{ ///Just a Bourd countaining Buttons
//Just Run the class and see
//This class is used by other programs to make it easy to use a Button Bourd Like Queens
private JButton button[][];
private int row,col;
ButtonBoard(int row,int col){ //constructor
this.row=row;
this.col=col;
getContentPane().setLayout(new GridLayout(row,col));//layout, all companents layed all equal sizes in a table, like a spread sheet
button=new JButton[row][col];//creating the Array, But still empty.
int x=0;
for(int i=0;i<button.length;i++)
for(int o=0;o<button[i].length;o++){ //filling the Array, and laying it on the contentPane
button[i][o]=new JButton(""+(++x));
getContentPane().add(button[i][o]);
}
pack(); //(Very Good)Shrinks the Frame to the Preffered Size, according to all its components
setDefaultCloseOperation(EXIT_ON_CLOSE);//If you close the Frame, the Program will exit
}
public void addActionListener(ActionListener al){//This Method Allows any body who can listen to action Events to listen to all Buttons
for(int i=0;i<button.length;i++)
for(int o=0;o<button[i].length;o++)
button[i][o].addActionListener(al);
}
public JButton getButton(int row,int col){//returns a button based on a row and colomn
return button[row][col];
}
public JButton getButton(int index){//returns a button based on an Index
return (JButton)(getContentPane().getComponent(index));
}
public int getRow(JButton b){// returns the row of a button
for(int i=0;i<button.length;i++)
for(int o=0;o<button[i].length;o++)
if(b==button[i][o])return i;
return -1;
}
public int getCol(JButton b){//returns the col
for(int i=0;i<button.length;i++)
for(int o=0;o<button[i].length;o++)
if(b==button[i][o])return o;
return -1;
}
public static void main(String[]a){// A Test Main Method, Run it and see
ButtonBoard b=new ButtonBoard(16,18);//creating the Button Board
b.show();//displaying
System.out.println(b.getButton(1,1).getText());
System.out.println(b.getButton(1).getText());
}
public int getIndex(JButton b){//returns the Index of a button
return Integer.parseInt(b.getText());
}
}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
class Queens implements ActionListener{//We want to listen to all Buttons
// a program that uses a Button Board to act as a Chess Board
//Put 8 Queens in the Board with out them threatning each other
int max=8;
int queenNo=0;
ButtonBoard bb;
Color normalColor=Color.gray;//The button is empty. These colors reflecting the type of button
Color queenColor=Color.red;// The Button has a queen
Color wayColor=Color.green;// The Button is in a way of a queen.
Vector v;//the collection of the queen buttons
Queens(boolean autoShow){//constuctor, When we create a new Object shall we show the Frame automatically or wait.
bb=new ButtonBoard(8,8);//creating a Button Board
bb.addActionListener(this);//This Object will listen to all Buttons
bb.setTitle("Hussam's Queens Problem.");
for(int i=0;i<64;i++)//changing the COlor of all
bb.getButton(i).setBackground(normalColor);
v=new Vector();
if(autoShow)show();
//to change the Title window each 3 sec.
javax.swing.Timer t=new javax.swing.Timer(3000,new ActionListener(){
public void actionPerformed(ActionEvent ae){
if (bb.getTitle().equals("Hussam's Queens Problem."))bb.setTitle("Place 8 Queens on the chess table");
else bb.setTitle("Hussam's Queens Problem.");
}
});
t.start();//to start the timer, to change the title every 3 sec
}
public void show(){
bb.show();
}
public void actionPerformed(ActionEvent e){//receives Events for the Buttons
JButton b=(JButton)e.getSource();
if(b.getBackground().equals(normalColor))v.add(b);//if it is normal (not a queen button neither a way button) then make it a queen button and add it
else if(b.getBackground().equals(queenColor))v.remove(b);//if it is a queen button then cancel it and make it normal
else return;//if it was a green button
setWays();//check and make all the souronding buttons to be Ways (Green) so that they canot be choosen
if(v.size()==8)JOptionPane.showMessageDialog(null,"You won. 8 Queens are on the table.");
}
public static void main(String[]a){
Queens q=new Queens(true);// new Game and auto show
//q.show(); //use this if it is set to manual show;
}
private void setQueen(int row,int col){//make the Button in the row and colomn specified to be a queen button
bb.getButton(row,col).setBackground(queenColor);
}
private void setWays(JButton b,Color c){//the starting method for calculating
int row,col,index;
row=bb.getRow(b);
col=bb.getCol(b);
index=bb.getIndex(b);
setWay(row,col,8,c);
setWay(row,col,-8,c);
setWay(row,col,1,c);
setWay(row,col,-1,c);
setWay(row,col,9,c);
setWay(row,col,-9,c);
setWay(row,col,7,c);
setWay(row,col,-7,c);
}
private void clearWays(){
for(int i=0;i<64;i++)
bb.getButton(i).setBackground(normalColor);
}
private void setWays(){
clearWays();
Iterator r=v.iterator();
while(r.hasNext())
setWays((JButton)r.next(),wayColor);
r=v.iterator();
while(r.hasNext())
((JButton)r.next()).setBackground(queenColor);
}
private void setWay(int row,int col,int space,Color c){//recursive method calculating
try{
bb.getButton(row,col).setBackground(c);
switch(space){
case(1):col++;break;
case(-1):col--;break;
case(8):row++;break;
case(-8):row--;break;
case(9):col++;row++;break;
case(-9):col--;row--;break;
case(7):col--;row++;break;
case(-7):col++;row--;break;
}
setWay(row,col,space,c);
}catch(ArrayIndexOutOfBoundsException e){return;}
}
}
import javax.swing.*;
public class QueensApplet extends JApplet{
public void init(){
new Queens(true);
}
}