-1

Recently i decided to start learning how to make 2D games With JAVA ( eclipse ) so i found a tutorial online that shows how to make superMari game with java, i wrote the same code he wrote and i followed step by step what he did, which wasn't a big thing to talk about, unfortunately he's code shows, after excuting, a window with two images in it while mine shows just the window with no images, i ensure you that i imported the two images and put them in one package to avoid all kind of problems but it still shows nothing.

my code has two classes, "main" and "Scene", here it is, hopefully someone will find a solution for me, thank you guys!

Main.java :

    package AiMEUR.AMiN.jeu;

import javax.swing.JFrame;

public class Main {

    public static Scene scene;

    public static void main(String[] args) {

        JFrame fenetre = new JFrame("Naruto in mario World!!");
        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fenetre.setSize(700, 360);
        fenetre.setLocationRelativeTo(null);
        fenetre.setResizable(false);
        fenetre.setAlwaysOnTop(true);

        scene = new Scene();


        fenetre.setContentPane(scene);
        fenetre.setVisible(true);

    }

}

Scene.java :

 package AiMEUR.AMiN.jeu;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Scene extends JPanel{

    private ImageIcon icoFond;
    private Image imgFond1;

    private ImageIcon icoMario;
    private Image imgMario;

    private int xFond1;

    public Scene(){

        super();

        this.xFond1 = -50;

        icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
        this.imgFond1 = this.icoFond.getImage();
        icoMario =  new ImageIcon(getClass().getResource("/Images/1.png"));
        this.imgMario = this.icoMario.getImage();
    //  paintComponent(this.getGraphics());
    }

    public void paintCompenent(Graphics g){

        super.paintComponent(g);
        Graphics g2 = (Graphics2D)g;

        g2.drawImage(this.imgFond1, this.xFond1, 0, null);
        g2.drawImage(imgMario, 300, 245, null);
    }

}
  • I would check that `icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));` and the other similar line are actually loading your images. – Daniel A. Thompson Jun 30 '16 at 14:50
  • sorry! but i didn't get what you mean! you think i should change that line of code you refered to? –  Jun 30 '16 at 14:53
  • No, that's the line you should check. Try this afterwards: `int status = icoFond.getImageLoadStatus()` - this ought to be one of `MediaTracker.ABORTED`, `MediaTracker.ERRORED`, or `MediaTracker.COMPLETE`. See the API at https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html – Daniel A. Thompson Jun 30 '16 at 14:55
  • It would help to see your directory structure. For instance, you probably have the directory `src/AiMEUR/AMiN/jeu`. Do you have the images in `src/Images` or just `Images` in the project? – AJNeufeld Jun 30 '16 at 14:57
  • I have hem just just in Images :/ is that a mistake? –  Jun 30 '16 at 14:59
  • Oh one sec! I have them in src/Images !! Sorry my bad. –  Jun 30 '16 at 15:00

2 Answers2

1

You have not named the paintComponent method correctly, and therefore it is not being overridden.

The correct name is paintComponent not paintCompenent:

public class Example extends JPanel {

    @Override
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
    }
}
explv
  • 2,709
  • 10
  • 17
-1

You can determine the loading status of an ImageIcon by doing something like this:

 public Scene(){

    super();

    this.xFond1 = -50;

    icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));

    int status = icoFond.getImageLoadStatus();
    switch (status) {
       case (MediaTracker.COMPLETE): {
          System.out.println("icoFond image has successfully loaded");
       }
       case (MediaTracker.ERRORED): {
          System.out.println("The icoFond image didn't load successfully");
          // probably because the image isn't actually at "/Images/fond.gif"
       }
    }


    this.imgFond1 = this.icoFond.getImage();
    icoMario =  new ImageIcon(getClass().getResource("/Images/1.png"));
    this.imgMario = this.icoMario.getImage();
//  paintComponent(this.getGraphics());
}
Daniel A. Thompson
  • 1,904
  • 1
  • 17
  • 26