3

I'm learning java by watching tutorials, I'm a beginner. Currently, I'm trying to follow a tutorial (https://www.youtube.com/watch?v=_SqnzvJuKiA) to make the snake game. Around minute 11, the man makes a jpg display on the window. I believe I followed all of his steps but can't seem to make it work. Iv'e attached following image of my code and output

This code and my output and paste "my" code.In the public void paint line netbeans tells me to "add @override annotation", which he didn't do, and "multiple annotations here [2] click to cycle". In the output on the top white rectangle should appear the jpg. Does someone see my mistake?

package snake;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class GP extends JPanel{
private ImageIcon titleImage;
public GP() {

}

public void paint(Graphics g)
{   
   // Titulo
   g.setColor(Color.white);
   g.drawRect(24, 10, 851, 55);

   titleImage = new ImageIcon("snaketitle.jpg");
   //ImageIcon icon = new ImageIcon("androidBook.jpg");
   titleImage.paintIcon(this, g, 25, 11);

   //Area Juego
   g.setColor(Color.WHITE);
   g.drawRect(24,74,851,577);

   g.setColor(Color.black);
   g.fillRect(25,75,850,575);



}
JJIqbal
  • 630
  • 1
  • 8
  • 23
  • I don't see a mistake here. Did you already check the snaketitle.jpg file you are using? Is this file loaded correclty? Is the path correct? – Chris623 Mar 14 '19 at 06:06
  • `@Override` is optional but a best practice as it gives your compiler (and your IDE) some hints about your intentions and prevents other issues from occurring. You can take a look at SO for similar questions to confirm that your path is correct. For example, https://stackoverflow.com/questions/16631636/what-is-the-correct-path-to-display-an-imageicon-png-file-for-windows-7 – riddle_me_this Dec 26 '19 at 02:57

1 Answers1

0

First of all i think your Question is understandable, what you are facing is that the program is not able to locate your image "androidBook.jpg". By the way, The ImageIcon class has several Constructors Try using

ImageIcon(URL location)
//Creates an ImageIcon from the specified URL.

and paste the URL pointing to the location of "androidBook.jpg". Also you can follow the below link for more insight... https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html