hrz*_*rza 1 java icons swing drag-and-drop netbeans-platform
我正在开发一些NetBeans平台应用程序,目前我仍然坚持使用Visual Library中的一些细节.好的,这是问题.我有我的应用程序的Visual Editor,托盘,场景和一切都很好,只是当我从托盘拖动图标时出现问题.在拖动事件期间不显示它们,我想创建该效果,有人可以帮忙吗?
我分两个阶段做到这一点:
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();
Run Code Online (Sandbox Code Playgroud)
2)在接收视图上绘制屏幕截图.当识别出拖动手势时,找到一种方法使屏幕截图可用于接收视图或其祖先之一(您可以在框架或其内容窗格中使其可用,具体取决于您希望屏幕截图可用的位置)并在paint方法中绘制图像.像这样的东西:
一个.使截图可用:
capturedDraggedNodeImage = view.getScreenshot(); // Transfer the screenshot
dragOrigin = SwingUtilities.convertPoint(e.getComponent(), e.getDragOrigin(), view); // locate the point where the click was made
Run Code Online (Sandbox Code Playgroud)
湾 拖动鼠标时,更新屏幕截图的位置
// 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());
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
你可以调用paintImmediately()来调用paintImmediately(),而不是调用repaint()并在paint()方法中执行绘制,但渲染会更糟,你可以观察到一些闪烁,所以我不推荐这个选项.使用paint()和repaint()可以提供更好的用户体验和流畅的渲染.