How to Change Shape of JFrame - Rounded Rectangle, Circle, Ellipse etc

You can change the shape of the JFrame. By default its shape is rectangular and assumes the size set using setSize() method. In this post we will discuss how to change the shape of  JFrame. You can change JFrame's shape into rounded rectagle, cirlce, ellipse, etc. We will see how to set the shape of JFrame with java code sample and screenshots. To change the shape of JFrame to any shape, we use the setShape() method.

Prerequisites

There are some prerequisites for setting or changing the shape of JFrame. They are:
The Frame should be undecorated. You should call the setUndecorated(true) method before calling setShape() method. When you set the frame undecorated, you will lose the title-bar,close button, maximise and minimise buttons. So you will have to create these button syourself.
The Frame should not be displayable. You should use the setShape() method before you call pack() method. If you are using NetBeans IDE, call setUndecorated(true) and setShape() methods before calling initComponents() method.

Rounded Rectangle Shape

To change the shape of JFrame into a rounded rectangle (a rectangle with rounded corners), you can use the setShape() method along with RoundRectangle2D.Double(double x, double y, double w, double h, double arcw, double arch) method. The sample code is given below.
import java.awt.geom.RoundRectangle2D;

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        setLayout(null);
        setUndecorated(true);
        setSize(400,300);
        setLocationRelativeTo(null);
        setShape(new RoundRectangle2D.Double(30,30, 350,200, 50,100));
        initComponents();
......
....

changing shape of Frame Setting Shape of Frame Java code JFrame shape JFrame rectangle round rectangle shape rounded corners
JFrame with round rectangle shape
Let us now look at the prameters of the method:
 RoundRectangle2D.Double(double x, double y, double w, double h, double arcw, double arch)
x - x co-ordinate of location of the JFrame. y- y co-ordinate of location of the JFrame. w - width of the shape (round Rectangle). h - height of the shape (round Rectangle). arcw - width of the arcs at the corners.     arch - height of the arcs at the corners.       
For better visibility of the shape, the following relation should be satisfied. Height of Jframe = height of shape + arc height and Width of Jframe = width of shape + arc width

Elliptical or Circular Shape

You can change the shape of JFrame in to circle or ellipse. First, i will show you how to change shape of JFrame to Ellipse. Then i will show you how to set shape of JFrame to circle. We create an ellipse using the method Ellipse2D.Double(double x, double y, double w, double h)  to create an ellipse. Then set it as the shape of JFrame using setShape() method. Look at the code.
import java.awt.Color;
import java.awt.geom.Ellipse2D;

public class NewJFrame1 extends javax.swing.JFrame {

    public NewJFrame1() {
        setUndecorated(true);
        setResizable(true);
        setSize(500,250);
        Ellipse2D a=new Ellipse2D.Double(0,0,500,250);
        setShape(a);
        getContentPane().setBackground(Color.BLUE);
        setLocationRelativeTo(null);
    }
...
...
The arguments are as follows:
x - x co-ordinate of upper-left corner of the framing rectangle of this ellipse. y - y co-ordinate of upper-left corner of the framing rectangle of this ellipse. w - width of the ellipse (length of major axis) h- height of ellipse (length of minor axis)
how to change or set shape of JFrame to circle or elliptical shape. shape of Frame circle or ellipse
Elliptical shape for JFrame
Now, to set the shape of the JFrame to circular, just set w equal to h. That is, an ellipse with equal width and height (major and minor axes of equal length) is a circle. So, put the diameter of circle instead of w and h in Ellipse2D.Double(double x, double y, double w, double h)  method. Thus, your JFrame will be circular in shape.

Arc Shape

You can also set the shape to arc. It can be arc of any width, height, angle etc. To set your JFrame's shape into an arc, use the Ellipse2D a=new Arc2D.Double(double x,double y,double w,double h,double start,double extent,int type) method. The following code shows an example program which changes the shape of JFrame into an arc which forms a semicircle.
public class NewJFrame1 extends javax.swing.JFrame {

    public NewJFrame1() {
        setUndecorated(true);
        setResizable(true);
        setSize(700,600);
        Arc2D a=new Arc2D.Double(0,0,700,600,0,180,Arc2D.PIE);
        setShape(a);
        getContentPane().setBackground(Color.BLUE);
        setLocationRelativeTo(null);
    }
... ..
The output is as follows:
Change shape of JFrame to arc or pie set shape of JFrame to semicircle, circle, arc or pie
Semicircular JFrame using Arc2D shape
The arguments of the method Arc2D.Double(double x,double y,double w,double h,double start,double extent,int type) are as follows: x - x coordinate of the upper-left corner of the arc's framing rectangle. y - y coordinate of the upper-left corner of the arc's framing rectangle. w - Overall width of the full ellipse of which this arc is a partial section. h - Overall height of the full ellipse of which this arc is a partial section. start - Starting angle of the arc in degrees. extent - Angular extent of the arc in degrees. type - Closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE. You may also form other shapes for you JFrame using Polygon Shape.

No comments:

Post a Comment