Splash Screen loading with application

10

My application, when starting, does the first search in the database. Since I use Hibernate , this first connection is a bit more time consuming because it assembles all the database mapping. I'm thinking of adding a Splash Screen at the beginning, so the user does not think that the application has crashed or is not loading, but from the examples I've seen, I have to give an amount of time to Thread load. However, the system load time varies depending on the microcontroller settings, whether the system has already been opened in the machine or is still in memory.

My question is whether I can make Splash's lifespan take a long time to load my system.

Below is an example of how I start my system and how it generates my Splash.

Splash.java

....

/**
*
*   @author desenvolvimento
*/
public class Splash extends JWindow {

AbsoluteLayout absoluto;
AbsoluteConstraints absimagem, absbarra;
ImageIcon image;
JLabel jLabel;
JProgressBar barra;

public Splash() {

    absoluto = new AbsoluteLayout();
    absimagem = new AbsoluteConstraints(0, 0);
    absbarra = new AbsoluteConstraints(0, 284);
    jLabel = new JLabel();
    image = new ImageIcon(this.getClass().getResource("/imagem/Logo.png"));
    jLabel.setIcon(image);
    barra = new JProgressBar();
    barra.setPreferredSize(new Dimension(285, 10));
    this.getContentPane().setLayout(absoluto);
    this.getContentPane().add(jLabel, absimagem);
    this.getContentPane().add(barra, absbarra);
    this.getContentPane().setBackground(Color.white);

    new Thread() {

        public void run() {

            int i = 0;
            while (i < 101) {
                barra.setValue(i);
                i++;
                try {

                    sleep(150);

                } catch (InterruptedException ex) {
                    Logger.getLogger(Splash.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            TelaLogin x = new TelaLogin();
            x.setVisible(true);

        }

    }.start();
    this.pack();
    this.setVisible(true);
    this.setLocationRelativeTo(null);

}

}

Principal.java

public class Agil {


public static void main(String[] args) {
  TelaLogin telaLogin = new TelaLogin();
    telaLogin.validate();
    telaLogin.pack();
    telaLogin.setVisible(false);
   }
}

TelaLogin.java

public class TelaLogin extends javax.swing.JFrame {

/**
 * Creates new form TelaLogin
 */
public TelaLogin() {
    initComponents();

   new Splash();

    EmitenteDAO emitentedao = new EmitenteDAO();
    String nomeFantasia = emitentedao.getEmitente();
    LbEmpresaLogin.setText(nomeFantasia);

    LeituraXmlConfig config = new LeituraXmlConfig();

    if (config.getValidaConfig().equals("0")) {

        //Inicia configuração
    } else if (config.getValidaConfig().equals("1")) {

    }


    URL url = this.getClass().getResource("imagem/icon_32.png");
    Image iconeTitulo = Toolkit.getDefaultToolkit().getImage(url);    
    this.setIconImage(iconeTitulo);

    TxUsuarioLogin.setDocument(new EntradaUpperCase());

}

Session session = Session.getSession();

@SuppressWarnings("unchecked")

 ........
    
asked by anonymous 24.07.2015 / 14:22

3 answers

2

An alternative would be to use the class SplashScreen , with it java itself will display and close the splash as soon as a awt/swing window is opened.

As an example follows the code I got from the Netbeans tutorial and adapted it:

public class CustomSplashScreen {

    private static SplashScreen mySplash;
    private static Graphics2D splashGraphics;
    private static Rectangle2D.Double splashProgressArea;

    public static void splashInit() {

        mySplash = SplashScreen.getSplashScreen();

        if (mySplash != null) {
            //pega as dimensoes da imagem que será usada no splashscreen
            // para que a barra fique proporcional
            Dimension ssDim = mySplash.getSize();
            int height = ssDim.height;
            int width = ssDim.width;
            //desenha a área da barra de progresso, você pode alterar as dimensoes pra testar a que mais gostar
            //  os parametros representam posição horizontal  e vertical (em relação a imagem), altura e largura, respectivamente
            splashProgressArea = new Rectangle2D.Double(1.0, height * 0.87, width, height * 0.08);
            //exibe a imagem do splash centralizada na tela
            splashGraphics = mySplash.createGraphics();
            //inicia a barra de progresso(pode ser removido)
            splashProgress(0);
        }
    }

    public static void splashProgress(int pct) {
        if (mySplash != null && mySplash.isVisible()) {

            //preenche a area da barra de progresso com a cor informada
            splashGraphics.setPaint(Color.LIGHT_GRAY);
            splashGraphics.fill(splashProgressArea);

            //colore bordas na barra de progresso(opcional)
            splashGraphics.setPaint(Color.BLUE);
            splashGraphics.draw(splashProgressArea);

            //pega o menor valor das coordenadas(horizontal  X e vertical Y) da barra 
            //será usado para o carregamento(não alterar daqui em diante)
            int x = (int) splashProgressArea.getMinX();
            int y = (int) splashProgressArea.getMinY();
            int wid = (int) splashProgressArea.getWidth();
            int hgt = (int) splashProgressArea.getHeight();

            //valor usado para o carregamento da barra
            int doneWidth = Math.round(pct * wid / 100.f);
            doneWidth = Math.max(0, Math.min(doneWidth, wid - 1));

            //aqui  é que vai preenchendo o carregamento da barra de acordo com o valor
            //passado em pct    
            splashGraphics.setPaint(Color.GREEN);
            splashGraphics.fillRect(x, y + 1, doneWidth, hgt - 1);
            mySplash.update();
        }
    }
}

But before using the class, it is important to add an image in your project and the reference in manifest.mf . If you are using Netbeans , the file looks something like this:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Change this way:

Manifest-Version: 1.0
SplashScreen-Image: <caminho da imagem>
X-COMMENT: Main-Class will be added automatically by build

where caminho da imagem must be the path where the image is in your project. As an example, see my test project:

Soon,theimageaddresswilllooklikethis:

Manifest-Version:1.0SplashScreen-Image:splashdemo/splash.pngX-COMMENT:Main-Classwillbeaddedautomaticallybybuild

Then,tocall,justdosoonyourmain

publicstaticvoidmain(Stringargs[]){CustomSplashScreen.splashInit();for(inti=0;i<10;i++){CustomSplashScreen.splashProgress(i*20);try{Thread.sleep(500);}catch(InterruptedExceptionex){ex.printStackTrace();}}}

forwillincrementavaluethatwillbepassedtoprogressreference,and(i*20)isjustacustomizationofhowthebarwillincrementinprogress,youcancustomizeithereanywayyoulike.

Note:YoucanalsotestvianetbeansitselfbygoingtoPropertiesandaddingtheline-splash:src/splashdemo/splash.pngtoVMOptions:

Soyoudonotneedtobegeneratingthejarallthetimetotest.

Therearemoreexamplesat link

Limitations

As reported here , progress is not synchronized with Thread loading, but the splash is closed like this that a awt/swing window becomes visible.

    
09.03.2016 / 12:10
0

You should start the Thread Splash, which will appear on your screen immediately at the beginning of the application, and leave the second Thread loading the database stops, at the end of loading Hibernate, before it calls the application screen, you kill the first thread and here comes the application screen. So it will be "calibrated" for any type of hardware.

    
02.03.2016 / 02:49
0

The Splash Screen was mounted as follows:

The main class calls a JFrame that has an image that stays visually in the center of the screen until the application does the first mapping of the database and mount the communication the screen is visible.

principal.java

SplashPrincipal sc = new SplashPrincipal();
    sc.setVisible(true);
    sc.setSize(535, 235);


    TelaLogin telaLogin = new TelaLogin();
    telaLogin.validate();
    telaLogin.pack();
    telaLogin.setVisible(true);

    sc.setVisible(false);
    
05.03.2016 / 15:20