Membuat Form Yang Transparan di Java Netbeans

Pastinya sangat menarik jika kita bisa membuat form menjadi transparan. form kita terlihat lebih elegant dan sangat bergaya. bagamana caranya. simak tutorial yang berhasil saya dapatkan di google. namun belum begitu saya pelajari. hehehe. saya posting disini dulu biar gampang nyarinya kalau kalau udah punya waktu untuk berexpertimen

 

Purpose

This tutorial demonstrates usage of Translucent and Shaped Windows in Java Swing applications.

Time to Complete

Approximately 30 minutes.

Overview

As of the Java SE 6 update 10 (6u10) release, you can add translucent and shaped windows to your Swing applications. This functionality is part of the public AWT package in JDK 7. You can create windows of three forms-
Window with uniform translucency : You can create window with uniform translucency where each pixel has the same translucency (or alpha) value.
Window with per-pixel translucency : You can create window where each pixel has its own alpha value. Make a part of the window translucent.

Window with any shape object :
You can create windows with a certain shape like circle, oval etc.

Software and Hardware Requirements

The following is a list of software requirements:
  • Download and install JDK 7.0 at this link.
  • Download and install NetBeans 7.0.1 at this link.

Prerequisites

Before starting this tutorial, you should have the software installed as listed under Software Requirements.

How to Implement Uniform Translucency

Windows with uniform translucency have the same translucency for each pixel. You can create uniform translucency by invoking the setOpacity(float) method in the Window class. The float argument passed to this method should be between 0 and 1. It represents the translucency of the window. The smaller the number, the more transparent the window.
The following example creates a window that is 55% opaque (45% translucent). If the underlying platform does not support translucent windows, the example exits.
1. Create a New Project. Select File > New Project.
Show Screenshot for Step

2. Select Java from the Categories column and Java Application from the Projects column and then click Next.
Show Screenshot for Step


3. Perform the following steps.
a. Name the project TranslucentWindow.
b. Uncheck the Create Main Class check box.
c. Click Finish.
Show Screenshot for Step


4. Right-click TranslucentWindow Project and select New > JFrame Form .
Show Screenshot for Step

5. Name the JFrame  TranslucentWindowDemo , package name demo, and then click Finish.
Show Screenshot for Step


6. Perform the following steps :
a. Right-click  TranslucentWindowDemo.java and select Open .
Show Screenshot for Step
b. Click Source tab.

7. Add the following import statements to the java class, TranslucentWindow.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;


Show Screenshot for Step

8. Write the following lines of code in the main() method to determine whether translucency is supported by your system’s graphic device.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

Show Screenshot for Step
Not all platforms support these capabilities. An UnsupportedOperationException exception is thrown when a platform that does not support these features. Hence it is best practice to first check that the platform supports the capability. The GraphicsDevice class provides isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency) method
that you can use for this purpose. You can pass one of three enum values, defined in
GraphicsDevice.WindowTranslucency, namely: PERPIXEL_TRANSLUCENT, TRANSLUCENT, PERPIXEL_TRANSPARENT.

9. Add the below code to the main method to exit the program if translucency is not supported by the graphic device.

if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println("Translucency is not supported");
System.exit(0);
}

Show Screenshot for Step

10. Perform the following steps to the constructor of the class, TranslucentWindowDemo() .

1. Delete the below NetBeans generated line of code.
initComponents();
Show Screenshot for Step
2. Add the following lines to set the properties of the frame.
super("TranslucentWindowDemo");
setLayout(new GridBagLayout());
setSize(300,200);
setLocationRelativeTo(null);
setUndecorated(true);
getContentPane().setBackground(Color.blue);

Show Screenshot for Step
11. Add the below code to the constructor to create a Button, Close and add the button to the frame.
JButton btnClose = new JButton("Close");
add(btnClose);

Show Screenshot for Step

12. To the constructor, add event handling code to the Close button.
ActionListener al;
al = new ActionListener() {

public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
};
btnClose.addActionListener (al);

Show Screenshot for Step
The Close button responds to click event and closes the Frame .

13. Perform the following changes to the run() method .
1. Delete the below NetBeans generated line of code.
new TranslucentWindowDemo().setVisible(true);
Show Screenshot for Step
2. Next add the below lines of code.
TranslucentWindowDemo tw = new TranslucentWindowDemo();
tw.setOpacity(0.55f);
tw.setVisible(true);

Show Screenshot for Step
The above code performs the following :
1. Starts a thread to create the GUI.
2. Sets the transluency of the window to 55% .
3. Displays the window .

14. In the Projects pane, right-click TranslucentWindowDemo.java and choose Run File.
Show Screenshot for Step

15. The output with 55% translucent window will be as below.  You can click the Close button to close the Window.
Show Screenshot for Step
Note that the button is also affected by the uniform translucency. Setting the opacity affects the whole window, including any components that the window contains.
You can test the application with different opacity values .

How to implement Shaped Windows

 

A shaped window is an undecorated window whose appearance conforms to a specific shape . Pixels outside of the shape are transparent and reveal the background.
Java 7 allows you to create Window of various shapes. You can create circle, triangle, elliptic windows or more complex shape. You can create a shaped window by invoking the setShape(Shape) method in the Window class. The Shape passed to the method determines how the window is clipped.The following example creates an oval-shaped window. If the underlying platform does not support shaped windows, the example exits.
1. Right-click TranslucentWindow Project and select New > JFrame Form .
Show Screenshot for Step

2. Name the class ShapedWindowDemo, package name demo and then click Finish.

Show Screenshot for Step

3. Perform the following steps:
1. Right click ShapedWindowsDemo.java and select Open.
Show Screenshot for Step 2. Click Source tab.

4. Add the following import statements to the java class, ShapedWindowDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.Ellipse2D;
import static java.awt.GraphicsDevice.WindowTranslucency.*;


Show Screenshot for Step

5. Add the following lines of code in the main method to determine whether translucency is supported by your system’s graphic device.

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
final boolean isTranslucencySupported= gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);

Show Screenshot for Step

6. Add the below code to the main method to exit the program if translucency is not supported by the graphic device.

if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) {
System.err.println("Shaped windows are not supported");
System.exit(0);
}


Show Screenshot for Step

7. Perform the following steps to the constructor of the class, ShapedWindowDemo() .

1. Delete the below NetBeans generated line of code.
initComponents();
Show Screenshot for Step
2. Add the following lines of code .
super("ShapedWindow");
setLayout(new GridBagLayout());
addComponentListener(new ComponentAdapter() {

@Override
public void componentResized(ComponentEvent e) {
setShape(new Ellipse2D.Double(0, 0, getWidth(), getHeight()));
}
});
setUndecorated(true);
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JButton("I am a Button"));

Show Screenshot for Step

8. In the Projects pane, right-click ShapedWindowDemo.java and choose Run File.
 
Show Screenshot for Step
The output will be as below
Show Screenshot for Step


9. Make the following changes to the run() method.
1. Delete the below line of code.
new ShapedWindowDemo().setVisible(true);
Show Screenshot for Step
2. Add the following lines of code .
ShapedWindowDemo sw = new ShapedWindowDemo();
sw.setOpacity(0.7f);
sw.setVisible(true);

Show Screenshot for Step

The above code is used to set the Transparency of the window to 70%.

10. In the Projects pane, right-click ShapedWindowDemo.java and choose Run File.
 
Show Screenshot for Step
The output with 70% translucent shaped window will be as below.
Show Screenshot for Step

Artikel Lainnya