How to make a Splash Screen

Fungus1487 Fungus1487 is offline Offline Apr 30th, 2007, 8:11 am |
0
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
Quick reply to this message  
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. }
0
newtonorina newtonorina is offline Offline | Dec 1st, 2008
I tried your code but it refused to work.
 
 

Message:


Similar Threads
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC