I want to generate images on my JPanel with MVC. I do not have too much experience with Java, so I have got some problem with it.
I checked through some question about it, and most like I see that people are suggest JLabel ImageIcon. So I tried to set it up, but doesn't work.
View:
public class View extends javax.swing.JFrame {
public View() {
initComponents();
}
public void addActionListener(ActionListener listener)
{
this.jButton1.addActionListener(listener);
}
public void drawImage(BufferedImage image)
{
JLabel label = new JLabel(new ImageIcon(image));
this.jPanel1.add(label);
this.add(jPanel1);
this.pack();
}
Controller:
public class Controller {
View view = new View();
public Controller(View view)
{
this.view = view;
view.addActionListener(new ButtonListener());
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(getClass().getClassLoader().getResource("noimg.png").getFile()));
} catch (IOException ex) {
ex.printStackTrace();
}
view.drawImage(image);
}
}
}
Main:
public class Main {
public static void main(String[] args) {
View view = new View();
Controller controller = new Controller(view);
view.setVisible(true);
}
When I press the button, nothing happens. I want to see that image appear on my JPanel, when I press the button.