Saturday 7 January 2017

SWT: Design Splash screen

A splash screen is a graphical control element consisting of window containing an image, a logo and the current version of the software. A splash screen usually appears while a game or program is launching.

How to create splash screen in SWT?
Create a Shell instance with style 'SWT.NO_TRIM'.

Shell shell = new Shell(display, SWT.NO_TRIM);

Following is the complete working application.
package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static String imagePath = "C:\\Users\\Downloads\\icons\\car.png";

 private static void addGraphicsToShell(Display display, Shell shell) {
  Image img = new Image(display, imagePath);
  ImageData imdata = img.getImageData();
  shell.setSize(imdata.width, imdata.height);

  Rectangle r = display.getBounds();
  int shellX = (r.width - imdata.width) / 2;
  int shellY = (r.height - imdata.height) / 2;
  shell.setLocation(shellX, shellY);

  shell.open();
  GC gc = new GC(shell);
  gc.drawImage(img, 0, 0);

  System.out.println("Sleeping for 5 seconds");
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  img.dispose();
  gc.dispose();
  shell.dispose();
  display.dispose();

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display, SWT.NO_TRIM);
  addGraphicsToShell(display, shell);

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }

  /* Dispose the display */
  display.dispose();

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment