
Translucent Drag-and-Drop #69
Chapter 9, Drag-and-Drop
|
353
HACK
solution is simply to display a translucent copy of the component itself,
referred to as a ghost in the code. Figure 9-10 shows a ghost in mid-drag.
This time, you will not use the package
java.awt.dnd; you’ll need your own
framework for this level of sophistication. By breaking out of the pre-built
box, you gain full control of drag-and-drop.
Drawing a Ghost
The very first thing you need is a glass pane able to draw a translucent pic-
ture over the UI. A glass pane is nothing more than a transparent
JPanel.
The code of
GhostGlassPane is very simple, as shown in Example 9-7.
Figure 9-10. A ghost is a translucent copy of a Swing component
Example 9-7. Creating a glass pane for ghosting
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class GhostGlassPane extends JPanel
{
private AlphaComposite composite;
private BufferedImage dragged = null;
private Point location = new Point(0, 0);