Java Swing | Translucent and shaped Window in Java
Last Updated :
15 Apr, 2021
Java provides different functions by which we can control the translucency of the window or the frame. To control the opacity of the frame must not be decorated. Opacity of a frame is the measure of the translucency of the frame or component.
In Java, we can create shaped windows by two ways first by using the AWTUtilities which is a part of com.sum.awt package AWTUtilities class have a function – setWindowShape(Window w, Shape s) which sets the shape of the window and second by using setShape(Shape s) which sets the shape of the window to the specified shape.
In this article we will discuss about uniform translucency of window.
Methods used :
- setWindowShape(Window w, Shape s) : sets the shape of the window w to the specified shape s
- setShape(Shape s) : sets the shape of the window to the specified shape s
- setOpacity(float f) : sets the value of opacity of the frame
1. Program to create a translucent frame and control its translucency with the help of a JSlider
Java
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solveit extends JFrame implements ChangeListener {
static JFrame f;
static JSlider b;
static JLabel l;
public static void main(String[] args)
{
f = new JFrame( "translucent window" );
solveit s = new solveit();
l = new JLabel();
JPanel p = new JPanel();
b = new JSlider( 0 , 100 , 100 );
b.setPaintTrack( true );
b.setPaintTicks( true );
b.setPaintLabels( true );
b.setMajorTickSpacing( 20 );
b.setMinorTickSpacing( 5 );
b.addChangeListener(s);
p.add(b);
p.add(l);
f.add(p);
l.setText( "Opacity value is =" + b.getValue());
f.setSize( 300 , 300 );
f.setUndecorated( true );
f.setOpacity(b.getValue() * 0 .01f);
f.setLocation( 500 , 300 );
f.show();
}
public void stateChanged(ChangeEvent e)
{
l.setText( "opacity value is =" + b.getValue());
f.setOpacity(b.getValue() * 0 .01f);
}
}
|
Output :
2. Program to create a shaped translucent window (using AWTUtilities)and control its translucency using a slider
Java
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.awt.AWTUtilities;
class solveit extends JFrame implements ChangeListener {
static JFrame f;
static JSlider b;
static JLabel l;
public static void main(String[] args)
{
f = new JFrame( "translucent window" );
solveit s = new solveit();
l = new JLabel();
JPanel p = new JPanel();
b = new JSlider( 0 , 100 , 100 );
b.setPaintTrack( true );
b.setPaintTicks( true );
b.setPaintLabels( true );
b.setMajorTickSpacing( 20 );
b.setMinorTickSpacing( 5 );
b.addChangeListener(s);
p.add(b);
p.add(l);
f.add(p);
l.setText( "Opacity value is =" + b.getValue());
f.setSize( 300 , 300 );
f.setUndecorated( true );
f.setOpacity(b.getValue() * 0 .01f);
AWTUtilities.setWindowShape(f, new Ellipse2D.Float(20f, -30f, 250 .0f, 150 .0f));
f.setLocation( 500 , 300 );
f.show();
}
public void stateChanged(ChangeEvent e)
{
l.setText( "opacity value is =" + b.getValue());
f.setOpacity(b.getValue() * 0 .01f);
}
}
|
Output :
3. Program to create a shaped translucent window (using setShape)and control its translucency using a slider
Java
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.awt.AWTUtilities;
class solveit extends JFrame implements ChangeListener {
static JFrame f;
static JSlider b;
static JLabel l;
public static void main(String[] args)
{
f = new JFrame( "translucent window" );
solveit s = new solveit();
l = new JLabel();
JPanel p = new JPanel();
b = new JSlider( 0 , 100 , 100 );
b.setPaintTrack( true );
b.setPaintTicks( true );
b.setPaintLabels( true );
b.setMajorTickSpacing( 20 );
b.setMinorTickSpacing( 5 );
b.addChangeListener(s);
p.add(b);
p.add(l);
f.add(p);
l.setText( "Opacity value is =" + b.getValue());
f.setSize( 300 , 300 );
f.setUndecorated( true );
f.setOpacity(b.getValue() * 0 .01f);
f.setShape( new Ellipse2D.Float(20f, -30f, 250 .0f, 150 .0f));
f.setLocation( 500 , 300 );
f.show();
}
public void stateChanged(ChangeEvent e)
{
l.setText( "opacity value is =" + b.getValue());
f.setOpacity(b.getValue() * 0 .01f);
}
}
|
Output :
Note : The above programs might not run in an online compiler please use an offline IDE. It is recommended to use latest version of java to run the above programs, user might face problems if older versions of java are used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...