Sunday, October 5, 2008

[Java How-To] #4 "Splash" Screens without a Plugin (Servoy 4)

Java How-To's are intended to provide real-world examples of how Java can be used to easily extend Servoy in useful ways. I will try to post a new one as often as possible.

Description

Because of the excellent support Servoy provides for scripting Java (via Rhino), creating a custom splash screen is a relatively simple scripting task, and you don't need a plugin to do it. All you need instead is a little knowledge of Swing, and enough skill with Photoshop® to create a cool splash image. ;-)

The code below creates a new frame, undecorates it (which is what makes it look like a "splash"), sizes it, shapes it, centers it, adds a graphic and text component from the media library and then displays the newly created frame for a specified period of time. The frame is disposed automatically thereafter.

You don't need to modify this code at all to get it working. Just upload a PNG image named "splash.png" to your Servoy media library, and also upload a simple text document named credits.txt. If you do want to edit this code though you certainly can. An example of doing so might be adding a progress meter or adding a border to the components etc.

If you want to change how long the splash is displayed for, just change the value of application.sleep(...).


Try this! I think you will be surprised at how good it works/looks.

Java Classes Used

For more information about any of the Java classes used, click their corresponding link below.

Total Lines of Code

Around 50

The Servoy 4 Code

function showSplash()
{
var pkgs = new JavaImporter(Packages.javax.swing.border.EmptyBorder,
Packages.javax.swing.ImageIcon,
Packages.javax.swing.JFrame,
Packages.javax.swing.JPanel,
Packages.javax.swing.JLabel,
Packages.javax.swing.JEditorPane,

java.awt.BorderLayout,
java.awt.Frame,
java.awt.Insets,
java.awt.GraphicsEnvironment,
java.awt.Point,
java.awt.Rectangle,

java.net.URL);

with(pkgs) {

// Create the splash frame and its components
var splash_panel = new JPanel(new BorderLayout(0,0));

var splash_image = new JLabel(new ImageIcon(new URL("media:///splash.png")));

var splash_credits_pane = new JEditorPane(new URL("media:///credits.txt"));
splash_credits_pane.setEditable(false);
splash_credits_pane.setMargin(new Insets(20,20,20,20));

splash_panel.add(splash_image, BorderLayout.CENTER);
splash_panel.add(splash_credits_pane, BorderLayout.SOUTH);
splash_panel.setBorder(new EmptyBorder(0,0,0,0));

var splash_frame = new JFrame();
splash_frame.setUndecorated(true);
splash_frame.getContentPane().add(splash_panel);
splash_frame.pack();

/*
Size/shape and center the splash frame

The bulk of this code was borrowed from a public post on Sun's
Java forum then converted to Servoy/JS.

See: http://forums.sun.com/thread.jspa?threadID=508444
*/
var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
var center = ge.getCenterPoint();
var bounds = ge.getMaximumWindowBounds();

var w = Math.max(bounds.width/2, Math.min(splash_frame.getWidth(), bounds.width)); // Using JS Math here, not Java Math
var h = Math.max(bounds.height/2, Math.min(splash_frame.getHeight(), bounds.height));
var x = center.x - w/2;
var y = center.y - h/2;

splash_frame.setBounds(x, y, w, h);

if (w == bounds.width && h == bounds.height)
{
splash_frame.setExtendedState(Frame.MAXIMIZED_BOTH);
}

// splash_frame.pack(); Optionally pack here (instead of above) to have the splash resize to shape of image

splash_frame.validate();

splash_frame.show();

application.sleep(3000);

splash_frame.dispose();
}
}


IMPORTANT: Please be advised that you should always backup your Servoy solution before implementing or attempting any of the techniques described in this blog. None of the techniques described are guaranteed to work in any way shape or form, or for any specific purpose. Use them entirely at your own risk.

8 comments:

Tom Parry said...

HI Jeff,
good looking show!

Please! Cn you put on the blog the versions of Servoy and Java required for this to work.

And in the next blog we have...

Jeff Bader said...

Hey thanks Tom!

This requires Java 1.4+ and Servoy 4 (although it could be tweaked to work with 3 e.g. by removing Java importer). I think as a general rule, moving forward, I am not going to post too much (if any) Servoy 3.x. code. So from here on out everyone can assume the code is for Servoy 4 unless otherwise stated.

Bob Cusick said...

Thanks for this post, Jeff. Nice work!

Jeff Bader said...

Hey thanks Bob! I hope 'Servoyans' find it useful.

thomasschnaus said...

I have a some questions about the "Splash" Screens without a Plugin (Servoy 4):

Is it possible to fill the splash screen with dynamic values from a variable or so?
Can I create a 'close'-button on it?
Why doesn't it work on webclient.
Do you have some more examples for non java programmers?

I think a 'splash' screen could be a cool preview before sending an order by mail or so for example.

Best regards
Thomas Schnaus

Jeff Bader said...

Hello and thanks for leaving a comment. Welcome to the blog!

It is possible to fill the splash screen with dynamic values from a variable. The question I would have for you is what type of dynamic data would you like to use? Text, media etc. If you let me know, I can add some sample code to the post.

Regarding a close button, there would be a couple of different ways to do that. One would be to simply not "undecorate" the frame. If you remove the following line: splash_frame.setUndecorated(true); then the frame will not be undecorated, and will in fact contain a close button and a minimize button and a maximize button (but then it won't feel too much like a splash ;-) ). There are a number of other ways to do this as well (if you want to keep the undecorated frame look). If you tell me where on the splash screen you would like the button, I can show you how to get it there.

Now on to the webclient question...Any code I post on this blog that uses Swing will not work in webclient. Swing is a set of GUI components (a "widget kit") that is/are designed for Java, not the web.

Finally, regarding code for non-java programmers, please see the whitepaper I wrote "Java for Servoy Developers." You can find it here: http://www.omnesoft.com/downloads/java_for_servoy_developers.pdf

If the whitepaper does not give you the knowledge to do what you want with Java and Servoy, let me know exactly what it is you are looking to do, and I will try to give you some code samples.

Thomas said...

Hi Jeff,

I'm thinking about using the "splash" screen for a couple of functions like a quick help, print preview and others. For that I would like to fill it by text (html) and/or pdf's created into variables.
Closing the "splash" screen by the close button in the frame that is not undecorated should be ok for me, but if you have a more "splash" like solution you are welcom.
How is the function to stay the screen open and let it close by the close button?

Thomas

Jeff Bader said...

Hello Thomas,

The code I have posted here for a splash screen won't do a good job of displaying PDF's or showing a print preview of any kind. It could show HTML in the same manner that a native Servoy HTMLArea filed does, or it could show plain or rich text.

I am going to send you an e-mail with some other options for help, previewing PDF etc.

Again, thanks for your interest in the blog!

Post a Comment