June 26, 2009

Published June 26, 2009 by

Shaped (Rounded Rectangle, Polygon, Oval, Arc etc) and Translucent Application Windows

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:


Figure: Rounded Rectangle Shaped Window


Figure: Rounded Rectangle Shaped Window with Translucent Effect

Source Code:
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:


Figure: Polygon Shaped Window


Figure: Polygon Shaped with Translucent Effect

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:

Figure: Oval Shaped Window


Figure: Oval Shaped Window with Translucent Effect

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();
    }

}
Circle Shaped Window:


Figure: Circle Shaped Window


Figure: Circle Shaped Window with Translucent Effect

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();
    }

}
Arc Shaped Window:


Figure: Arc Shaped Window


Figure: Arc Shaped Window with Translucent Effect

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();
    }

}
Reference:

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
Read More

May 20, 2009

Published May 20, 2009 by

Using Borders for Components

Class BorderFactory has various methods for rendering a border around the edges of a swing component.

Some are:

Empty Border: It takes up space but does no drawing, specifying the width of the top, left, bottom, and right sides.



Etched Border: It’s a border with an "etched" look using the component's current background color for highlighting and shading. There are two types : EtchedBorder.RAISED, or EtchedBorder.LOWERED





Line Border: A line border with the specified color.



Bevel Border: It’s a beveled border of the specified type (either BevelBorder.LOWERED or BevelBorder.RAISED), using the specified highlighting and shadowing. The outer edge of the highlighted area uses a brighter shade of the highlight color. The inner edge of the shadow area uses a brighter shade of the shadow color.





Titled Border: It adds a title to an existing border, with positioning (example, sitting on the top line), justification (example, leading) and the default font and text color (determined by the current look and feel).



Justification can be one of the following:
• TitledBorder.LEFT
• TitledBorder.CENTER
• TitledBorder.RIGHT
• TitledBorder.LEADING
• TitledBorder.TRAILING
• TitledBorder.DEFAULT_JUSTIFICATION (leading)
Vertical position of the text in relation to the border can be one of the following:
• TitledBorder.ABOVE_TOP
• TitledBorder.TOP (sitting on the top line)
• TitledBorder.BELOW_TOP
• TitledBorder.ABOVE_BOTTOM
• TitledBorder.BOTTOM (sitting on the bottom line)
• TitledBorder.BELOW_BOTTOM
• TitledBorder.DEFAULT_POSITION (top)


Source Code for the programs:

public class BordersViewer extends javax.swing.JFrame {
       
    public BordersViewer() {
        initComponents();
    }

    
    private void initComponents() {

        borderTypesButtonGroup = new javax.swing.ButtonGroup();
        borderTypesPanel = new javax.swing.JPanel();
        rbtnEmpty = new javax.swing.JRadioButton();
        rbtnLoweredEtched = new javax.swing.JRadioButton();
        rbtnRaisedEtched = new javax.swing.JRadioButton();
        rbtnLine = new javax.swing.JRadioButton();
        rbtnLoweredBevel = new javax.swing.JRadioButton();
        rbtnRaisedBevel = new javax.swing.JRadioButton();
        rbtnTitled = new javax.swing.JRadioButton();
        borderPanel = new javax.swing.JPanel();
        lblBorder = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        borderTypesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Border Types"));

        borderTypesButtonGroup.add(rbtnEmpty);
        rbtnEmpty.setSelected(true);
        rbtnEmpty.setText("Empty");
        rbtnEmpty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnEmptyActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnEmpty);

        borderTypesButtonGroup.add(rbtnLoweredEtched);
        rbtnLoweredEtched.setText("Lowered Etched");
        rbtnLoweredEtched.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnLoweredEtchedActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnLoweredEtched);

        borderTypesButtonGroup.add(rbtnRaisedEtched);
        rbtnRaisedEtched.setText("Raised Etched");
        rbtnRaisedEtched.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnRaisedEtchedActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnRaisedEtched);

        borderTypesButtonGroup.add(rbtnLine);
        rbtnLine.setText("Line");
        rbtnLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnLineActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnLine);

        borderTypesButtonGroup.add(rbtnLoweredBevel);
        rbtnLoweredBevel.setText("Lowered Bevel");
        rbtnLoweredBevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnLoweredBevelActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnLoweredBevel);

        borderTypesButtonGroup.add(rbtnRaisedBevel);
        rbtnRaisedBevel.setText("Raised Bevel");
        rbtnRaisedBevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnRaisedBevelActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnRaisedBevel);

        borderTypesButtonGroup.add(rbtnTitled);
        rbtnTitled.setText("Titled");
        rbtnTitled.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnTitledActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnTitled);

        borderPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        lblBorder.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lblBorder.setText("This is an Empty Border");

        javax.swing.GroupLayout borderPanelLayout = new javax.swing.GroupLayout(borderPanel);
        borderPanel.setLayout(borderPanelLayout);
        borderPanelLayout
                .setHorizontalGroup(borderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, borderPanelLayout.createSequentialGroup()
                                .addContainerGap(175, Short.MAX_VALUE).addComponent(lblBorder).addGap(194, 194, 194)));
        borderPanelLayout
                .setVerticalGroup(borderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(borderPanelLayout.createSequentialGroup().addGap(71, 71, 71).addComponent(lblBorder)
                                .addContainerGap(92, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(borderPanel, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(borderTypesPanel, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(borderTypesPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(borderPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(39, Short.MAX_VALUE)));

        pack();
    }//

    private void rbtnEmptyActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBorder.setText("This is an Empty Border");
    }

    private void rbtnLoweredEtchedActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lblBorder.setText("This is a Lowered Etched Border");
    }

    private void rbtnRaisedEtchedActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        lblBorder.setText("This is a Raised Etched Border");
    }

    private void rbtnLineActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        lblBorder.setText("This is a Line Border");
    }

    private void rbtnLoweredBevelActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblBorder.setText("This is a Lowered Bevel Border");
    }

    private void rbtnRaisedBevelActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        lblBorder.setText("This is a Raised Bevel Border");

    }

    private void rbtnTitledActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory
                .createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Titled Border"));
        lblBorder.setText("This is a Titled Border");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BordersViewer().setVisible(true);
            }
        });
    }

    private javax.swing.JPanel borderPanel;
    private javax.swing.ButtonGroup borderTypesButtonGroup;
    private javax.swing.JPanel borderTypesPanel;
    private javax.swing.JLabel lblBorder;
    private javax.swing.JRadioButton rbtnEmpty;
    private javax.swing.JRadioButton rbtnLine;
    private javax.swing.JRadioButton rbtnLoweredBevel;
    private javax.swing.JRadioButton rbtnLoweredEtched;
    private javax.swing.JRadioButton rbtnRaisedBevel;
    private javax.swing.JRadioButton rbtnRaisedEtched;
    private javax.swing.JRadioButton rbtnTitled;


}
Read More