User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,631 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,249 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Apr 30th, 2007
Views: 3,771
Theres no doubting that using Java Swing is time consuming on load times. So heres a demo splash screen to keep your users captivated.

You will need two classes for this example

i.e. Splash.java and SplashWindow.java

(remember line: 8 change this to match your splash image)

just change the class name to match your entry class and bobs your uncle.


Hope this has helped.... remotely
java Syntax
  1. import java.awt.Frame;
  2. import java.awt.Toolkit;
  3. import java.net.URL;
  4.  
  5. public class Splash {
  6. public static void main(String[] args) {
  7. Frame splashFrame = null;
  8. URL imageURL = Splash.class.getResource("img.png");
  9. if (imageURL != null) {
  10. splashFrame = SplashWindow.splash(
  11. Toolkit.getDefaultToolkit().createImage(imageURL) );
  12. } else {
  13. System.err.println("Splash image not found");
  14. }
  15. try { // Change the class name to match your entry class
  16. Class.forName("MainApp").getMethod("main", new Class[]
  17. {String[].class}).invoke(null, new Object[] {args});
  18. } catch (Throwable e) {
  19. e.printStackTrace();
  20. System.err.flush();
  21. System.exit(10);
  22. }
  23. if (splashFrame != null) {
  24. splashFrame.dispose();
  25. }
  26. }
  27. }
  28.  
  29.  
  30.  
  31. /*END OF CLASS*/
  32.  
  33.  
  34.  
  35.  
  36. import java.awt.Dimension;
  37. import java.awt.EventQueue;
  38. import java.awt.Frame;
  39. import java.awt.Graphics;
  40. import java.awt.Image;
  41. import java.awt.MediaTracker;
  42. import java.awt.Toolkit;
  43. import java.awt.Window;
  44.  
  45. public class SplashWindow extends Window {
  46. /**
  47. *
  48. */
  49. private static final long serialVersionUID = 1L;
  50. private Image splashImage;
  51. private boolean paintCalled = false;
  52. public SplashWindow(Frame owner, Image splashImage) {
  53. super(owner);
  54. this.splashImage = splashImage;
  55. MediaTracker mt = new MediaTracker(this);
  56. mt.addImage(splashImage,0);
  57. try {
  58. mt.waitForID(0);
  59. } catch(InterruptedException ie) {}
  60. int imgWidth = splashImage.getWidth(this);
  61. int imgHeight = splashImage.getHeight(this);
  62. setSize(imgWidth, imgHeight);
  63. Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
  64. setLocation( (screenDim.width - imgWidth) / 2,
  65. (screenDim.height - imgHeight) / 2 );
  66. }
  67.  
  68. @Override
  69. public void update(Graphics g) {
  70. g.setColor(getForeground());
  71. paint(g);
  72. }
  73.  
  74. @Override
  75. public void paint(Graphics g) {
  76. g.drawImage(splashImage, 0, 0, this);
  77. if (! paintCalled) {
  78. paintCalled = true;
  79. synchronized (this) { notifyAll(); }
  80. }
  81. }
  82.  
  83. @SuppressWarnings("deprecation")
  84. public static Frame splash(Image splashImage) {
  85. Frame f = new Frame();
  86. SplashWindow w = new SplashWindow(f, splashImage);
  87. w.toFront();
  88. w.show();
  89. if (! EventQueue.isDispatchThread()) {
  90. synchronized (w) {
  91. while (! w.paintCalled) {
  92. try {
  93. w.wait();
  94. } catch (InterruptedException e) {}
  95. }
  96. }
  97. }
  98. return f;
  99. }
  100. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC