I am new to coding, and am having troubles displaying a JPEG using the paintComponent(); method to a JFrame in java. My code looks like this:
import java.awt.*;
import javax.swing.*;
class SimpleGuiMe {
MyDrawPanel imageex;
public static void main (String [] args){
SimpleGuiMe gui = new SimpleGuiMe();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
imageex = new MyDrawPanel();
frame.getContentPane().add(imageex);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
Image image = new ImageIcon("WOW(1).jpg").getImage();
g.drawImage(image,3,4,this);
}
}
}
I'm not sure if I need to save the JPEG file that I want to display in a certain directory in order to use it.
Also I'm using a somewhat outdated textbook to get the code shown here. It is post Java 5.0, but was only published in 2005.
Any help will be greatly appreciated!