I recently read this thread (Creating a custom button in Java) on creating custom buttons in java by extending the JButton class, however all the solutions on this thread use graphics drawn in java.
I wanted to have my button based on a button image I had drawn in photoshop. So I tried to apply what I read in that thread with this result:
import javax.swing.*;
import java.awt.*;
public class nextButton extends JButton {
@Override
protected void paintComponent(Graphics g) {
Image image = new ImageIcon("nextButton.png").getImage();
g.drawImage(image,0,0,this);
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.setSize(75, 150);
return size;
}
}
When I run the main program having added this button to a JPanel it doesn't display. I am assuming it could be one of several reasons:
a) The size of the JButton doesn't match the image? b) I haven't loaded the image properly. In the notes my lecturer gave me he writes out the display image code with just "imageName.png" with no file path so I have no idea if this is the correct way to do it, or how the program will know to load the image. c) Something else which is beyond my knowledge so far.
If anyone knows how to solve this I'd be very grateful.
Thanks so much!