-3

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.

AME
  • 302
  • 2
  • 17
  • Put the label into the GUI on GUI creation, not on button push. On button push, simply create the ImageIcon and set the label's icon. That's it. – DontKnowMuchBut Getting Better Apr 21 '19 at 12:33
  • I want to generate uknown amount of image, so I can't create the labels at the start. I want to generate them at random position with random images. – AME Apr 21 '19 at 12:39
  • 2
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with – Andrew Thompson Apr 21 '19 at 12:42
  • .. more width and height - to show how the extra space should be used. – Andrew Thompson Apr 21 '19 at 12:42
  • 2
    Never use the `getFile()` method of URL. It *does not* return a valid file name; it is a hold-over from the days of Java 1.0, back when most URLs happened to have path portions that referred to physical files, either on the local machine or on a remote machine. There is an `ImageIO.read` method which accepts a URL, so pass the value returned from getResource directly to ImageIO.read. – VGR Apr 21 '19 at 15:46
  • `getClass().getClassLoader().getResource("noimg.png")` Strange as it might seem, most people willing to help will not have a `noimg.png` available on the right path for that to work. **That** is why I suggested a **hot-link** to images on the net that are accessible to all. Note also that an ideal MCVE / SSCCE should be single copy/paste. To do that with multiple classes, demote all but the one with `main` method to default access (i.e. not `public`) and paste them into the same source code file as the `main`. Please also include `import` statements. – Andrew Thompson Apr 22 '19 at 02:35

1 Answers1

0

I just had to setSize the label.

Here is the fixed code:

public void drawImage(BufferedImage image)
    {
        JLabel label = new JLabel(new ImageIcon(image));
        label.setLocation(1, 1);
        label.setSize(50, 130);
        this.jPanel1.add(label);
        this.jPanel1.validate();
        this.jPanel1.repaint();
}
AME
  • 302
  • 2
  • 17