Total members 11895 |It is currently Sat Dec 28, 2024 1:40 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This is a Java J2SE Code for knight game that show knight image(Bondok) and the player must fill all positions ( Squares) of game board by moving the knight by allowable moves (It moves as horse in chess). if you filled all Board squers then you are winner.

-This is a netbeans project-

java code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;


public class AboutDialog extends JDialog implements ActionListener
{

/**
* Creates a new instance of AboutDialog
*/
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension d=kit.getScreenSize();
JLabel gameNameLabel;
JLabel developerNameLabel;
JLabel facultyNameLabel;
JLabel locationLabel;
JLabel developerMail;

JButton okButton;

public AboutDialog()
{
setIconImage(kit.getImage("Images/bn.gif"));
setTitle("Bondok - TETOSOFT - About");
setBounds((int)d.getWidth()/2-220,(int)d.getHeight()/2-200,300,250);
getContentPane().setBackground(Color.YELLOW);


gameNameLabel=new JLabel("Bondok--Ver 1.0");
gameNameLabel.setBounds(10,10,200,20);
developerNameLabel=new JLabel("Made by-> M.Talaat Saad");
developerNameLabel.setBounds(10,40,200,20);
facultyNameLabel=new JLabel("FCI");
facultyNameLabel.setBounds(10,70,250,20);
locationLabel=new JLabel("Cairo, Egypt");
locationLabel.setBounds(10,100,200,20);
developerMail=new JLabel("Mail-> ");
developerMail.setBounds(10,130,200,20);

okButton=new JButton("OK");
okButton.addActionListener(this);
okButton.setBounds(115,170,70,30);

add(gameNameLabel);
add(developerNameLabel);
add(facultyNameLabel);
add(locationLabel);
add(developerMail);
add(okButton);
add(new JLabel(""));
setVisible(true);
toFront();
}

public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();

if(obj==okButton)
{
setVisible(false);
dispose();
}
}

}


java code
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.Image;

public class BoardGamePanel extends JPanel
{
public Toolkit kit=Toolkit.getDefaultToolkit();
public Image Bondok=kit.getImage("Images/bn.gif");
public Bondok bond=new Bondok();

public boolean gameStarted;

public BoardGamePanel()
{
filled=0;
gameStarted=false;
move=true;
bond.mypic=Bondok;
bond.xposition=0;
bond.yposition=0;
bond.row=0;
bond.column=0;
action squarePressed=new action();
addMouseListener(squarePressed);

}
/////////
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
Rectangle2D rec;
setLocation(40,60);
setBackground(Color.WHITE);
setSize(320,320);
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));

// Drawing the Main Board

for (int i=0; i<8; i=i+2)
{
for (int j=0; j<8; j=j+2)
{
g2.setColor(new Color(0,115,0));
rec=new Rectangle2D.Double(j*40,(1+i)*40,40,40);
g2.fill(rec);
rec=new Rectangle2D.Double((1+j)*40,i*40,40,40);
g2.fill(rec);
}
}

// Drawing The borders of the Main Board

for(int i=0;i<9;i++)
{
g2.setPaint(Color.BLACK);
g2.draw(new Line2D.Double(40*i,0,i*40,320));
g2.draw(new Line2D.Double(0,i*40,320,i*40));
}

if(filled!=0)
g2.drawImage(bond.mypic,bond.xposition,bond.yposition,40,40 ,this);


// Drawing the filled Squared

for(int i=1;i<filled;i++)
{
g2.setPaint(new Color(120,0,0));
g2.fillOval(prevCol[i]*40+5,prevRow[i]*40+5,30,30);
}

if(help==true)
{
for(int i=0;i<8;i++)
{
if(possible_not_fillCol[i]==true&&possible_not_fillRow[i]
==true&&hintCol[i]!=prevCol[filled-1]&&hintRow[i]!=prevRow[filled-1])
{
g2.setPaint(new Color(0,0,255));
g2.fillOval(hintCol[i]*40+5,hintRow[i]*40+5,30,30);
}

}
}
}

