
Sample
**********************************************************************
TRANSPARENT IMAGE THAT WILL BE USE IN SOURCE CODE (batman.png)
Note : Make sure batman.png locate at same location with java file below.You can use other location, but for this simple tutorial, we will use same location.
*******************************************************************
COMPLETE SOURCE CODE FOR : TransparentSplashScreen.java
*******************************************************************
import javax.swing.JWindow;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class TransparentSplashScreen extends JWindow
{
//Get transparent image that will be use as splash screen image.
Image bi=Toolkit.getDefaultToolkit().getImage("batman.png");
ImageIcon ii=new ImageIcon(bi);
public TransparentSplashScreen()
{
try
{
setSize(ii.getIconWidth(),ii.getIconHeight());
setLocationRelativeTo(null);
show();
Thread.sleep(10000);
dispose();
JOptionPane.showMessageDialog(null,"This program will exit !!!","<>",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
//Paint transparent image onto JWindow
public void paint(Graphics g)
{
g.drawImage(bi,0,0,this);
}
public static void main(String[]args)
{
TransparentSplashScreen tss=new TransparentSplashScreen();
}
}
*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

I tried your code... but don't get the same effect...
ReplyDeletethe white around the batman doesn't go transparent. Everything else works fine, but i just don't get the white away...
any ideas ?
What type of image that you use???png or gif???if you use png or gif, make sure it's background is transparent...not other color like white,grey,black or anything else...i hope it will help...if you use above image(batman.png)...it should has transparent background...
ReplyDeletei took the image you provide and used your code as well... but still had the same problem. i tried it on eclipse, jEdit and Netbeans... still problem.
ReplyDeleteIs there someone else who can try this code as well? I am a newbie in java, so i might be wrong as wel.
i took you code and made a some tweaks of my own to get the effect you are getting... so just check it out.
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class TransparentSplashScreen extends JWindow
{
//Get transparent image that will be use as splash screen image.
Image bi=Toolkit.getDefaultToolkit().getImage("C:/batman.png");
ImageIcon ii=new ImageIcon(bi);
JPanel panel;
JLabel label = new JLabel(new ImageIcon("C:/batman.png"));
public TransparentSplashScreen()
{
MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent evt) { System.exit(0); } };
addMouseListener(ml);
panel = new JPanel() {
public void paintComponent(Graphics grh) {
Paint p = new Color(0, 255, 0, 0);
Graphics2D g2d = (Graphics2D) grh;
g2d.setPaint(p);
}
};
panel.add(label);
add(panel);
setSize(ii.getIconWidth(),ii.getIconHeight());
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[]args)
{
TransparentSplashScreen tss=new TransparentSplashScreen();
com.sun.awt.AWTUtilities.setWindowOpaque(tss, false);
}
}
don't know if I'm allowed to do this...
ReplyDeletebut go check out this image
http://icons.iconarchive.com/icons/icontoaster/icons-10-bundle/256/clipping-sound-icon.png
save it and replace the batman image with this one...
I'm starting to love this post more and more.
test test and some more stuff.
i have the same problem, any one found the problem? thx :D
ReplyDeleteI notice that its OK in Java 1.6 but not in Java 1.4 (did not try in Java 1.5).
ReplyDeleteAnyone know how to make Java 1.4 leave a transparent JWindow?
i took your code make some tweaks and it works fine
ReplyDeleteimport javax.swing.JWindow;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class TransparentSplashScreen extends JWindow
{
//Get transparent image that will be use as splash screen image.
Image bi=Toolkit.getDefaultToolkit().getImage("batman.png");
ImageIcon ii=new ImageIcon(bi);
public TransparentSplashScreen()
{
try
{
setSize(ii.getIconWidth(),ii.getIconHeight());
setLocationRelativeTo(null);
show();
com.sun.awt.AWTUtilities.setWindowOpaque(this, false);
Thread.sleep(10000);
dispose();
JOptionPane.showMessageDialog(null,"This program will exit !!!","<>",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
//Paint transparent image onto JWindow
public void paint(Graphics g)
{
g.drawImage(bi,0,0,this);
}
public static void main(String[]args)
{
TransparentSplashScreen tss=new TransparentSplashScreen();
}
}
Can I ask if anyone is using mac??? it seem like between mac and window the procedure defers a little. @March5 and @June22, your code works fine in window, but it seems like you don't need to call up the
ReplyDeletecom.sun.awt.AWTUtilities.setWindowOpaque(this, false);
in the mac systems...
just need to double check this with someone.
Not works!
ReplyDeleteNice.... code posted by "AnonymousMar 4, 2011 06:41 PM" was working fine... thank you.... I have searched long for this.
ReplyDeletethnx allot August 7 and Baskar... for the mac users i have some good news...
ReplyDeleteSetting JWindow or JFrame Opacity
try {
Class awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
Method method = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
method.invoke(null, /*JFrame or JWindow here*/, /*The opacity level in float value from 0.0 to 1*/);
} catch (Exception exc) {
exc.printStackTrace();
}
Setting JWindow or JFrame Opaque
try {
Class awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
Method method = awtUtilitiesClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, /*JFrame or JWindow here*/, b);
} catch (Exception exc) {
exc.printStackTrace();
}
Seems to work in mac and windows... now the problem comes with linux, will try to sort this out using ubuntu.
checkout http://www.java-forums.org/members/duvanslabbert.html
will be posting new stuff there and on this blog as well to try and give you UI junkies out there something new to play with.
Could you give me a video tutorial link on Transparent Splash Screen??
ReplyDeleteits not working on jframe, why????
ReplyDelete