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(); ...... .... |
JFrame with round rectangle shape |
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)
Elliptical shape for JFrame |
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); }... .. |
Semicircular JFrame using Arc2D shape |
No comments:
Post a Comment