2012-05-02 20 views
1

我正在研究一些NetBeans平台应用程序,并且我目前在可视库中停留了一些细节。好的,这是问题。我有我的应用程序的可视化编辑器,托盘,场景和一切都很好,只是当我将图标从托盘拖到场景时出现问题。他们不显示在拖动事件期间,我想创造这种效果,有人可以帮助吗?如何在拖放过程中显示图标

回答

1

如果我听到您的声音很好,您正在创建某种图形编辑器,并拖放了元素,并且您希望在拖放过程中创建效果?

如果是这样,你基本上需要创建一个你正在拖动的对象的鬼魂并将它附加到鼠标的移动。当然,说起来容易做起来难,但你明白了。 所以你需要的是把你拖动的图像(它不应该太麻烦),并根据鼠标的位置来移动它(想想减去鼠标光标在对象中的相对位置你在拖)。

但我认为那种代码是可用的地方。我建议你看看,最多:
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/

希望帮助你!

4

我为此在两个阶段:

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)在接收视图上绘制屏幕截图。当识别出拖动手势时,找到一种方法将截图提供给接收视图或其祖先之一(您可以在框架或其内容窗格中使其可用),这取决于您想要使截图拖动可用的位置)并在绘画方法中绘制图像。类似这样的:

a。提供截图:

capturedDraggedNodeImage = view.getScreenshot(); // Transfer the screenshot 
dragOrigin = SwingUtilities.convertPoint(e.getComponent(), e.getDragOrigin(), view); // locate the point where the click was made 

b。当拖动鼠标时,更新屏幕截图的位置

// 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方法:

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); 
} 

而不是调用重绘(中)和paint()方法执行绘画,你可以调用paintImmediately()随着鼠标移动,但渲染会有很多穷,你可以观察到一些闪烁,所以我不会推荐这个选项。使用paint()和repaint()可以提供更好的用户体验和平滑的渲染。

相关问题