///////
// MouseListener

private class action implements MouseListener
{
public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e)
{

X=e.getX();
Y=e.getY();
if(X<=320&&Y<=320)
{
if(gameStarted==true)
{
Fill_Moves();
Move(X,Y);

help=false;

if(move)
{
prevRow[filled]=bond.row;
prevCol[filled]=bond.column;
filled++;
repaint();
Find_Row_Col(X,Y);
Show_Possible_Moves(bond.row,bond.column);
move=true;
if(filled==64)
{
JOptionPane.showConfirmDialog(null,"You are won!",

"Bondok",JOptionPane.OK_OPTION,JOptionPane.INFORMATION_MESSAGE);
}
}
}
else
{
int response=JOptionPane.showConfirmDialog(null,

"Are you sure you want to start from this position?","Bondok",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

if(response==0) //yes
{
Find_Row_Col(X,Y);
bond.xposition=(bond.column*40);
bond.yposition=(bond.row*40);

prevRow[filled]=Y/40;
prevCol[filled]=X/40;

Show_Possible_Moves(bond.row,bond.column);
repaint();
filled++;
gameStarted=true;

}

}
}

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

}
////////
public class Bondok
{
int row;
int column;
public int xposition;
public int yposition;
public Image mypic;
}
/////
public void Find_Row_Col(int x ,int y)
{
bond.row=y/40;
bond.column=x/40;
}
//////
public void Show_Possible_Moves(int r,int c)
{
hintRow[0]=r-2;
hintCol[0]=c+1;

hintRow[1]=r-2;
hintCol[1]=c-1;

hintRow[2]=r+2;
hintCol[2]=c+1;

hintRow[3]=r+2;
hintCol[3]=c-1;

hintRow[4]=r-1;
hintCol[4]=c+2;

hintRow[5]=r-1;
hintCol[5]=c-2;

hintRow[6]=r+1;
hintCol[6]=c+2;

hintRow[7]=r+1;
hintCol[7]=c-2;

for(int i=0;i<8;i++)
{
valid[i]=true;
if(hintRow[i]>7||hintRow[i]<0||hintCol[i]>7||hintCol[i]<0)
valid[i]=false;
}
}
//////////////////////////
public void Fill_Moves()
{
boolean flag=true;
for(int i=0;i<8;i++)
{
for(int j=0;j<filled;j++)
if(hintRow[i]==prevRow[j]&&hintCol[i]==prevCol[j])
{
flag=false;
//break;
}

possible_not_fillCol[i]=flag;
possible_not_fillRow[i]=flag;
flag=true;

}

}
///////////////////////
public void Move(int a,int b)
{
boolean live=false;

for(int i=0;i<8;i++)
{
if(hintRow[i]==(b/40)&&hintCol[i]==(a/40)&&

possible_not_fillRow[i]==true&&possible_not_fillCol[i]==true&&valid[i]==true)
{
bond.xposition=((a/40)*40);
bond.yposition=((b/40)*40);
live=true;
move=true;
break;
}
}

if(live==false)
{
move=false;
JOptionPane.showConfirmDialog(null," Invalid Move ","Bondok Game.",

JOptionPane.OK_OPTION,JOptionPane.WARNING_MESSAGE);
}
}
////////////////////
public void DisplayPossibleMoves()
{
help=true;
repaint();
}
/////////////////
public void BackOneMove()
{
if(filled>1)
{
filled--;

bond.row=prevRow[filled];
bond.column=prevCol[filled];

bond.xposition=(bond.column*40);
bond.yposition=(bond.row*40);

Show_Possible_Moves(bond.row,bond.column);

repaint();
}
else
{
JOptionPane.showConfirmDialog(null,"You can not back more!","Bondok Game-TETOOSFT",JOptionPane.OK_OPTION);
}
}

///////////////////////

int hintRow[]=new int[8];
int hintCol[]=new int[8];

int prevRow[]=new int[100];
int prevCol[]=new int[100];

boolean possible_not_fillRow[]=new boolean[8];
boolean possible_not_fillCol[]=new boolean[8];

boolean valid []=new boolean[8];

private int filled;

private boolean help=false;
int X;
int Y;

private boolean move;

}


