0
import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Image;

import javax.sound.midi.Patch;

import javax.swing.*;

this is were the code start

public class graficCar extends JComponent
{
    private ImageIcon image1=null;

private JLabel label1=null;

private ImageIcon image2=null;

private JLabel label2=null;

 graficCar(){

setLayout(new FlowLayout());

the problem here

image1=new ImageIcon(getClass().getResource("4596067.png"));

   label1=new JLabel(image1);
add(label1);
}

the main

public static void main(String[] args)
{
    graficCar g=new graficCar();
    g.setDebugGraphicsOptions(JFrame.EXIT_ON_CLOSE);
    g.setVisible(true);

}
fadden
  • 51,356
  • 5
  • 116
  • 166

1 Answers1

0

As you mentioned, your png file is in C:\Users\user\Downloads folder. I bet class is somewhere else. The problem is getResource() finds file in classpath by default. Here you could find details if you want to go deeper.

One solution is to put .png right next to your .java class but it's bad practice. More convenient way described here. The best practice is to create special folder for your resources in the project and then use relative path to it. How to do this is out of scope although it's described in many sites and in Stackoverflow too. Don't hesistate to use Search :)

ADS
  • 708
  • 3
  • 14