So I want to change the position of an image which I implemented as a JComponent after I click the corresponding button, however, it does not do anything when getting clicked.
I think the problem may lay inside my paintComponent method, since I want the image to be drawn in the center of my panel in the beginning.
The panel itself has a GridBagLayout, although I think that anchoring it in the center would not help either, since I want to move the image whenever the button is clicked and only want it to show up in the center of the panel when I open the frame...
For this button in particular I want my JComponent/image to move 50 pixels to the right.
Anyway here's what I did:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (this.getWidth() - img.getWidth(null))/2;
int y = (this.getHeight() - img.getHeight(null))/2;
g.drawImage(img, x, y, null);
}
@Override
public void actionPerformed(ActionEvent e) {
img.repaint(img.getX()+50, img.getY(), img.getHeight(), img.getWidth());
}
Currently, your paintComponent method always uses the same values for
xandy, so it should come as no surprise that the image doesn’t move.xandyare state that needs to be remembered. This is done by defining instance fields:You can’t initialize them at construction time, because a component has a width and height of zero when it’s created (usually). Fortunately, there is a method which tells you when a component has been given a width and height; that method is addNotify, which you can and should override:
Notice that you should pass
this(your component instance) to any method which expects an ImageObserver argument, rather than null.Now paintComponent doesn’t need to do any calculation. It just paints the image at whatever
xandyhappen to be:And now that your coordinates are instance fields, you can change them directly: