Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT provide code
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


chandrasekhar
provide code
2 Answer(s)      5 years and 11 months ago
Posted in : Swing AWT



Advertisement
View Answers

November 19, 2008 at 3:03 AM


Hi friend,

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

public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener
{
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;

public ChessGameDemo(){
Dimension boardSize = new Dimension(600, 600);

// Use a Layered Pane for this this application

layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize( boardSize );
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );

// Add a chess board to the Layered Pane

chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout( new GridLayout(8, 8) );
chessBoard.setPreferredSize( boardSize );
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

for (int i = 0; i < 64; i++)
{
JPanel square = new JPanel( new BorderLayout() );
chessBoard.add( square );

int row = (i / 8) % 2;
if (row == 0)
square.setBackground( i % 2 == 0 ? Color.blue : Color.white );
else
square.setBackground( i % 2 == 0 ? Color.white : Color.blue );
}

// Add a few pieces to the board

JLabel piece = new JLabel( new ImageIcon("/home/vinod/amarexamples/chess.jpg") );
JPanel panel = (JPanel)chessBoard.getComponent( 0 );
panel.add( piece );
piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/chess1.jpg") );
panel = (JPanel)chessBoard.getComponent(15);
panel.add(piece);
}

public void mousePressed(MouseEvent e){
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());

if (c instanceof JPanel)
return;

Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel)c;
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}


November 19, 2008 at 3:05 AM



// Move the chess piece around

public void mouseDragged(MouseEvent me)
{
if (chessPiece == null) return;

chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}


// Drop the chess piece back onto the chess board

public void mouseReleased(MouseEvent e)
{
if (chessPiece == null) return;

chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());

if (c instanceof JLabel)
{
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else
{
Container parent = (Container)c;
parent.add( chessPiece );
}

chessPiece.setVisible(true);
}

public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public static void main(String[] args)
{
JFrame frame = new ChessGameDemo();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable( false );
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}

-------------------------------------

visit for more information.

http://www.roseindia.net/java/example/java/swing/

Thanks.


Related Tutorials/Questions & Answers:
Provide the code please
Provide the code please   Program to calculate the sum of two big numbers (the numbers can contain more than 1000 digits). Don't use any library classes or methods (BigInteger etc
please provide the code
please provide the code   Develop a multi-client chat application
Advertisements
Please provide the code
Please provide the code   Write a program to recursively download the web pages available in the given site URL
Please provide the code
Please provide the code   Program to find depth of the file in a directory and list all files those are having more number of parent directories
Please provide the code
Please provide the code   Create a file system: We just have one file on the hard disk. But we can create directories, sub-directories and files in that file only
provide code - Swing AWT
provide code  Dear frnds please provide code for two player CHESS GAME.....using swings,awt concepts   Hi friend, import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public
please provide code - Java Beginners
please provide code  provide code to snake game in J2SE
provide me code - Java Beginners
provide me code  can any body provide code for SNAKE XENZIA game in NOKIA mobile? please urgently
provide code in servlets,javascript,jsp - JavaMail
provide code in servlets,javascript,jsp  i create mail like fallowing... radiobutton automatically. So frnds please understand my problem urgently provide... hope that, this link will help you. if you have any problem then send me code
Please provide the java code for the given program.
Please provide the java code for the given program.   We need an application for managing an educational institute. That application should provide the details of Students Courses Faculty Fee details etc..,   
Plz Provide correct program code for all questions.
Plz Provide correct program code for all questions.   Write a program to find the difference between sum of the squares and the square of the sums... the following code: 1) import java.util.*; class FindDifference { public static
Please provide jquery inline form validation for the code
Please provide jquery inline form validation for the code   <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Student Form</title> <script type
Please Provide the direct Downloadable source code for better understanding
Please Provide the direct Downloadable source code for better... clear.* As am a fresher, it is difficult for me to implement the code practically. I kindly request you to please provide the direct downloadable source code so
How to compile PHP file and provide it to user without code?
How to compile PHP file and provide it to user without code?  I have created a class in PHP, that contains some useful methods. Now I want to give... in asp.net instead of providing code file. How can I compile the PHP file and provide
pl z provide code for below question in javascripr or jsp - JSP-Servlet
pl z provide code for below question in javascripr or jsp  Once the parallel request happened, the user will not be able to continue his work, until the first request ends by itself and back to the home page   Hi
Pls provide me jsp code to get values in drop down list from another table's field....
Pls provide me jsp code to get values in drop down list from another table's field....  my project has customer registration and company registration........ pls provide me code.. thanx
provide source - Java Beginners
provide source  please provide code for this program Write a program to download a website from a given URL. It must download all the pages from... the files/pages must be stored in a folder.  Hi This is the source code
provide me the program for that ques.
provide me the program for that ques.  wtite a program in java there is a class readchar.the functionality of this class is to read a string from...;Hi Friend, You can try the following code: import java.util.*; class
provide example in J2ee/jsp - JSP-Servlet
provide example in J2ee/jsp  how to manage search options page in jsp using back end as oracle/sql? i am using multiple search options. so please provide code for this or tutorial
Please provide the coding for this problem
Please provide the coding for this problem  Please provide the coding for the following problem. Write a "hangman" game that randomly generates a word and prompts the user to guess one letter at a time. Each letter in the word
JDBC doesn't provide an EOF method.
JDBC doesn't provide an EOF method.  How can I know when I reach the last record in a table, since JDBC doesn't provide an EOF method
To provide Help Option - Java Beginners
To provide Help Option  hi i am writing one small application there i wanted to provide "Help" button to user.From there they will be geting how... theire own help article writen..." how we can provide our Help writen for our
Please provide coding for following problem
Please provide coding for following problem  Please provide the coding for the following problem: You will write a java program that will read data from a file. The data in the file will be: John Doe 75 Joe Blow 65 Mary Smith 80
code
code  please provide code for custom tags.by using currdate tag we need to get current date?please give me code
Please provide option for full materials download...
Please provide option for full materials download...   Please provide option for full materials download ... i dont have internet connection in my home. plz provide offline tutorial option
how the streams provide synchronization - Java Beginners
how the streams provide synchronization  How the java streams provides synchronization
Can you provide me Hibernate configuration tutorial?
Can you provide me Hibernate configuration tutorial?  Hello There, I want to learn about Hibernate configuration, Can anyone provide me with hibernate configuration tutorial> Thanks
How to build .XML file, please provide an example.
How to build .XML file, please provide an example.  Hello, I want to know how to build .XML file. If you can provide an example, that would be great... Thanks
Plz provide me all the material for Java
Plz provide me all the material for Java  Plz provide me all the material for Java   Please go through the following link: Java Tutorials
code
code to create the RMI client on the local machine: import java.rmi.*; public... : "+e); } } } However, when the preceding code is executed it results in a RemoteException. Identify the reason behind this exception. Also, provide
How to provide timeout condition in my project
How to provide timeout condition in my project  Hi I am doing online examination system project i want 2 set time for my exam after that system shuld declare timeout how to provide that plz
 

 

 

DMCA.com