Welcome to HQ's Blog

Hello World! If you've visited this blog before, welcome back. Please help me what article do you like by give it a comment. But if you're new with this blog, please feel free to stay ahead for a shortwhile, to read my articles. I assure you will get something by read it carefully. Haha...

Ok ok... By the way, this blog belongs to me, Haqqi. Yeah, that is my nickname. If you want to know about me, please read the top sidebar titled "About Me". If you want to know MORE about me, please feel free to add my Messenger (xp_guitarist) and chat with me. I am Indonesian. My English is not so good, so please don't laugh if my 'syntax' is wrong. But sometimes I will post my blog in Bahasa Indonesia (I think most of them will).

The reason I create this blog is, I want to share everything I can share. From my experience, computer program I used, my algorithms and source codes, and some useful infos and tips. So, if you want to request something I can, feel free to ask. And if you also want to share me something, I always open my hand, haha. Anyway, WELCOME TO MY BLOG!

This site is best viewed with Mozilla Firefox with resolution 1024x800 or more

Page copy protected against web site content infringement by Copyscape

ANNOUNCEMENT!!!

This blog is dead!!! I moved it to my new blog in http://haqqi.net. Thank you for reading this blog. Hope you will read my new blog too.


Tutorial Java : Membuat Splash Screen

Sep 10, 2009

Lagi-lagi tutorial Java sederhana


Woi, ketemu lagi. Mumpung lagi semangat-semangatnya nulis tutorial nih. Kali ini saya akan memberikan tutorial sederhana, yaitu bagaimana cara membuat splash screen yang biasanya ditampilkan sebelum sebuah program dijalankan. Sebenarnya, jika Anda memakai NetBeans, sangat mudah sekali kalau ingin membuat splash screen. Tutorialnya ada di sini. Sedangkan yang saya buat, bisa diaplikasikan meski pakai text editor biasa.

Teknik ini memanfaatkan class JWindow dari package javax.swing dan peran sebuah thread untuk melakukan sleep. Anda bisa mengacak-acak (baca: mempelajari) source code di bawah ini.


/*
* DO NOT REMOVE THIS COPYRIGHT
*
* This source code is created by Muhammad Fauzil Haqqi
* You can use and modify this source code but
* you are forbidden to change or remove this license
*
* Nick : Haqqi
* YM : xp_guitarist
* Email : fauzil.haqqi@gmail.com
* Blog : http://fauzilhaqqi.blogspot.com
*/

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

/**
*
* @author Haqqi
*/
public class SplashScreen extends JWindow {
//*********************//
//*** Variable area ***//
//*********************//
private BufferedImage image;

//************************//
//*** Constructor area ***//
//************************//
public SplashScreen(BufferedImage image) {
this.image = image;
}

//*******************//
//*** Method area ***//
//*******************//
public void showSplash(int duration) {
// primary panel, get from frame's content pane
JPanel pane = (JPanel) getContentPane();
// set the size based on image size
int width = image.getWidth();
int height = image.getHeight();
pane.setPreferredSize(new Dimension(width, height));

// label to display the image
JLabel label = new JLabel(new ImageIcon(image));
pane.add(label);

pack();
// set window to center of the screen
setLocationRelativeTo(null);
setVisible(true);

// sleep for a while
try {
Thread.sleep(duration);
} catch (InterruptedException ex) {}

setVisible(false);
dispose();
}

public static void main(String[] args) {
BufferedImage image = null;
try {
image = ImageIO.read(SplashScreen.class.getResource("logo-banner.gif"));
} catch (IOException ex) {}
SplashScreen ss = new SplashScreen(image);
ss.showSplash(2000);
}
}

Satu class tersebut, bisa Anda manfaatkan untuk splash screen dengan gambar yang berbeda, terserah Anda. Anda cukup parsing URL yang tepat dari gambar Anda. Pada contoh di atas, file gambar terletak dalam satu folder dengan file java dari class SplashScreen. Jadi saya menggunakan method getResource() untuk mendapatkan URL-nya.

Selamat mencoba!!!

2 comments:

Anonymous said...

good job..!!

Abdullah said...

bos, tutorialnya keren, bisa buat tutorial splash j2me ga bos?