0

The variable was made, the image is loaded into the variable and the paintComponent is asked to display the image in my JPanel. I can't see my mistake.

public class Main extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new Main();
        frame.setSize(1524, 715);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Animatieproject Stijn Mannaerts: Free Kick");
        frame.setContentPane(new Voetbalveld());
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }
}

class Voetbalveld extends JPanel {

    private ImageIcon afbIntro;

    public Voetbalveld() {

        afbIntro = new ImageIcon("scr/afbeeldingen/Intro.jpg");

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        afbIntro.paintIcon(null, g, 0, 0);

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3145921
  • 139
  • 1
  • 1
  • 5
  • 4
    Look through today's questions on embedded resources, it has been covered several times! – Andrew Thompson Jan 02 '14 at 13:54
  • Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), especially the last link. Hope it might be able to help you somewhat :-) – nIcE cOw Jan 02 '14 at 15:02

4 Answers4

0

That isn't how painting is done. Why not use Graphics.drawImage() instead? Check out this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/painting/

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

Your code seems right to me. Make sure you don't want src instead of scr. Next, if your image folder is inside source folder of your project then you might want to try this:

afbIntro = new ImageIcon(Voetbalveld.class.getResourceAsStream("scr/afbeeldingen/Intro.jpg"));

or

afbIntro = new ImageIcon(Voetbalveld.class.getResourceAsStream("afbeeldingen/Intro.jpg"));

depending on your file structure. The path in the second example needs to be relative to the file Main.java.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

The frame class

    import javax.swing.JFrame;

    public class Main
    {
        public static void main(String[] args)
        {
        Jframe frame = new JFrame("Animatieproject Stijn Mannaerts: Free Kick");
        frame.setSize(1524, 715);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Voetbalveld());
        frame.setVisible(true);
        }
    }

The panel class

    import javax.swing.*;
    import java.awt.*;

    public class Voetbalveld extends JPanel
    {
        public void paintComponent(Graphics g)
        {    
        ImageIcon afbIntro = new ImageIcon("scr/afbeeldingen/Intro.jpg");
        /*The following are two methods for image sizing,
         *Use the one that best fits your code:
         *
         *g.drawImage(afbIntro.getImage(), x, y, null); 
         *Fill in the arguments for x and y to locate your upper left corner
         *The image will be in it's original size with the designated upper left corner
         *
         *g.drawImage(afbIntro.getImage(), x, y, w, h, null);
         *Fill in the arguments for w and h to set the width and height of your image
         *The image will be in it's scaled size (w and h) and starting at the 
         *designated upper left corner (x and y)
         */
        }
    }
JavaDude
  • 75
  • 1
  • 3
  • 8
0

You spelled src wrong. You have scr.

"scr/afbeeldingen/Intro.jpg"

Fix that

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720