You could charge you draw your icon yourself inside the method paintComponent.
Code:
import java.awt.Graphics ;
import java.awt.Image ;
import javax.swing.ImageIcon ;
import javax.swing.JButton ;
public class MyButton extends JButton { / / ! we must extend the component in which you want to insert a background image
private Image img ;
private String imageName ;
/ / A builder for more simply choose the image
public MyButton(String imageName) {
img = New ImageIcon(getClass().GetResource(imageName)).getImage();
}
/ / We must redefine the method paintComponent () for components Swing ! and paint ( ) to awt
@ Override
protected void paintComponent(Graphics g) {
/ / super.paintComponent ( g );
if (img == null) return;
g.drawImage(img 0, 0, getWidth(), getHeight(), this);
getIcon().paintIcon(this, g (getWidth()getIcon -().getIconWidth())/2, (getHeight()getIcon -().getIconHeight())/2);
}
}
here is the line added
Code:
getIcon().paintIcon(this, g (getWidth()getIcon -().getIconWidth())/2, (getHeight()getIcon -().getIconHeight())/2);
getIcon method (.. ) takes as parameter the component where we want to draw the graphics with which we draw , and the location of the icon ( x, y) , here I made sure that it is center.
Bookmarks