Showing posts with label jwindow. Show all posts
Showing posts with label jwindow. Show all posts

Create splash screen in java using JWindow

Source code below will show you, how to create splash screen in java using JWindow




splashscreenimage.jpg


*****************************************************************************
COMPLETE SOURCE CODE FOR : CreateSplashScreenUsingJWindow.java
*****************************************************************************


import javax.swing.JWindow;
import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Dimension;

public class CreateSplashScreenUsingJWindow extends JWindow
{
Image imageForSplashScreen;
ImageIcon ii;

public CreateSplashScreenUsingJWindow()
{
//Get image for splash screen image.
//Put full address for your image location
//In my case, my image location is : C:\Documents and Settings\evergreen\Desktop\splashscreenimage.jpg
imageForSplashScreen=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\splashscreenimage.jpg");

//Create ImageIcon from Image
ii=new ImageIcon(imageForSplashScreen);

//Set JWindow size from image size
setSize(ii.getIconWidth(),ii.getIconHeight());

//Get current screen size
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();

//Get x coordinate on screen for make JWindow locate at center
int x=(screenSize.width-getSize().width)/2;

//Get y coordinate on screen for make JWindow locate at center
int y=(screenSize.height-getSize().height)/2;

//Set new location for JWindow
setLocation(x,y);

//Make JWindow visible.
setVisible(true);
}

//Paint image onto JWindow
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(imageForSplashScreen,0,0,this);
}

public static void main(String[]args)
{
CreateSplashScreenUsingJWindow cssujw=new CreateSplashScreenUsingJWindow();

try
{
//Make JWindow appear for 5 seconds before disappear
Thread.sleep(5000);
cssujw.dispose();
}
catch(Exception exception)
{
exception.printStackTrace();
}

JFrame frame=new JFrame("Main Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}


*****************************************************************************
JUST COMPILE AND EXECUTE IT
Note : Download splashscreenimage.jpg into your computer.
Change location for your splashscreenimage.jpg in the source
code. If you don't do it, the image will not appear.
*****************************************************************************

CLICK HERE FOR >> Java transparent splash screen

Close JWindow using mouse click on it

Source code below will show you how to create a JWindow that can terminate when we click on it.

************************************************************************
COMPLETE SOURCE CODE FOR : ClosingJWindowUsingMouseClick.java
************************************************************************


import javax.swing.JWindow;
import javax.swing.JLabel;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.awt.FlowLayout;

public class ClosingJWindowUsingMouseClick
{
public static void main(String[]args)
{
//Create a JWindow
JWindow window=new JWindow();

//Create a JLabel that we want to add to JWindow
JLabel label=new JLabel("Click me to exit");

//Set layout for JWindow
window.setLayout(new FlowLayout());

//Add label to JWindow
window.add(label);

//Create Mouse Listener that listen when someone click on that JWindow
MouseListener ml=new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
//Exit application
System.exit(0);
}
};

//Add Mouse Listener to JWindow
window.addMouseListener(ml);

//Set JWindow size with width=400 pixels and height=400 pixels
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Close JWindow using button

Source code below will show you how to exit a JWindow using a button.

************************************************************************
COMPLETE SOURCE CODE FOR : ClosingAJWindowUsingButton.java
************************************************************************


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;

public class ClosingAJWindowUsingButton extends JWindow implements ActionListener
{
JButton button=new JButton("Exit");

public ClosingAJWindowUsingButton()
{
button.addActionListener(this);
setLayout(new FlowLayout());
add(button);
setSize(400,400);
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==button)
{
System.exit(0);
}
}

public static void main(String[]args)
{
ClosingAJWindowUsingButton caj=new ClosingAJWindowUsingButton();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Set JWindow background color

Source code below will show you how to change JWindow background color. If we directly use setBackground method for JWindow, it seem it's color don't change. So we will use JPanel. In this case, we will set color that we want for JWindow background to JPanel background color. After that we will add the created JPanel into JWindow. So after this, if you want to create a JWindow that contain other components like JTextField or JButton with background color, you must add into a JPanel with background color that you want first. After that, add the JPanel into JWindow.

***********************************************************
COMPLETE SOURCE CODE FOR : SetJWindowBackgroundColor.java
***********************************************************


import java.awt.Color;

import javax.swing.JWindow;

import javax.swing.JPanel;

public class SetJWindowBackgroundColor
{
public static void main(String[]args)
{
//Color is base on RGB
//R=0 , G=219 , B=252
Color color=new Color(0,219,252);

JPanel myPanel=new JPanel();
myPanel.setBackground(color);

JWindow myWindow=new JWindow();
myWindow.add(myPanel);
myWindow.setSize(400,400);
myWindow.setVisible(true);
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

Put component into JWindow

Source code below will show you how to add a component into JWindow. In this case we will use button as component that we want to add into JWindow.

*******************************************************************
COMPLETE SOURCE CODE FOR : PutComponentIntoJWindow.java
*******************************************************************


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.BorderLayout;

import java.awt.Container;

public class PutComponentIntoJWindow
{
public static void main(String[]args)
{
//Create a button.
//Button will be a component that we will add into JWindow
JButton button=new JButton("MY BUTTON");

//Create a JWindow
JWindow window=new JWindow();

//When you want to add component into JWindow you must get it Container first
//After that you can add component into JWindow using it.
Container contain=window.getContentPane();

//Set container layout to BorderLayout
contain.setLayout(new BorderLayout());

//Add button into JWindow
contain.add(button,BorderLayout.CENTER);

//Set JWindow size
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Note : Thanks for someone that show me some light. It is wrong when we want to add component into JWindow like example(For example : window is variable for JWindow, it wrong when we want to add component into JWindow...we do like this window.add(button)).We must get it's container first, and after that we will add component into JWindow using container that we get.You can see source code above after i repaired it.

What is a JWindow ???

A JWindow is a container that can be displayed anywhere on the user's desktop. It does not have the title bar, window-management buttons, or other trimmings associated with a JFrame, but it is still a "first-class citizen" of the user's desktop, and can exist anywhere on it.
(Description From Java API)

JWindow also suitable when we want to build a splash screen for an application. What is splash screen ???
-Splash screen is a term used to describe an image that appears while computer program is loading(FROM WIKIPEDIA).
-For example :
Splash screen for Microsoft Word 2003



Create a JWindow

**********************************************************
COMPLETE SOURCE CODE FOR : CreateJWindow.java
**********************************************************


import javax.swing.JWindow;

public class CreateJWindow
{
public static void main(String[]args)
{
JWindow myWindow=new JWindow();
myWindow.setSize(400,400);
myWindow.setVisible(true);
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

Borderless window

Source code below will show you how to create borderless window in java.

*****************************************************************
COMPLETE SOURCE CODE FOR : BorderlessWindow.java
*****************************************************************


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.BorderLayout;

public class BorderlessWindow
{
public static void main(String[]args)
{
JButton button1=new JButton("MY BUTTON");
JWindow window=new JWindow();

window.getContentPane().add(button1,BorderLayout.CENTER);

window.setSize(400,400);

window.setVisible(true);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

RELAXING NATURE VIDEO