-1

I'm just beginner at Java Programming and am using NetBeans. The code below runs and No Error is displayed but no image is seen! This image is in the "frame" package beside this two classes.

package frame;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Screen extends JPanel{

    private BufferedImage image;

    public Screen(){
        try {
            System.out.println("OK");
            image = ImageIO.read(getClass().getResourceAsStream("phantomPDF.png"));
        } catch(IOException e) {
            e.printStackTrace();
        }

    repaint();
    }

public void paint(Graphics g){
    g.drawImage(image, 100, 100, null);
    System.out.println("Yes");
    }

}

and this is my frame and main method :

package frame;

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

public class Frame extends JFrame{

    Screen s;

    public Frame() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 600);
        setResizable(false);
        setTitle("Graphics");
        setVisible(true);

        init();

    }


    public void init(){
        setLocationRelativeTo(null);

        setLayout(new GridLayout(1, 1, 0, 0));

        s = new Screen();
        add(s);


   }

    public static void main(String[] args){
        new Frame();
    }
}
Dan
  • 5,153
  • 4
  • 31
  • 42
  • Does the System.out.println() print "Yes" to the console? Just after you've loaded your image can you check whether it is null or not, this may help determine what causes the problem. Also can you try setting it to visible after you've added your Screen object – AlmasB May 10 '15 at 11:45

1 Answers1

0

I had to put "try - catch" statement into "paint" class so that works fine now ... but why should i do that?