Saturday, June 8, 2013
Today I will tell you how you can create shaped windows in Java. This is an extraordinary feature which was added in Java7. According to this feature you can create any shape and use that shape for your window. You can also create complex shapes and give your windows that shape. Different shapes like oval, round rectangle or complex like L-shape or I-shape. Here we will inherit the JFrame class and then add the ComponentListener to it. This is because when the window will be resized the shape will be calculated automatically. We will create a shape using Ellipse2D.Double and then call setShape() method to assign the shape to it. Below is a screenshot
--------------------------------------------------------------------------------------------------------------------------
import java.awt.Shape;
import java.awt.GridBagLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.geom.*;
public class OvalShapedWindow extends JFrame {
public OvalShapedWindow() {
super("Oval Window");
setLayout(new GridBagLayout()); //setting layout
addComponentListener(new ComponentAdapter() {
// If the window is resized, the shape is recalculated
@Override
public void componentResized(ComponentEvent e) {
Shape winShape=new Ellipse2D.Double(0,0,getWidth(),getHeight());
setShape(winShape); //giving shape
}
});
setUndecorated(true); //removing title-bar
setSize(350,250);
setLocationRelativeTo(null); //positioning at center
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JButton("Click Me")); //adding Button
}
public static void main(String[] args) {
// Creating UI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
OvalShapedWindow sw = new OvalShapedWindow();
sw.setVisible(true); // Display the window
}
});
}
}
--------------------------------------------------------------------------------------------------------------------------
An oval-shaped window as seen on Ubuntu 12.04 |
Java Source Code
--------------------------------------------------------------------------------------------------------------------------import java.awt.Shape;
import java.awt.GridBagLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.geom.*;
public class OvalShapedWindow extends JFrame {
public OvalShapedWindow() {
super("Oval Window");
setLayout(new GridBagLayout()); //setting layout
addComponentListener(new ComponentAdapter() {
// If the window is resized, the shape is recalculated
@Override
public void componentResized(ComponentEvent e) {
Shape winShape=new Ellipse2D.Double(0,0,getWidth(),getHeight());
setShape(winShape); //giving shape
}
});
setUndecorated(true); //removing title-bar
setSize(350,250);
setLocationRelativeTo(null); //positioning at center
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JButton("Click Me")); //adding Button
}
public static void main(String[] args) {
// Creating UI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
OvalShapedWindow sw = new OvalShapedWindow();
sw.setVisible(true); // Display the window
}
});
}
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
Labels:Swing
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment