| | |
How to make a Splash Screen
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
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
import java.awt.Frame; import java.awt.Toolkit; import java.net.URL; public class Splash { public static void main(String[] args) { Frame splashFrame = null; URL imageURL = Splash.class.getResource("img.png"); if (imageURL != null) { splashFrame = SplashWindow.splash( Toolkit.getDefaultToolkit().createImage(imageURL) ); } else { System.err.println("Splash image not found"); } try { // Change the class name to match your entry class Class.forName("MainApp").getMethod("main", new Class[] {String[].class}).invoke(null, new Object[] {args}); } catch (Throwable e) { e.printStackTrace(); System.err.flush(); System.exit(10); } if (splashFrame != null) { splashFrame.dispose(); } } } /*END OF CLASS*/ import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.Window; public class SplashWindow extends Window { /** * */ private static final long serialVersionUID = 1L; private Image splashImage; private boolean paintCalled = false; public SplashWindow(Frame owner, Image splashImage) { super(owner); this.splashImage = splashImage; MediaTracker mt = new MediaTracker(this); mt.addImage(splashImage,0); try { mt.waitForID(0); } catch(InterruptedException ie) {} int imgWidth = splashImage.getWidth(this); int imgHeight = splashImage.getHeight(this); setSize(imgWidth, imgHeight); Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); setLocation( (screenDim.width - imgWidth) / 2, (screenDim.height - imgHeight) / 2 ); } @Override public void update(Graphics g) { g.setColor(getForeground()); paint(g); } @Override public void paint(Graphics g) { g.drawImage(splashImage, 0, 0, this); if (! paintCalled) { paintCalled = true; synchronized (this) { notifyAll(); } } } @SuppressWarnings("deprecation") public static Frame splash(Image splashImage) { Frame f = new Frame(); SplashWindow w = new SplashWindow(f, splashImage); w.toFront(); w.show(); if (! EventQueue.isDispatchThread()) { synchronized (w) { while (! w.paintCalled) { try { w.wait(); } catch (InterruptedException e) {} } } } return f; } }
Similar Threads
- How to make a Splash Screen (Java)
- Splash screen in netbeans 6.0 (Java)
- Gateway splash screen, then nothing (Troubleshooting Dead Machines)
- splash screen woes (Java)
- Splash Screen (VB.NET)
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows



