1
  1. jWindow opened for 2 seconds but image doesn't paint... any thoughts?
  2. image file is in the same folder as class file...
public class CreateSplashScreen extends JWindow {
    JWindow jw = new JWindow();
    Image scImage = Toolkit.getDefaultToolkit().getImage("testImage.png");
    ImageIcon imageIcon = new ImageIcon(scImage);
    public CreateSplashScreen() {
        try {
            jw.setSize(700, 500);
            jw.setLocationRelativeTo(null);
            jw.setVisible(true);
        } catch (Exception e) {
        }
    }

    public void paint(Graphics g) {
       super.paint(g);
       g.drawImage(scImage, 0, 0, jw);
    }

    public void CloseSplashScreen() {
        jw.setVisible(false);
    }
    
    public static void main(String[] args) {
        CreateSplashScreen sp = new CreateSplashScreen();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(CreateSplashScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
        sp.CloseSplashScreen();
    }
    
}
  1. jWindow opened for 2 seconds but image doesn't paint... any thoughts?
  2. image file is in the same folder as class file...
Hana
  • 11
  • 3

2 Answers2

0

Why are you creating an internal JWindow when your class CreateSplashScreen already extends JWindow? There is no need of it. You are messing with your program.

How? You are actually viewing the inner JWindow by jw.setVisible(true); but you are painting the image in the CreateSplashScreen's `JWindow.

Try this code :

public class CreateSplashScreen extends JWindow 
{
    ImageIcon i = new ImageIcon(getClass().getResource("/createsplashscreen/testImage.png"));
    
    public CreateSplashScreen() {
     setSize(700, 500);
     setLocationRelativeTo(null);
     setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
     super.paint(g);
     g.drawImage(i.getImage(), 0, 0, null);
    }

    public void CloseSplashScreen() {
     setVisible(false);
    }
    
    public static void main(String[] args) {
        CreateSplashScreen sp = new CreateSplashScreen();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            
        }
        sp.CloseSplashScreen();
    }    
}

Note: I do not know about your method to fetch image resource from the source folder.

Edit: Assuming that the name of the package containing your class CreateSplashScreen is createsplashscreen, make sure that the image testImage.png is present in the createsplashscreen package of your project.

Miles Morales
  • 307
  • 1
  • 15
  • Do I need method to fetch image from folder? – Hana Aug 02 '20 at 03:03
  • Had no idea about that ... maybe that's why it didn't paint? Could you give me some hint if you can? – Hana Aug 02 '20 at 03:04
  • @Hana *Do I need method to fetch image from folder?* - I said I do not know about your method. It might work. Or it might not. If it doesn't, try reading this post https://stackoverflow.com/questions/6845231/how-to-correctly-get-image-from-resources-folder-in-netbeans – Miles Morales Aug 02 '20 at 03:11
  • @Hana I have updated the code in the answer and it works. Try it. – Miles Morales Aug 02 '20 at 03:29
  • @Hana An advice : Please learn more about how to include resources in your project folder and how to fetch those resources from your package folder. This is where you can start from : https://stackoverflow.com/questions/22928116/how-to-set-image-on-jlabel-from-project-folder and https://stackoverflow.com/questions/14819454/get-image-from-local-directory-using-swing?rq=1 – Miles Morales Aug 02 '20 at 03:44
  • Thank you Peter ... tried your code but ended up with java.lang.NullPointException. path is "/name of package/imagefile" also made Resources folder under src package folder and "/Resources/image file" but didn't work either .... always end up with NullPointException error.... out of options now. Am I doing something wrong? – Hana Aug 03 '20 at 04:05
  • @Hana Can you edit the question and add a screenshot of your project structure showing the location of your image file and `CreateSplashScreen` class? Also, please add your current code, in which you are getting a `NullPointerException`. – Miles Morales Aug 03 '20 at 10:14
  • Like you said image file is inside of package folder, somehow I add a line in manifest. mf file as "SplashScreen-Image: /packagename/imagefile" and that solved problem Will add screen shot when I get home for any possible issue. Thanks Peter – Hana Aug 04 '20 at 12:26
0

Screen shot of file structure

@Peter For error code, I deleted one line that I added in mamifest.mf file and build a program... This time, didn't give me an error, weird... I was following error code when I got it and it led me to something like "CLASSPATH" section of application generated code... sorry I can't remember exactly Really appreciate Peter for your help. Wish you luck...

Hana
  • 11
  • 3