Home Answers Viewqa Swing-AWT provide code

 
 


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

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 Pages:
please provide the code
please provide the code   Develop a multi-client chat application
Please provide the code
Please provide the code   Write a program to recursively download the web pages available in the given site URL
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   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..,   
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
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
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 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
code
code  please provide code for custom tags.by using currdate tag we need to get current date?please give me code
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
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
pls provide steps to connect mysql database in flex - XML
pls provide steps to connect mysql database in flex  hi, pls provide steps to connect mysql database in flex.pls i am trying from last week still it was not connected .pls provide the step by step procedure.i am looking
how to create text file from jsp and provide downlode option
how to create text file from jsp and provide downlode option  HI Deepak , thanks for your post u really doing great work for people who is new... then try the following code: 1)data.jsp: <%@ page import="java.sql.*, java.io.
code required
code required  I am making a blog in java using netbeans and database... and not the struts framework I want to provide option for the users to upload a photo(only .jpg,,png etc files) But i am not getting how to provide this option on the JSP
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
java code
java code  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
Map code
this can be done????????????/ can u help me to do so. plz provide a code which
Java code
Java code  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any... the following code: import java.util.*; import java.text.*; class NumberProgram
how to code this?
Provide a method that calculates the total of the order Provide a method... of the customerâ��s order. Provide getters and setters for the entr�©e and drink. Provide a method that calculates the total of the order Provide a method
how to code this?
: Create a class to keep track of the customerâ��s order. Provide getters and setters for the entr�©e and drink. Provide a method that calculates the total of the order Provide a method that prints the Entree menu Provide a method
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
jsp code for shopping cart
jsp code for shopping cart  please provide me the jsp code for online shopping cart
java code - Java Beginners
java code  how to check the locks on files on solaris server?can you please provide java code
java code - Java Beginners
java code  can u plz provide source code for the mini project of "online course portal for a campus
Hibernate Code - Hibernate
Hibernate Code   How to Invoke Stored Procedure in Hibernate???????? Plz provide details code
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
Pagination example with html code
Pagination example with html code  Hi, could u please provide pagination code with clear cut explanation. Thanks in advance
jdbc code - JDBC
jdbc code  are jdbc code for diferent programs are same or different?please provide me simple jdbc code with example?  Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/ Here you
Code - JSP-Servlet
Code  Using servlet,JSP,JDBC and XML create a Web application for a courrier company to provide online help in tracking the delivery status of items
java code - Java Beginners
java code  i m doing aproject on tsp can you provide me the entire source code for the problem in java for 10 cities   Hi Friend, Please send some more details. Thanks
code - Development process
after that,so if possible provide th code in java,post the code to:vpbtech@gmail.com  Hi Friend, Try the following jsp code: function
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
java code - Java Beginners
java code  plese provide code for the fallowing task Write a small record management application for a school. Tasks will be Add Record, Edit Record, Delete Record, List Records. Each Record contains: Name(max 100 char
hibernate code - Hibernate
hibernate code  sir, i have tried ur hibernate 3.0 firt example code, i have done as u have told in the tutorial like import all the related...(FirstExample.java:37) /hibernate.cfg.xml not found. plz provide me some sol
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
Need an example of basic code.
Need an example of basic code.   Im working on an art project that I want to use some code in, and I am proficient in python but that looks fairly... of the building blocks of my idea so if someone could provide an example I would
Need an example of basic code.
Need an example of basic code.   Im working on an art project that I want to use some code in, and I am proficient in python but that looks fairly... of the building blocks of my idea so if someone could provide an example I would
Need an example of basic code.
Need an example of basic code.   Im working on an art project that I want to use some code in, and I am proficient in python but that looks fairly... of the building blocks of my idea so if someone could provide an example I would

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.