Generally most of the application windows are rectangle shaped. This article
show how to create different shaped rounded rectangle, polygonal, oval,
circled, arced windows in Java. This can help you to create various shaped
splash screens in Java. The window shaping and translucent effect is
introduced in JDK 6u10 release.
Shaped Windows:You can set any shape
in an undecorated window. Calling method setUndecorated(true) will make a
window undecorated. The shaped windows feature is available through the new
com.sun.awt.AWTUtilities class. Note that the com.sun.awt.AWTUtilities class
is not part of an officially supported API and appears as an implementation
detail. The API is only meant for limited use outside of the core platform. It
may change drastically between update releases, and it may even be removed or
be moved in some other packages or classes. To apply a shape to a window use
the setWindowShape method. The method uses the window argument to define the
window you want to set the shape to. Additionally, it uses the shape argument
to define the desired shape. The shape can be any instance of the
java.awt.Shape interface, for example, Ellipse2D.Float or
RoundRectangle2D.Float.
Translucent Windows:By the
translucency effect you can make application windows transparent that means
you can tune the general opacity of the windows. The general opacity level of
the application window is controlled by the the setWindowOpacity method. This
method takes window and opacity variables as arguments. The window argument
defines the window to apply the effect to. This argument must be an instance
of the java.awt.Window class or its descendant, such as javax.swing.JFrame.
The opacity argument is responsible for the level of opacity of the window.
The lower its value, the more transparent the window becomes. The range of
allowed values is [0f ; 1f]. If you set the value to 0f, the window becomes
completely transparent (i.e. invisible). If the value is set to 1f, then the
window becomes completely opaque (which is equal to a case when the effect is
not applied at all).
Now it’s time for action.
Rounded rectangle shaped window:
import com.sun.awt.AWTUtilities; import java.awt.Color; import java.awt.Shape; import java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; /** * * @author Shamsuddin Ahammad */ public class RoundedRectangleWindow { public RoundedRectangleWindow() { JFrame frame = new JFrame(); frame.setSize(700, 550); frame.setLocationRelativeTo(null);//to show the frame in the center of screen frame.setUndecorated(true); frame.setTitle("Rounded Rectangle Window"); JLabel label = new JLabel("A Rounded Rectangle Window"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.BLUE); frame.getContentPane().add(label); frame.getContentPane().setBackground(Color.PINK); /* * Setting window shape * RoundRectangle2D.Float(x,y,width,height,arcWidth,arcHeight) */ Shape shape = new RoundRectangle2D.Float(0, 0, frame.getWidth(), frame.getHeight(), 50, 50); AWTUtilities.setWindowShape(frame, shape); //translucency operations if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) { AWTUtilities.setWindowOpacity(frame, 0.65F); } frame.setVisible(true); //frame will dispose after 10 seconds try { Thread.sleep(10000); } catch (InterruptedException ie) { ie.printStackTrace(); } frame.dispose(); } public static void main(String args[]) { RoundedRectangleWindow roundedRectangleWindow = new RoundedRectangleWindow(); } }
Polygon Shaped Window:
Source Code:
import com.sun.awt.AWTUtilities; import java.awt.Color; import java.awt.Polygon; import java.awt.Shape; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; /** * * @author Shamsuddin Ahammad */ public class PolygonWindow { public PolygonWindow() { JFrame frame = new JFrame(); frame.setSize(700, 550); frame.setLocationRelativeTo(null);//to show the frame in the center of screen frame.setUndecorated(true); frame.setTitle("Polygon Window"); JLabel label = new JLabel("A Polygon(Octagon) Window"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.BLUE); frame.getContentPane().add(label); frame.getContentPane().setBackground(Color.PINK); /*int xpoints[]={140,372,512,512,372,140,0,0}; int ypoints[]={0,0,140,338,478,478,338,140};*/ //8 points for an octagon int xpoints[] = { 191, 509, 700, 700, 509, 191, 0, 0 }; int ypoints[] = { 0, 0, 161, 389, 550, 550, 389, 161 }; /*Setting window shape Polygon(xpoints,ypoints,numberOfPoints)*/ Shape shape = new Polygon(xpoints, ypoints, xpoints.length); AWTUtilities.setWindowShape(frame, shape); //translucency operations if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) { AWTUtilities.setWindowOpacity(frame, 0.65F); } frame.setVisible(true); //frame will dispose after 10 seconds try { Thread.sleep(10000); } catch (InterruptedException ie) { ie.printStackTrace(); } frame.dispose(); } public static void main(String args[]) { PolygonWindow roundedRectangleWindow = new PolygonWindow(); } }
Oval Shaped Window:
Source Code:
import com.sun.awt.AWTUtilities; import java.awt.Color; import java.awt.Shape; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; /** * * @author Shamsuddin Ahammad */ public class OvalWindow { public OvalWindow() { JFrame frame = new JFrame(); frame.setSize(700, 550); frame.setLocationRelativeTo(null);//to show the frame in the center of screen frame.setUndecorated(true); frame.setTitle("Oval Window"); JLabel label = new JLabel("An Oval Window"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.BLUE); frame.getContentPane().add(label); frame.getContentPane().setBackground(Color.PINK); /*Setting window shape Ellipse2D.Float(x,y,width,height)*/ Shape shape = new Ellipse2D.Float(0, 0, frame.getWidth(), frame.getHeight()); AWTUtilities.setWindowShape(frame, shape); //translucency operations if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) { AWTUtilities.setWindowOpacity(frame, 0.65F); } frame.setVisible(true); //frame will dispose after 10 seconds try { Thread.sleep(10000); } catch (InterruptedException ie) { ie.printStackTrace(); } frame.dispose(); } public static void main(String args[]) { OvalWindow roundedRectangleWindow = new OvalWindow(); } }
Source Code:
import com.sun.awt.AWTUtilities; import java.awt.Color; import java.awt.Shape; import java.awt.geom.Ellipse2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; /** * * @author Shamsuddin Ahammad */ public class CircleWindow { public CircleWindow() { JFrame frame = new JFrame(); frame.setSize(700, 700); frame.setLocationRelativeTo(null);//to show the frame in the center of screen frame.setUndecorated(true); frame.setTitle("Circle Window"); JLabel label = new JLabel("A Circle Window"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.BLUE); frame.getContentPane().add(label); frame.getContentPane().setBackground(Color.PINK); /*Setting window shape Ellipse2D.Float(x,y,width,height)*/ Shape shape = new Ellipse2D.Float(0, 0, frame.getWidth(), frame.getHeight()); AWTUtilities.setWindowShape(frame, shape); //translucency operations if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) { AWTUtilities.setWindowOpacity(frame, 0.65F); } frame.setVisible(true); //frame will dispose after 10 seconds try { Thread.sleep(10000); } catch (InterruptedException ie) { ie.printStackTrace(); } frame.dispose(); } public static void main(String args[]) { CircleWindow roundedRectangleWindow = new CircleWindow(); } }
Source Code:
import com.sun.awt.AWTUtilities; import java.awt.Color; import java.awt.Shape; import java.awt.geom.Arc2D; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; /** * * @author Shamsuddin Ahammad */ public class ArcWindow { public ArcWindow() { JFrame frame = new JFrame(); frame.setSize(700, 550); //show the frame in the center of screen frame.setLocationRelativeTo(null); frame.setUndecorated(true); frame.setTitle("Arc Window"); JLabel label = new JLabel("An Arc Window"); label.setHorizontalAlignment(SwingConstants.CENTER); label.setForeground(Color.BLUE); frame.getContentPane().add(label); frame.getContentPane().setBackground(Color.PINK); /*Setting window shape Arc2D.Float(x,y,width,height,start,extent,type)*/ Shape shape = new Arc2D.Float(0, 0, frame.getWidth(), frame.getHeight(), 0, 200, Arc2D.OPEN); AWTUtilities.setWindowShape(frame, shape); //translucency operations if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) { AWTUtilities.setWindowOpacity(frame, 0.65F); } frame.setVisible(true); //frame will dispose after 10 seconds try { Thread.sleep(10000); } catch (InterruptedException ie) { ie.printStackTrace(); } frame.dispose(); } public static void main(String args[]) { ArcWindow roundedRectangleWindow = new ArcWindow(); } }
Dmitry Bondarenko and Anthony Petrov (April 2008; Updated: August 2008)
How to Create Translucent and Shaped Windows,
http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows