Code Monkey home page Code Monkey logo

Comments (4)

code-disaster avatar code-disaster commented on July 25, 2024

It appears that the -splash VM argument is processed and filtered by the java executable, and not forwarded to / ignored by libjvm. WinRun4J for example implements its own solution, which isn't cross-platform.

from packr.

maheshkurmi avatar maheshkurmi commented on July 25, 2024

Thanks for rply.. that means it is not possible to use splashscreen with packr.

from packr.

code-disaster avatar code-disaster commented on July 25, 2024

No. Not per VM option. And I believe it would be a major pain to add custom support, like WinRun4J does, which works well on all platforms.

I don't know your requirements. If you really need a splash screen of some kind, it may be feasible to do one in Java instead. Loading and starting the JVM doesn't need much time on desktop systems.

from packr.

maheshkurmi avatar maheshkurmi commented on July 25, 2024

Once again thanks for your suggestion, I implemented splash screen in java itself. Actually my application takes a while in starting up, thats why splash screen was badly needed. This is what I did..

package org.shikhar.simphy;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.Toolkit;

import javax.swing.JFrame;

/**
 * Derived from
 * https://www.randelshofer.ch/oop/javasplash/javasplash.html#secondsplash
 * 
 * @author Mahesh kurmi
 *
 */
public class SplashWindow extends JFrame {
    private static SplashWindow instance;
    private Image image;
    private boolean paintCalled = false;
    private String msg = "";

    /**
     * The main method does three things: 
     * 1. Open the splash window 
     * 2. Invoke the main method of class Sandbox (Main app class)
     * 3. Close the splash window when the main method of Sandbox returns
     * @param args
     */
    public static void main(String[] args) {
        SplashWindow.splash();
        SplashWindow.invokeMain("org.shikhar.simphy.Sandbox", args);
        SplashWindow.disposeSplash();
    }

    public static void splash() {
        /*
         * In the first splash() method we use the AWT Toolkit class to create
         * the image. We use this method instead of the new Image IO API,
         * because it does not immediately load the image using the current
         * thread.
         */
        splash(Toolkit.getDefaultToolkit().createImage(
                SplashWindow.class
                        .getResource("/org/shikhar/simphy/images/splash.png")));
        // ImageUtilities.getIconFromClassPathSuppressExceptions("/org/shikhar/simphy/images/splash.png").getImage());
    }

    private SplashWindow(Image image) {
        super();
        this.image = image;

        MediaTracker mt = new MediaTracker(this);
        mt.addImage(image, 0);
        try {
            mt.waitForID(0);
        } catch (InterruptedException ie) {
        }
    }

    /**
     * Here we invoke the main() method of our application using, er, clumsy Reflection code.
     * @param className
     * @param args
     */
    private static void invokeMain(String className, String[] args) {
        try {
            Class.forName(className)
                    .getMethod("main", new Class[] { String[].class })
                    .invoke(null, new Object[] { args });
        } catch (Exception e) {
            InternalError error =

            new InternalError("Failed to invoke main method");
            error.initCause(e);
            throw error;
        }
    }

    private static void splash(Image image) {
        /*
         * an instance of the SplashWindow is created then, the instance is shown. And then,
         * we wait in a synchronized block until the paint method of the
         * instance has been called.
         */
        if (instance == null && image != null) {
            instance = new SplashWindow(image);
            instance.setSize(816, 251);
            instance.setUndecorated(true);
            instance.setLocationRelativeTo(null);
            instance.setVisible(true);
            if (!EventQueue.isDispatchThread()
                    && Runtime.getRuntime().availableProcessors() == 1) {

                synchronized (instance) {
                    while (!instance.paintCalled) {
                        try {

                            instance.wait();

                        } catch (InterruptedException e) {
                        }
                    }
                }
            }
        }
    }

    @Override
    public void update(Graphics g) {
        paint(g);
    }

    @Override
    public void paint(Graphics g) {
        if (image == null) {
            System.out.println("image is null");
            return;
        }

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

        Graphics2D g2D = (Graphics2D) g;
        g2D.drawImage(image, 0, 0, this);
        g2D.setColor(new Color(239, 127, 34));
        g2D.fillRect(543, 34, 800, 30);
        g2D.setColor(new Color(255, 255, 255));
        g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        g2D.drawString(msg + "...", 543, 52);

        if (!paintCalled) {
            paintCalled = true;
            synchronized (this) {
                notifyAll();
            }
        }
    }

     /**Called from Sandbox class to set progress */
    public static void setProgress(String msg, int percentage) {
        if (instance == null)
            return;
        instance.msg = msg;
        instance.repaint();
    }

    /**
     * free resources if any
     */
    public static void disposeSplash() {
        if (instance == null)
            return;
        instance.setVisible(false);
        instance.image.flush();
        instance.image = null;
        instance.dispose();
        instance = null;
    }

}

For now it does the job, but I am not much comfortable with JVM loading mechanism so there may be few problems with the implementation. Kindly reply if there is any improvement needed.

from packr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.