Java Swing重绘图像

时间:2011-12-21 17:35:10

标签: java swing repaint

我想为一个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< - 这个函数重新绘制整个帧,而不仅仅是我想重新绘制的图像,大小约为50x50。 如何在不创建新类的情况下重新绘制特定的JPEG图像?

感谢。

4 个答案:

答案 0 :(得分:1)

this.repaint将强制重绘父框架。仅在保留图像的控件上调用repaint

示例:刷新加载到JLabel上的图像:

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

你这样做:

label.repaint();

答案 1 :(得分:1)

not just the Image I'd like it to repaint, which is about 50x50 size

JComponent#paintImmediately小心使用EDT

答案 2 :(得分:1)

你是如何进行拖放的?

最简单的方法是将图标添加到JLabel,然后拖动标签。每当你在标签上调用setLocation(...)时,它都会重绘()本身。

Component Mover课为你完成所有艰苦的工作。

答案 3 :(得分:0)

仅在绘制图像的面板上调用repaint