我正在开发一些NetBeans平台应用程序,目前我正忙于处理可视化库中的一些细节。好了,问题来了。我有我的应用程序的可视化编辑器,与托盘,场景和一切都很好,只是有一个问题,当我拖动图标从托盘到场景。它们在拖拽事件期间不会显示,我想要创建该效果,有人能帮我吗?
发布于 2012-05-02 18:48:25
我分两个阶段完成这项工作:
1)创建调色板元素的屏幕截图(图片)。我懒惰地创建屏幕截图,然后将其缓存到视图中。要创建屏幕截图,您可以使用以下代码片段:
screenshot = new BufferedImage(getWidth(), getHeight(), java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE);// buffered image
// creating the graphics for buffered image
Graphics2D graphics = screenshot.createGraphics();
// We make the screenshot slightly transparent
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
view.print(graphics); // takes the screenshot
graphics.dispose();2)在接收视图上绘制截图。识别拖动手势后,找到一种方法使屏幕截图对接收视图或其上级视图可用(您可以使其在框架或其内容窗格上可用,具体取决于要使屏幕截图拖动可用的位置),并在paint方法中绘制图像。如下所示:
a.提供截图:
capturedDraggedNodeImage = view.getScreenshot(); // Transfer the screenshot
dragOrigin = SwingUtilities.convertPoint(e.getComponent(), e.getDragOrigin(), view); // locate the point where the click was madeb.拖动鼠标时,更新屏幕截图的位置
// Assuming 'e' is a DropTargetDragEvent and 'this' is where you want to paint
// Convert the event point to this component coordinates
capturedNodeLocation = SwingUtilities.convertPoint(((DropTarget) e.getSource()).getComponent(), e.getLocation(), this);
// offset the location by the original point of drag on the palette element view
capturedNodeLocation.x -= dragOrigin.x;
capturedNodeLocation.y -= dragOrigin.y;
// Invoke repaint
repaint(capturedNodeLocation.x, capturedNodeLocation.y,
capturedDraggedNodeImage.getWidth(), capturedDraggedNodeImage.getHeight());c.在paint method中绘制截图:
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.drawImage(capturedDraggedNodeImage, capturedNodeLocation.x,
capturedNodeLocation.y, capturedDraggedNodeImage.getWidth(),
capturedDraggedNodeImage.getHeight(), this);
}您可以在鼠标移动时调用paintImmediately(),而不是调用repaint()并在paint()方法中执行绘制,但渲染效果会差得多,并且可能会观察到一些闪烁,因此我不建议使用该选项。使用paint()和repaint()可以提供更好的用户体验和流畅的渲染效果。
发布于 2012-05-02 18:31:38
如果我没听错的话,您正在创建一个带有拖放元素的图形编辑器,您想在拖放过程中创建一个效果吗?
如果是这样的话,你基本上需要创建一个你正在拖动的对象的重影,并将其附加到鼠标的移动。当然,说起来容易做起来难,但你明白大意了。因此,您需要的是获取您正在拖动的对象的图像(应该不会有太多麻烦),并根据鼠标的位置移动它(考虑减去鼠标光标在您正在拖动的对象中的相对位置)。
但我认为这种代码在某个地方是可用的。我建议你去查一下:
http://free-the-pixel.blogspot.fr/2010/04/ghost-drag-and-drop-over-multiple.html
http://codeidol.com/java/swing/Drag-and-Drop/Translucent-Drag-and-Drop/
希望能对你有所帮助!
https://stackoverflow.com/questions/10411191
复制相似问题