Chess game variant - help using JLayeredPane and the drag layer.

Hi there!
I'm making a game based on chess, but with slightly different rules. The code at the bottom of this thread...
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=518707
... forms the basis of the game board, and this is how I have implemented the dragging and dropping of pieces. My game, however is slightly different as there is a single neutral piece (the ball), which players can manipulate by moving one of their pieces to the square containing the ball, then moving the ball to another location.
When a player clicks and drags a piece to the ball square, then releases, I want to attach the 'ball' JLabel to the mouse so that the player can move it around the board WITHOUT holding down the mouse button. Then, when the player clicks again the ball is moved to the new square.
The only problem I have is attaching the 'ball' to the mouse so I can move it around (without the user having to hold the mouse button down). I thought about tried using the drag layer, but I couldn't quite get that working. I also thought about using the mouseMoved(MouseEvent e) and mouseClicked(MouseEvent e) methods.
I'm pretty new to this coding stuff, so any suggestions would be great.
Thanks!

Using the drag layer isn't going to work unless you are using an honest to goodness component for the ball. Your other option might be to try to use the glasspane, which is a pain in the neck, or a floating window component.
In any case,I built the code below for dragging Windows around in a frame. You're free to use it but please site me in your school or business work
package tjacobs.ui;
import java.awt.*;
import java.awt.event.*;
public abstract class WindowUtilities {
public static Point getBottomRightOfScreen(Component c) {
     Dimension scr_size = Toolkit.getDefaultToolkit().getScreenSize();
     Dimension c_size = c.getSize();
     return new Point(scr_size.width - c_size.width, scr_size.height - c_size.height);
     public static class Draggable extends MouseAdapter implements MouseMotionListener {
Point mLastPoint;
Window mWindow;
public Draggable(Window w) {
w.addMouseMotionListener(this);
w.addMouseListener(this);
mWindow = w;
public void mousePressed(MouseEvent me) {
mLastPoint = me.getPoint();
public void mouseReleased(MouseEvent me) {
mLastPoint = null;
public void mouseMoved(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {
int x, y;
if (mLastPoint != null) {
x = mWindow.getX() + (me.getX() - (int)mLastPoint.getX());
y = mWindow.getY() + (me.getY() - (int)mLastPoint.getY());
mWindow.setLocation(x, y);
}

Similar Messages

  • HT1222 Why my iphone 5 still cannot install this update? I've clicked the download and install button but it do nothing, im using wifi and the baterry is 100% but still, it do nothing when i've clicked! Help!

    Why my iphone 5 still cannot install this update? I've clicked the download and install button but it do nothing, im using wifi and the baterry is 100% but still, it do nothing when i've clicked! Help!

    Reset the phone (no data loss): Press both the home and power buttons for at least 10 seconds, releasing when the Apple logo appears.  After the phone restarts, try updating again.

  • HT2534 how do i use itunes and the app store without any credit card or gift card?

    i just got my iphone and i used to use itunes all the time before when downloading all songs were completly free, how do i use itunes and the app store to find free stuff to download without having to add a credit card or gift card with my account?

    Create a NEW account/ID for her using these instructions. Make sure you follow the instructions. Many do not and if you do not you will not get the None option. You must use an email address that you have not used with Apple before. Make sure you specify a birthdate that results in being at least 13 years old
      Creating an iTunes Store, App Store, iBookstore, and Mac App Store account without a credit card
    More details of how:
      http://ipadhelp.com/ipad-help-tips-tricks/how-to-get-free-apps-from-the-app-stor e-without-a-credit-card/

  • I bought the Adobe student version of Photoshop and Premiere Elements a few months ago - and now I am trying to use it, and the serial number that I have is incorrect - or it is only the registration number or something? It says I should have been sent an

    I bought the Adobe student version of Photoshop and Premiere Elements a few months ago - and now I am trying to use it, and the serial number that I have is incorrect - or it is only the registration number or something? It says I should have been sent an email with the serial number once the product was registered - But I never received this email. If anyone is able to help me with this issue - that would be great. the registration number is: PEPEP6-5578-6450-8871-9107  I am using a windows based system

    If I were you I would first make sure CS6 was deactivated or signed-out. Then download Adobe's cleaner and remove cc, CS6 and any other creative  application installed including Creative Cloud desktop application.  Then install CS6 and use your CS6  serial number during the install to activate CS6 standard or extended which ever the serial number is for.   CS6 Version 13.0 should the be activated. That level of CS6 is extremely bug ridden so be sure to the use CS6 menu Help>Updates to get CS6 updates.  Windows should update to version 13.0.1.3 Mac to version 13.0.6.  Not Subscription CS6 version 13.1.2...
    Use the CC Cleaner Tool to solve installation problems | CC, CS3-CS6

  • I go to touch an app or game button on my iPhone and the screen blinks like its trying to open, but nothing happens. Why and what do I do?

    I go to touch an app or game button on my iPhone and the screen blinks like its trying to open, but nothing happens. Why and what do I do to fix it?

    Are there any apps in the muti-tasking bar? If the app closed unexpectedly and appeared in the muti-tasking bar, end it. Double tap the home button and then hold the app until it starts to wiggle then tap the minus at the top left at the app (if you want, end all the remaining apps)  try opening them now. Hope this helps! :)

  • I have just purchased a used 3gs and the previous owner hadnt reset it.

    I have just purchased a used 3gs and the previous owner hadnt reset it.
    So i did the reset and left it over night as this can take a few hours.
    This morning 10 hours later there is stillthe cirlcle going round in the centre of the screen.
    There is no sim card in the phone.
    What do i do now? please help?

    To make this phone truly yours, do a Restore as New...
    See this Support Article on Restoring your phone...
    http://support.apple.com/kb/HT1414

  • The disk containing your iPhoto library is running low on space.How is this possible? I have no photos and have never used iphoto and the program is up to date.

    How is this possible? I have no photos and have never used iphoto and the program is up to date. I try to import and drop my pictues into iphoto but nothing happens or I get above statement. Help please!

    Then it's a bogus message and indicates that the default library is damaged.
    Hold down the option (or alt) key key and launch iPhoto. From the resulting menu select 'Create Library' and use that one.
    Regards
    TD

  • TS3899 With Yahoo Mail, and anotare account (Inacap Mail), I can only receive mails but I can't send emails. I don't know if this is a problem of the iPad or it is a problem with yahoo mail, because using Gmail and the email of my job I don' have this pro

    With Yahoo Mail, and anotare account (Inacap Mail), I can only receive mails but I can't send emails. I don't know if this is a problem of the iPad or it is a problem with yahoo mail, because using Gmail and the email of my job I don' have this problem.

    Google them to confirm the settings that you need for the outgoing server, then check the setting you entered on the pad.  Pay real close attention to the outgoing server name, and port.  You may need to change in on the pad. 

  • When I went to bed I had 3,000  song and multiple playlists.  This morning I have ~30 songs and no playlists.  What could have happened to my songs playlists?  I run iTunes on a Windows 7 64-bit machine.  HELP! (oh, and the Songs are not in recycle bin)

    When I went to bed I had 3,000  song and multiple playlists.  This morning I have ~30 songs and no playlists.  What could have happened to my songs playlists?  I run iTunes on a Windows 7 64-bit machine.  HELP! (oh, and the Songs are not in recycle bin)
    My wife's ipod nano was plugged into the machine overnight.
    When she went running with it, there were no songs on it.  She checked the computer and was horrified to see that the playlists had disappeared and most of the songs had disappeared.
    QUESTIONS:
    1. What could it be?
    2. How can I fix it?

    Hi, there alot of us suffering from this phernomonon but there is a forum topic about is already.
    https://discussions.apple.com/message/16767377#16767377
    Come check it out.
    Hope this works out for all of us.

  • I purchased a used macbook and the other persons apple id information is still on this for example apple updates it wont allow me to update apps unless i am logged in with his email. how do i change that

    i got a used macbook and the other persons apple id is still connected to the macbook. for example i cant update apps unless i have his passwod but i dont wanna pay for apps such as iphoto and more. is there anything i can do? this is a macbook 13 inch late 2008 model

    You're going to have to purchase those apps for yourself. The seller shouldn't have left anything he had purchased on the computer when he sold it to you.

  • Can I sync two computers when one is using lion and the other snow leopard

    can I sync two computers when one is using lion and the other snow leopard

    will my data be shared well using Mobile Me?

  • Using iMovie and the voice over sound has stopped working?

    Using iMovie and the voice over sound has stopped working?

    Are these the internal speakers?
    Have you done a PRAM reset, CMD+Option+p+r...
    http://support.apple.com/kb/HT1379
    In fact, do 3 in a row, takes a bit of time.
    Intel-based Macs: Resetting the System Management Controller (SMC)...
    http://support.apple.com/kb/HT3964

  • I keep getting Alarm popups saying that it cannot send msg using the server null. I think I have disabled email (I use Gmail) and the calendar however I still get these popups and I can't close them?

    I keep getting Alarm popups saying that it cannot send msg using the server null.
    I think I have disabled email (I use Gmail) and the calendar however I still get these popups and I can't close them?
    How can I disable the Alarm popups?
    Thanks
    Brian

    OS X Mail: Troubleshooting sending and receiving email messages - Apple Support
    Google Mail recently implemented additional security measures "for your protection" of course. The manifestation of that may be the requirement to create a unique, "application-specific" password for each one of the various Google services you may use. That requirement probably includes Google Mail. So if the above Apple Support document doesn't resolve the problem, research Google's application-specific password requirements, and how to configure Mail to use it.
    I asked the Hosts to edit or obscure the email address in your post.

  • I purchased a used iphone4 and the phone displays the previous owners apple id and prompts me for the previous owners password. how do i get rid of his info....and install my own apple id number?

    I purchased a used iphone4 and the phone displays the previous owners apple id and prompts me for the previous owners password. how do i get rid of his info....and install my own apple id number?

    Login to the app store with YOUR ID. You can't use the app store without an Apple ID. If there are apps on the phone that were purchased with the original owners ID, you do NOT own them. The owner had no legal right to give them to you and they are permanently tied to the ID that was used to purchase them.
    ...i was also allowed to purchase an apple network id for this phone...
    I have absolutely NO IDEA what you mean by that. You can not "purchase an apple network id".

  • Using Delphi, and the InDesign automation object ("indesign

    Using Delphi, and the InDesign automation object ("indesign.application"), I need to open a indd file, move to a bookmark and insert text or picture at that exact location. Any examples of such code I could start with?

    Hi Bill,
    Sorry to be so late getting to this thread!
    If you want to add something to a page, or find an object that's on a page, you need a reference to the page. Simply displaying the page with ShowBookmark won't give you a reference to the page--InDesign scripting, for the most part, doesn't rely on the selection and current view in the way that Word scripting does (this is a good thing, but that's a topic for another day).
    You had to have a reference to a page to create the HyperlinkPageDestination that you used to create the bookmark--but, if not, you can get a reference to the page using (VBScript form):
    Rem Given a reference to an instance of InDesign "myInDesign"...
    myInDesign.Bookmarks.Item("Description").ShowBookMark
    Set myPage = myInDesign.ActiveWindow.ActivePage
    In general, though, you probably don't need the bookmark and the whole "ActiveWindow" construct--just use the reference to the page itself.
    Now that you have a page, you can put things on the page.
    Rem Create a rectangle
    Set myRectangle = myPage.Rectangles.Add
    Rem Size and position the rectangle
    myRectangle.GeometricBounds = Array("6p", "6p", "24p", "24p")
    Rem Place a graphic in the rectangle
    myRectangle.Place "c:\test.tif"
    If you have a text frame on the page that has the label "myLabel", you can get a reference to the text frame and add text:
    Set myTextFrames = myPage.TextFrames.Item("myLabel")
    Rem myTextFrames will return an array--get the first item
    Set myTextFrame = myTextFrames(0)
    Rem Replace the text in the text frame:
    myTextFrame.Contents = "This is some text."
    Rem Or add text to the end of the text frame:
    myTextFrame.InsertionPoints.Item(-1).Contents = "This is some additional text."
    If you know that there is only one text frame with the label "Description" in the entire document, you can even say:
    Set myTextFrames = myDocument.TextFrames.Item("myLabel")
    Rem myTextFrames will return an array--get the first item
    Set myTextFrame = myTextFrames(0)
    ...to get to it directly. If, at that point, you want to get at the page containing the text frame (and assuming that the text frame is not in a group, pasted inside text, or pasted inside another page item), you can use:
    Set myPage = myTextFrame.Parent
    Have you looked through the "Working with Documents" chapter of the InDesign CS3 Scripting Guide: VBScript and the associated scripts archive? I realize that you're using Delphi, but it doesn't look too difficult to convert from one to the other.
    Thanks,
    Ole

Maybe you are looking for

  • Adobe Premiere Elements 7.0 hangs while exporting and during peak file generation.

    I have Adobe Premiere Elements 7.0 (PRE7). I have a project which is using a single clip from a Mini-DV that was captured with Nero 8. Nero 8 was used because it was captured prior to installing PRE7. The file is 12GB and is on a USB disk drive becau

  • Sequence Panel and CTI lock-up - Solved, problem due to 48kHz .wav audio files

    Answering my own issue here, but maybe someone else will benefit as well... PC, Windows 7 Upon opening an ongoing project I found the sequence panel and CTI were immediately unresponsive.  The PC was also showing a high amount of background processin

  • BPM - Synch Asynch Process

    My Scenario is File to File and also SOAP thru BPM for File Split. I have to do BPM for file split because of the timing dependency of delivery of files. Here are the steps in Integration process: 1. Receive File from Sender A 2. Transform this file

  • Problem with reseting Meta Data

    hi everyone heres my problem... i keep all of my music on my external hard drive and i frequently clear my Itunes and reload all my music. When i do this i notice the my HOURS that ive spent formating the meta data for my music the way i like it has

  • Where is the "Load more messages..." button in Mail?

    My Mail icon has a badge showing unread messages since I did a "Continue search on server" but the usual button at the bottom of any inbox saying "Load more messages" is missing. As a result I am unable to view my old unread messages. I have restarte