[2097] in java-interest
Dragging and dropping between windows
daemon@ATHENA.MIT.EDU (Ed Anuff)
Mon Sep 25 00:12:35 1995
Date: Sun, 24 Sep 1995 18:40:01 -0800
To: java-interest@java.sun.com
From: edanuff@protagonist.com (Ed Anuff)
I'm trying to find a solution for dragging and dropping between windows
using the JDK. I've found that you can simulate this by creating a new
window at the point where the user clicks on the icon they want to drag,
and then having that window be whats dragged. I have some code below that
does this, but has the problem that you cant just click on the object and
drag it - you need to first click on the object, then click on it again in
order to start a drag. The reason for this is that I cant find a way pass
the mouse click on to the new window, and it appears that once the new
window is created, events stop being sent to the old window. Has anybody
tried anything similar and found a solution?
Ed
---test code start---
import java.applet.*;
import java.awt.*;
public class DragTest extends Applet {
public void init() {
resize(500, 500);
add(new Button("New Window"));
}
public boolean action(Event evt, Object arg) {
if ("New Window".equals(arg)) {
TestWindow theWindow = new TestWindow();
}
return true;
}
}
class TestWindow extends Frame {
DragIcon theIcon = null;
TestWindow() {
super("Hello");
setLayout(new FlowLayout());
resize(120, 32);
add(new DragIcon());
add(new DragIcon());
add(new DragIcon());
show();
}
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.WINDOW_DESTROY:
dispose();
return true;
default:
return super.handleEvent(e);
}
}
}
class ComponentUtil {
public static Frame getFrame(Component theComponent) {
Component currParent = theComponent;
Frame theFrame = null;
while (currParent != null) {
if (currParent instanceof Frame) {
theFrame = (Frame)currParent;
break;
}
currParent = currParent.getParent();
}
return theFrame;
}
public static void LocalToGlobal(Component theComponent, Point
localPoint) {
Component currParent = theComponent;
Point thePoint = null;
while (currParent != null) {
thePoint = currParent.location();
localPoint.translate(thePoint.x, thePoint.y);
if (currParent instanceof Window) break;
currParent = currParent.getParent();
}
}
}
class DragIcon extends Canvas {
DragWindow theWin = null;
DragIcon() {
resize(32,32);
}
public void paint(Graphics g) {
Rectangle r = bounds();
g.drawRect(0, 0, r.width-1, r.height-1);
g.drawLine(0, 0, r.width, r.height);
g.drawLine(r.width, 0, 0, r.height);
g.drawLine(0, r.height / 2, r.width, r.height / 2);
g.drawLine(r.width / 2, 0, r.width / 2, r.height);
}
public boolean mouseDown(Event evt, int x, int y) {
if (theWin == null) {
theWin = new
DragWindow(ComponentUtil.getFrame(this), this);
theWin.resize(32, 32);
Rectangle theRect = bounds();
Point thePoint = new Point(x, y);
ComponentUtil.LocalToGlobal(this, thePoint);
theWin.move(thePoint.x - 16, thePoint.y - 16);
theWin.show();
}
return true;
}
}
class DragWindow extends Window {
Component theOwner;
boolean dragging = false;
DragWindow(Frame theFrame, Component owner) {
super(theFrame);
theOwner = owner;
add("Center", new TestCanvas());
}
public void paint(Graphics g) {
Rectangle theRect = bounds();
g.fillOval(0, 0, theRect.width, theRect.height);
}
public void DoDrop() {
((DragIcon)theOwner).theWin = null;
dispose();
}
public boolean mouseUp(Event evt, int x, int y) {
DoDrop();
return true;
}
synchronized public void centerAt(int x, int y) {
Point thePoint = new Point(x,y);
ComponentUtil.LocalToGlobal(this, thePoint);
move(thePoint.x - 16, thePoint.y - 16);
}
public boolean mouseDrag(Event evt, int x, int y) {
centerAt(x,y);
return true;
}
}
class TestCanvas extends Canvas {
public TestCanvas() {
}
public void paint(Graphics g) {
Rectangle r = bounds();
g.drawRect(0, 0, r.width-1, r.height-1);
g.drawLine(0, 0, r.width, r.height);
g.drawLine(r.width, 0, 0, r.height);
g.drawLine(0, r.height / 2, r.width, r.height / 2);
g.drawLine(r.width / 2, 0, r.width / 2, r.height);
}
}
---test code end---
Ed Anuff
Protagonist Interactive
edanuff@protagonist.com
http://www.protagonist.com/
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com