1

I'm working on a domino game and it's going pretty well, now I want to drag a domino tile from one JPanel to another, my dragging implementation works, it's just that I can't find how to drag shapes between two jpanels.

Here's how it looks: enter image description here

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jesse
  • 1,332
  • 2
  • 14
  • 25
  • Looks like someone else has the same assignment: http://stackoverflow.com/questions/5474653/how-to-drag-and-drop-shape-between-two-panels – jzd Mar 30 '11 at 12:11
  • you mean how you can show the shape near the button while you drag it from one panel to the other? – javment Mar 29 '11 at 22:25
  • Right, the shape follows my mousepointer, it already works within one JPanel, just not from one JPanel to another. – Jesse Mar 29 '11 at 22:36
  • You must read about the glasspane,it's a jpanel that covers all the jframe so you can perfom the drag and drop effect(shape follows mouse pointer) with drawing into this panel. – javment Mar 29 '11 at 23:17
  • Nice, that's looks just like what I need, thnx! – Jesse Mar 29 '11 at 23:23
  • Also you can read a similar question from me(and my answer), at [link](http://stackoverflow.com/questions/4291089/java-drag-and-drop-an-image-and-show-the-image-thumbnail-while-dragging). – javment Mar 29 '11 at 23:30
  • I got the glassPane thing working, except when I try to draw an Image object in it, the background is grey, so I can't see what's underneath it. Obviously the background of the Image itself is grey. How do I make the background of a Image transparant? thnx. – Jesse Mar 30 '11 at 13:13
  • In the glasspane class have you set the setOpaque(false);? also in public void paintComponent(Graphics g) {} don't call the super.paitCompoment(g).When you mean an image? you mean an BufferedImage with grey bg? – javment Mar 30 '11 at 14:08
  • the JPanel I use for the glassPane is set to setOpaque(false), when I use g.fillOval(xxxx) or something it works fine. But when I use an Image or a BufferedImage object it sets the background to grey. It doesn't help to remove super.paintComponent(g). – Jesse Mar 30 '11 at 14:38
  • you mean that, when you use the g.drawImage() into paintCompoment() all the jpanel becomes grey?? – javment Mar 30 '11 at 14:57
  • yes, g.drawImage(), should have mentioned that before... since the glassPane covers the entire frame, it all becomes grey indeed. – Jesse Mar 30 '11 at 16:49
  • I don't understand why, can you paste some of the paintCompoment code. – javment Mar 30 '11 at 20:39
  • I managed to get rid off the white (it wasn't grey... crappy laptop screen) blackground, sort of anyway, what I did was, paint the dominoTile in a BufferedImage that's exactly the size of the tile, when you draw this image with drawImage() it will only draw the tile and no background, but it feels like a bit of a workaround and it sure complicates things. It would be nice to be able to just draw a tile on a transparant background in a Image object. Or is there a way to draw it directly on the JPanel, without the use of an Image object? – Jesse Mar 30 '11 at 22:01
  • `public void paintComponent (Graphics g) { panelImage = createImage(getWidth(), getHeight()); graphics = (Graphics2D)panelImage.getGraphics(); if(selectedTile != null) { selectedTile.fill(graphics); } g.drawImage(panelImage, 0, 0, null); if(curCursor != null) setCursor(curCursor); }` – Jesse Mar 30 '11 at 22:04
  • @javment (can't find some descent way to format the code, sorry) – Jesse Mar 30 '11 at 22:11
  • @javment I've got it! if you look in the code above you can see I create the Image object with the createImage() method from the existing JPanel, which has a white background, that's why my Image also has a white background.. duhhh... thank you very much for your help javment! – Jesse Mar 30 '11 at 22:37
  • Yes you create an image with same dimensions of the glasspane. Maybe you can improve the perfomance with not call the createImage() in every paintCompoment call, maybe if the size of glasspane not change often you can create a panelImage in the begin, and update the image when an resize in the panel occurs.Maybe, i'm not sure, but generally its better to have less code into the paintCompoment. – javment Mar 30 '11 at 23:47
  • you're right, but I can't clear the screen if I don't make a new Image object every time, I don't know how to clear an Image. I tried Image.flush() but that doesn't work. Performance wise I would not use this dragmethod again, it's a real performance hog, cpu goes to 100% while dragging or even moving the mouse (cause of all the mouseMoved events). – Jesse Mar 31 '11 at 08:26
  • Why clean the screen?you have a frame, you set the glasspane into that frame, and this glasspane is transparent panel which covers all the frame.In that panel you can draw the image, in some x,y location. When the mousedragged you simple chahge the values of x,y. – javment Apr 01 '11 at 13:18
  • @javment yes, that's what I do, but if I don't clear the screen/create a new Image object, the previous image won't be cleared, ending up with loads of images on the glasspane. – Jesse Apr 03 '11 at 10:56

1 Answers1

0

It is called Drop Location Rendering.

rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
  • It looks like Drop Location rendering uses a transferHandler, it would be a lot of work to adjust my code for this, is there another way to do it? The method I use now is just setting the shapes x/y coordinates to my mouse coordinates when I drag it. – Jesse Mar 29 '11 at 22:57