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

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




problem with Icon on customised transparent JButton

 
Reply to this topicStart new topic

problem with Icon on customised transparent JButton, icon also getting transparent with button

cybernaut09
15 Jun, 2008 - 05:23 AM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 20


My Contributions
I was trying to create a transparent button with an image on it . But as I make the button transparent , image also get transparent too . I do not want this to happen . I want only the background of the button to be transparent with no changes in the original image . Please help me with my mistakes.
Here is my code .
CODE

import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Image;
import java.awt.GradientPaint;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")

class myButton extends JButton
{
    String imgUrl,style;
    GradientPaint gp = new GradientPaint(100, 300, Color.GRAY, 0, 75, Color.WHITE, true);
    public myButton(String txt)
     {
        super();
        imgUrl=txt;
        setPreferredSize(new Dimension(50, 25));
        setBorderPainted(false);
        setFocusPainted(false);
        setContentAreaFilled(false);
    }
    
    public void paintComponent(Graphics g)
        {  
            Graphics2D g2d = (Graphics2D) g;
            int w= getWidth(),h=getHeight();
            // fill the clipped area
            g2d.setPaint(gp);
            g2d.fillRect(0, 0, w, h);
            // paint outer border
            g2d.setPaint(Color.GRAY);
            g2d.drawRect(0, 0, w+30, h-1 );
            // paint inner border
            g2d.setPaint(Color.WHITE);
            g2d.drawRect(1, 1, w+30 , h-3);
            // paint right outside border
            g2d.setPaint(Color.GRAY);
            g2d.drawLine(w - 1, 1, w - 1, h);
                        // paint right inside border
            g2d.setPaint(Color.WHITE);
            g2d.drawLine(w - 2, 2, w - 2, h - 1);
                                                            //----------adding image -----------
                        ImageIcon img=new ImageIcon(imgUrl);
            Image image = img.getImage();
            Composite oldcomp = g2d.getComposite();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
            g2d.drawImage(image, 15, 5, this);
            g2d.setComposite(oldcomp);
            super.paintComponent(g);
            g2d.dispose();
        }
}

here constructor myButton(String txt) can be used instead of JButton(String txt) . In mybutton constructor argument String txt has been used to pass the name of the image file ,its in .png format in my case .

This post has been edited by cybernaut09: 15 Jun, 2008 - 05:26 AM
User is online!Profile CardPM
+Quote Post

pbl
RE: Problem With Icon On Customised Transparent JButton
15 Jun, 2008 - 03:33 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
You want it transparent or not ?
If you make it transparent (setOpaque(false)) then sure the Icon won't be seen
You wuill have to play with the .gif properties which make an image transparent or not (I think)
User is online!Profile CardPM
+Quote Post

cybernaut09
RE: Problem With Icon On Customised Transparent JButton
16 Jun, 2008 - 12:09 AM
Post #3

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 20


My Contributions
QUOTE(pbl @ 15 Jun, 2008 - 04:33 PM) *

You want it transparent or not ?
If you make it transparent (setOpaque(false)) then sure the Icon won't be seen
You wuill have to play with the .gif properties which make an image transparent or not (I think)


So stupid of me tongue.gif . I wrote Transparent instead of translucent everywhere in my question .
sorry pbl , please help .
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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