2012-05-02 35 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()可以提供更好的用戶體驗和平滑的渲染。

相關問題