Skip to Content
Swing Hacks
book

Swing Hacks

by Joshua Marinacci, Chris Adamson
June 2005
Intermediate to advanced
544 pages
22h 24m
English
O'Reilly Media, Inc.
Content preview from Swing Hacks
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);
354
|
Chapter 9, Drag-and-Drop
#69 Translucent Drag-and-Drop
HACK
This glass pane has two properties: the picture to be displayed and its loca-
tion. To make the picture translucent, use an
AlphaComposite instance.
Besides colors, strokes, and painters, Java2D drawings can be affected by
composites that define how the newly drawn pixels are mixed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Swing GUI Programming

Learning Swing GUI Programming

Brian Cole
Java Swing

Java Swing

Robert Eckstein, Marc Loy, Dave Wood
JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition

JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition

Kathy Walrath, Mary Campione, Alison Huml, Sharon Zakhour

Publisher Resources

ISBN: 0596009070Purchase bookErrata Page