簡體   English   中英

Java Swing重新繪制圖像

[英]Java Swing Repaint an image

我想為Image實現一個DragAndDrop,但似乎無法讓Swing重繪功能在特定Image上工作。

碼:

public class playerFrame extends JFrame{
...
    private void destroyerImageMouseDragged(java.awt.event.MouseEvent evt)  
    }                                             
    repaintCurrentPosition(evt);
    }               

    public void repaintCurrentPosition(MouseEvent e){
        this.setLocation(e.getX(), e.getY());
        this.repaint();
    }

this.repaint < - 這個函數重新繪制整個幀,而不僅僅是我想要重繪的Image,大小約為50x50。 如何在不創建新類的情況下重新繪制特定的JPEG圖像?

謝謝。

this.repaint將強制重新繪制父框架。 僅在保持圖像的控件上調用repaint

示例:刷新加載到JLabel的圖像:

ImageIcon icon = createImageIcon("images/middle.gif");
label = new JLabel("Image and Text", icon, JLabel.CENTER);

你做:

label.repaint();
not just the Image I'd like it to repaint, which is about 50x50 size

JComponent#paint立即與EDT一起使用

您如何進行拖放?

最簡單的方法是只將一個Icon添加到JLabel,然后拖動標簽。 每次在標簽上調用setLocation(...)時,它都會重繪()本身。

Component Mover類為您完成了所有艱苦的工作。

僅在repaint圖像的面板上調用repaint

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM