Reputation: 91
Ive recently attempted to add an image to the Jrame, and for some reason i got an error. i followed a video, and an article, but still not working. Eclipse doesnt show any errors, and the program runs, but there is an error in the console, relating to the imageIcon line. Heres the code:
public class Image extends JFrame{
private ImageIcon image;
public Image(){
super("Image Display");
setLayout(new FlowLayout());
setMinimumSize(new Dimension(500,500));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
image = new ImageIcon(this.getClass().getResource("button.png"));
pack();
}
public void paint(Graphics g1){
Graphics g = (Graphics)g1;
//g.setColor(Color.yellow);
//g.fillRect(0, 0, 500, 500);
image.paintIcon(this, g, 0, 0);
}
}
Upvotes: 0
Views: 41
Reputation: 324197
i followed a video, and an article,
There is no need to do custom painting to display an image and the video and articles are not showing the proper way to do this. You should NOT be overriding the paint() method of a JFrame.
Start with the Swing tutorial for Swing basics. It contains example code you can download and execute.
The first section to read would probably be the section on How to Use Icons
for an example that shows you how to use existing components do to this.
If you do want to learn more about custom painting, then you can check out the section on Performing Custom Painting
.
Upvotes: 1