java code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class HelpDialog extends JDialog implements ActionListener
{

/** Creates a new instance of HelpDialog */
JLabel helpTextLabel;
JButton okButton;
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension d=kit.getScreenSize();
public HelpDialog()
{
setIconImage(kit.getImage("Images/bn.gif"));
setTitle("Bondok - TETOSOFT - About");
setBounds((int)d.getWidth()/2-220,(int)d.getHeight()/2-200,250,130);
getContentPane().setBackground(Color.YELLOW);
setLayout(null);
helpTextLabel=new JLabel("Bondok-TETOSOFT-Ver 1.0");

helpTextLabel.setBounds(30,10,160,30);

okButton=new JButton("OK");
okButton.setBounds(70,50,70,30);
okButton.addActionListener(this);

add(helpTextLabel);
add(okButton);

setVisible(true);
toFront();
}

public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();

if(obj==okButton)
{
setVisible(false);
dispose();
}
}
}

java code
import javax.swing.JFrame;



public class Main
{

/** Creates a new instance of Main */
public static void main(String[] args)
{
MainFrame frame=new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();

}

}

java code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.*;

public class MainFrame extends JFrame
{
BoardGamePanel gamePanel=new BoardGamePanel();

public MainFrame()
{
Toolkit kit=Toolkit.getDefaultToolkit();
Image image=kit.getImage("Images/bn.gif");

setTitle("Bondok Game < TETOSOFT > ");
setSize(590,500);
setLocation(100,60);
setIconImage(image);

JLabel title=new JLabel(">> TETOSOFT <<");
title.setBounds(130,10,350,30);
title.setFont(new Font("Comic Sans MS",Font.BOLD,35));
title.setForeground(Color.RED);
Container contentPane=getContentPane();
ToolPanel toolPanel=new ToolPanel();

contentPane.add(title);
contentPane.add(toolPanel);
contentPane.add(gamePanel);

JMenuBar JMBar=new JMenuBar();
setJMenuBar(JMBar);
JMenu FileItems=new JMenu("File");
JMBar.add(FileItems);

JMenuItem NewGame=new JMenuItem("New Game");
FileItems.add(NewGame);

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}


class ToolPanel extends JPanel implements ActionListener
{
JButton retryButton;
JButton exitButton;
JButton backButton;
JButton aboutButton;
JButton showPossMovesButton;
JButton pauseButton;
JButton helpButton;

JLabel timerLabel;
JLabel timerValueLabel;
JLabel scoreLabel;
JLabel scoreValueLabel;

Toolkit kit=Toolkit.getDefaultToolkit();
Image currentPosImg=kit.getImage("Images/current.gif");
Image coveredImg=kit.getImage("Images/coverd.gif");

public ToolPanel()
{
setLocation(390,60);
setSize(170,350);
setLayout(null);


retryButton=new JButton("Retry");
retryButton.setBounds(8,5,70,30);
retryButton.addActionListener(this);
exitButton=new JButton("Exit");
exitButton.setBounds(95,5,70,30);
exitButton.addActionListener(this);
backButton=new JButton("Back");
backButton.setBounds(8,45,70,30);
backButton.addActionListener(this);
aboutButton=new JButton("About");
aboutButton.setBounds(95,45,70,30);
aboutButton.addActionListener(this);
showPossMovesButton=new JButton("Some Possible Moves");
showPossMovesButton.setBounds(7,150,160,30);
showPossMovesButton.addActionListener(this);
pauseButton=new JButton("Pause");
pauseButton.setBounds(8,190,70,30);
pauseButton.addActionListener(this);
helpButton =new JButton("Help");
helpButton.setBounds(95,190,70,30);
helpButton.addActionListener(this);

timerLabel=new JLabel("Timer: ");
timerLabel.setFont(new Font("Comic Sans MS",Font.PLAIN,16));
timerLabel.setBounds(13,85,60,30);
timerValueLabel=new JLabel("0 Seconds");
timerValueLabel.setFont(new Font("Comic Sans MS",Font.BOLD,13));
timerValueLabel.setForeground(Color.RED);
timerValueLabel.setBounds(90,85,80,30);
scoreLabel=new JLabel("Score: ");
scoreLabel.setFont(new Font("Comic Sans MS",Font.PLAIN,16));
scoreLabel.setBounds(13,115,60,30);
scoreValueLabel=new JLabel("64 Squares Left");
scoreValueLabel.setFont(new Font("Comic Sans MS",Font.BOLD,13));
scoreValueLabel.setForeground(Color.RED);
scoreValueLabel.setBounds(65,115,110,30);


add(retryButton);
add(exitButton);
add(backButton);
add(aboutButton);
add(showPossMovesButton);
add(pauseButton);
add(helpButton);
add(timerLabel);
add(timerValueLabel);
add(scoreLabel);
add(scoreValueLabel);

}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;

g2.setFont(new Font("Comic Sans MS",Font.BOLD,14));

g2.drawImage(currentPosImg,8,230,45,44,this);
g2.drawImage(coveredImg,8,285,45,44,this);
g2.drawString("Current Position",60,255);
g2.drawString("Covered Position",60,310);
}
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();

if(obj==exitButton)
{
int response=JOptionPane.showConfirmDialog(null,
"Are you sure you want to exir ?","Bondok < TETOSOFT >",

JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

if(response==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}

if(obj==aboutButton)
{
AboutDialog aboutDialog=new AboutDialog();
}

if(obj==helpButton)
{
HelpDialog helpDialog=new HelpDialog();
}

if(obj==retryButton)
{
int response=JOptionPane.showConfirmDialog(null,

"Are you sure want to retray ?","Bondok Game-TETOSOFT",

JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(response==0)
{
gamePanel=new BoardGamePanel();
getContentPane().getComponent(2).setVisible(false);
getContentPane().add(gamePanel);
}
}

if(obj==backButton)
{
gamePanel.BackOneMove();
}
if(obj==showPossMovesButton)
{
gamePanel.DisplayPossibleMoves();
}
}

}

}





Attachments:
File comment: Bondok 2D java Game Project RAR
Bondok Game.rar [56.87 KiB]
Downloaded 967 times

_________________
Please recommend my post if you found it helpful


Last edited by tetosoft on Mon Jun 11, 2007 7:50 pm, edited 1 time in total.
Author:
Beginner
User avatar Posts: 109
Have thanks: 5 time

can i download your game project?
thanks


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

Attachment:
Bondok Game.rar [56.87 KiB]
Downloaded 1175 times


Here it is ,

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

thanks @msi_33


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

you are welcome. :)

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

msi_333 wrote:
you are welcome. :)


hello msi_333,

please follow this link http://www.codemiles.com/java/ball-color-match-2d-java-game-version-1-0-t394.html

need ur reply..thank you!


Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 7 posts ] 

  Related Posts  to : fill all Squares of game board - 2d Java Game Example
 ping pong game - java-Sticker-ball game (modified v1.1)     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 Snake Java (SE) Game     -  
 Chess Game in Java     -  
 Need help getting started on a small java game?     -  
 need help in java 1.4 snake game project     -  
 JChess game (2 players Java chess )     -  
 Ball Color Match 2D Java Game Version 1.0     -  
 ping pong java code-Sticker-ball game     -  
 XO-Game     -  



Topic Tags

Java Projects
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com