I'd like to implement a DragAndDrop for an Image but can't seem to get the Swing repaint function to work on the specific Image.
Code:
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 <- this function repaints the whole frame and not just the Image I'd like it to repaint, which is about 50x50 size. How do you repaint a specific JPEG image without creating a new class?
thanks.
this.repaint
will force the parent frame to be repainted. Call repaint
only on the control holding your image.
Example: to refresh this image loaded onto the JLabel:
ImageIcon icon = createImageIcon("images/middle.gif");
label = new JLabel("Image and Text", icon, JLabel.CENTER);
You do:
label.repaint();
not just the Image I'd like it to repaint, which is about 50x50 size
JComponent#paint立即与EDT一起使用
How are you doing the drag and drop?
The easiest way is to just add an Icon to a JLabel and then drag the label around. Everytime you invoke setLocation(...) on the label it will repaint() itself.
The Component Mover class does all the hard work for you.
仅在repaint
图像的面板上调用repaint
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.