0

I am actually want to load image but only applet dialog open and no error occurred but the image is not loading.the code is below here

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Ball extends JPanel{
  Image image; 


  public Ball(){
    super();       
    image = Toolkit.getDefaultToolkit().getImage("/D:\\lolololo\\tuto\\bin\\sa.jpg");
  }

private Image getResource(String string) {
    return image;
    // TODO Auto-generated method stub

}

  public void paintComponent(Graphics g){


   // Draw our Image object.
   g.drawImage(image,50,10,574,960, this); // at location 50,10
     // 200 wide and high
  }

  public void main(String arg[]){
   JFrame frame = new JFrame("ShowImage");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(800,500);

   Ball panel = new Ball();
   frame.setContentPane(panel);
   frame.setVisible(true);
  }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Syed Sabhi Xaidi
  • 11
  • 1
  • 2
  • 7
  • 3
    `"/D:\\lolololo\\tuto\\bin\\sa.jpg"` should not contain the leading `/`. It should be `"D:\\lolololo\\tuto\\bin\\sa.jpg"` presuming the rest of that path is correct. As an aside `public void main(String arg[]){` is **not** a valid method signature for the main method! – Andrew Thompson Jun 28 '13 at 17:03
  • 4
    Given `Toolkit.getImage(String)` does not throw any errors when the image is not found, I prefer to use `ImageIO` to load them. It provides lots of helpful output when anything goes wrong. – Andrew Thompson Jun 28 '13 at 17:08

2 Answers2

1

The way you are loading the image is wrong. This will never work when you extract as Runnable jar.

  • Create package ("res") inside inside your src

Now load the image this way

image = ImageIO.read(Ball.class.getResource("/res/sa.jpg"));

This will work.

As indicated by Andrew in his comment main class should be

public static void main(String arg[]) {}
Makky
  • 17,117
  • 17
  • 63
  • 86
1

+1 to @AndrewThompsons comments.

1) Below is incorrect, you do not honer the paint chain by calling the supers implementation of super.paintComponent(...):

public void paintComponent(Graphics g) {
   // Draw our Image object.
   g.drawImage(image,50,10,574,960, this); // at location 50,10
   // 200 wide and high

}

As per docs for paintComponent:

Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

should be:

public class Ball extends JPanel {

    BufferedImage image;

    public Ball() {
       super();

       try {
           image=ImageIO.read(new File("c:/test.jpg"));//change to your path of file 
       }catch(Exception ex) {//file did not load properly
           ex.printStackTrace();
       }
    }

    @Override
    protected void paintComponent(Graphics g){
         super.paintComponent(g);

         // Draw our Image object.
         g.drawImage(image,0,0,image.getWidth(),image.getHeight(), this); // at location 50,10
        // 200 wide and high
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(),image.getHeight());//replace with size of image
    }

}

Also notice:

  • I overrode getPreferredSize() of JPanel and returned Dimensions I wanted (i.e Image size) or the JPanel will only be as big as the components added to it and not the image (so if no components 0,0).

  • I also chose BufferedImage vs Image and surrounded the statement with a try catch to check if any errors are thrown.

  • I see you also had g.drawImage(image,50,10...) why 50 and 10 and not 0,0?

2) Also this (as @AndrewThompson has said):

image = Toolkit.getDefaultToolkit().getImage("/D:\\lolololo\\tuto\\bin\\sa.jpg");

No need for the / thats only if its located in your classes package etc not when its on your HDD at a set location.

3) also as said by @AndrewThompson a main method should be:

public static void main(String[] args){}

Notice the static modifer applied other wise its just another method.

4) Also dont use JFrame#setSize(..), rather use LayoutManager and/or override getPreferredSize and than simply call pack() on JFrame instance before setting it visible.

5) Also please have a read on Concurrency in Swing. especially the Event-Dispatch-Thread

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Also see [this](http://stackoverflow.com/questions/14632998/null-exception-while-drawing-image-to-jframe/14633038#14633038) answer which shows many examples of drawing images – David Kroukamp Jun 28 '13 at 18:22