Welcome to Dream.In.Code
Become a Java Expert!

Join 136,887 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,762 people online right now. Registration is fast and FREE... Join Now!




Make a transparent swing UI components

 
Reply to this topicStart new topic

> Make a transparent swing UI components

lordms12
Group Icon



post 9 Jul, 2008 - 10:39 AM
Post #1


In this tutorial I will explain how to create transparent swing UI components.

Let’s start with simple example of how to make a transparent button:
java
class TransparentButton extends JButton {
public TransparentButton(String text) {
super(text);
setOpaque(false);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
super.paint(g2);
g2.dispose();
}
}


To use TransparentButton instead of JButton all what you want to modify is transparency level (I will show you how in next paragraph) and overwrite constructors you want as the above code.

All what you want to change transparency level is to change the 0.5f value in line g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
To make it clearer
0.0f means 100% transparent (you will not see it)
0.5f means 50% transparent
1.0f means 0% transparent (Not transparent at all)


Let’s try this way now with popup menu:
java
class TransparentPopupMenu extends JPopupMenu {
public TransparentPopupMenu() {
super();
setOpaque(false);
}
}

class TransparentMenuItem extends JMenuItem{
public TransparentMenuItem(String text) {
super(text);
setOpaque(false);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
super.paint(g2);
g2.dispose();
}
}


Note: You can get rid of TransparentPopupMenu class but in this case you have to remember to call setOpaque(false); after initialize JPopupMenu object

And you can try this way with other components.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

abgorn
Group Icon



post 6 Aug, 2008 - 02:30 PM
Post #2
A picture that shows the effects of this code would be nice. If you wouldn't mind, it would be helpful.
Go to the top of the page
+Quote Post

lordms12
Group Icon



post 6 Aug, 2008 - 04:26 PM
Post #3
QUOTE(abgorn @ 6 Aug, 2008 - 03:30 PM) *

A picture that shows the effects of this code would be nice. If you wouldn't mind, it would be helpful.

Attached Image
Attached Image
Go to the top of the page
+Quote Post

abgorn
Group Icon



post 29 Aug, 2008 - 01:59 AM
Post #4
Thanks, I hope to use it in my own work. biggrin.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/3/08 09:03PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month