Sunday, October 21, 2007

SplashScreen for impatient Users :)

One more API in Java for those who are impatient in handling Java Application. A heavy application in Java takes time before starting up and hence JDK6 comes with a concept of SplashScreen. Its a simple class in JDK6 where user can show a screen on top of his application screen(before it getting start) that ,be patient, application will run in few seconds. Good part, it also supports gif image, so for completely impatient user who generally starts clicking here and there :D and screwing application, show a GIF image of progress bar.

It comes in various flavors:

- SplashScreen stay on the desktop till the main screen(application UI) will not come. Some polling or signal mechanism internally, I never bothered to see the code :D

- SplashScreen stay for given amount of time. A simple one.

Running is not an easy job. You have to define splash option with java

java -splash:imagename classname

I guess a good API for those who make games or some big application. One can give information like Copyright, Developer name, Loading..., Company name, etc etc. I mean generally for these information we waste one screen in our main application itself, why not use SplashScreen for that :).

You can see some sample code here:

http://www.devdaily.com/java/edu/SplashScreen/SplashScreenMain.java
http://java.sun.com/docs/books/tutorial/uiswing/misc/splashscreen.html

Wednesday, October 10, 2007

Java Font vs Native Font

So, here is one more problem of Java being machine independent. Font size depicts by Java Application will not be same as of Native Application, like if we consider Windows OS, native applications are MS office, Notepad and many more. Conversion from the size in points into device pixels depends on device resolution as reported by the platform APIs. Java 2D defaults to assuming 72 dpi.Platform defaults vary like Mac OS: 72 dpi, Linux/Solaris: 96 dpi, Windows: 96 dpi/120 dpi.So whenever you write a code for java font, be carefully of it's inconsistency with native application.

Look at this code, how to manage the font size on a particular platform(pretty simple)

import java.awt.*;
import javax.swing.*;

public class TextSize extends JPanel {
public void paint(Graphics g) {
Font font = new Font("Arial", Font.PLAIN, 30);
g.setFont(font);
g.drawString("Hello World", 25, 100);
Toolkit tk = Toolkit.getDefaultToolkit ();
int nativeResolution = tk.getScreenResolution();
final double javaResolution = 72.0; // java default is 72 dpi
Font font_new = new Font("Arial", Font.PLAIN, (int)Math.round(30.0*nativeResolution/javaResolution));
g.setFont(font_new);
g.drawString("Hello World", 25, 150);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
TextSize tx = new TextSize();
frame.add(tx);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}




















One can see the difference of Java and Native font size difference. The first "Hello World" is normal java code. Second " Hello World" is modified java code and third one is Native "Hello World". The staircase problem is also visible in Java Font, for removing that we can set ANTIALIAS_ON :-)