Doug Hauf
Doug Hauf

Reputation: 3213

Why is my splash screen not showing up?

I want to display a splash screen for ten seconds with the image that is listed below. I have never used a splashScreen before so I am not certain as to how this works.

Question: How do I make the SplashScreen show a .jpg?

code:

import java.awt.*;
import java.awt.event.*;

public class SplashDemo extends JFrame {
    private static final long serialVersionUID = 1L;

    public SplashDemo() {
        super("SplashScreen demo");
        setSize(300, 200);
        setLayout(new BorderLayout());

        final SplashScreen splash = SplashScreen.getSplashScreen();

        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }

        Graphics2D g = splash.createGraphics();

        if (g == null) {
            System.out.println("g is null");
            return;
        }

        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(90);
            }
            catch(InterruptedException e) {
            }
        }

        splash.close();
        setVisible(true);
    }

    public static void main (String args[]) {
        SplashDemo test = new SplashDemo();
    }
}

enter image description here

New code Here:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SplashDemo extends Frame implements ActionListener {
    private static final long serialVersionUID = 1L;

    static void renderSplashFrame(Graphics2D g, int frame) {
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,40);
        g.setPaintMode();
        g.setColor(Color.BLACK);
    }

    public SplashDemo() throws IOException {
        super("SplashScreen demo");
        setSize(300, 200);
        setLayout(new BorderLayout());
        final SplashScreen splash = SplashScreen.getSplashScreen();

        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }

        Graphics2D g = splash.createGraphics();
        BufferedImage img = ImageIO.read(new File("splash.gif"));
        g.drawImage(img, 0, 0, null);

        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(90);
            }
            catch(InterruptedException e) {
            }
        }

        splash.close();
        setVisible(true);
        toFront();
    }

    public void actionPerformed(ActionEvent ae) {
        System.exit(0);
    }
}

Here is my new code. The code runs and displays to the console but it is not displaying anything to the screen. Is there something that I am missing in the code. I want the splash screen to show for a set amount of seconds.

New Code: This is right off of the Oracle site (It is still not showing anything)

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SplashDemoTest extends Frame implements ActionListener {
    static void renderSplashFrame(Graphics2D g, int frame) {
        final String[] comps = {"foo", "bar", "baz"};
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,40);
        g.setPaintMode();
        g.setColor(Color.BLACK);
        g.drawString("Loading "+comps[(frame/5)%3]+"...", 120, 150);
    }
    public SplashDemoTest() {
        super("SplashScreen demo");
        setSize(300, 200);
        setLayout(new BorderLayout());
        Menu m1 = new Menu("File");
        MenuItem mi1 = new MenuItem("Exit");
        m1.add(mi1);
        mi1.addActionListener(this);
        this.addWindowListener(closeWindow);

        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        mb.add(m1);
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }
        Graphics2D g = splash.createGraphics();
        BufferedImage img;
        try {
            img = ImageIO.read(new File("splash.gif"));
            g.drawImage(img, 0, 0, null);

        } catch (IOException e1) {
            e1.printStackTrace();
        }

        if (g == null) {
            System.out.println("g is null");
            return;
        }
        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(90);
            }
            catch(InterruptedException e) {
            }
        }
        splash.close();
        setVisible(true);
        toFront();
    }
    public void actionPerformed(ActionEvent ae) {
        System.exit(0);
    }

    private static WindowListener closeWindow = new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            e.getWindow().dispose();
        }
    };

    public static void main (String args[]) {
        SplashDemoTest test = new SplashDemoTest();
    }
}

Upvotes: 0

Views: 673

Answers (1)

demongolem
demongolem

Reputation: 9708

With g.drawImage() I imagine is what it should be to show the jpg file.

So after splash.createGraphics(), you can use that method to draw your image.

Upvotes: 1

Related Questions