[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Select a component over a transparent point of other one

I have a custom JCompnent that draw a image whit transparency. When two componenents are overlaped I want to be able to click over the transparent area of the front component and get the back component ( assume thar the point is the same in the container, and the back component point has a not transparent pixel at that point). I'm able to pick the color of a point and recognize that is a system color when that point is transparent, but its not secure assume that the back component will not have a pixel from that same system color.
I will appreciate any sugestion.
thanks.
0
rubentrancoso
Asked:
rubentrancoso
  • 5
  • 2
  • 2
  • +1
1 Solution
 
hoomanvCommented:
are you triying to click on a button that is behind another component which has a transparent area ?
can i see the code ?
0
 
rubentrancosoAuthor Commented:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.PixelGrabber;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class SandBox extends JFrame {

      private static final long serialVersionUID = -2580548786666251441L;
      IconListener il = new IconListener();
      
      public SandBox() {
            getContentPane().setLayout(null);
            setVisible(true);
            setSize(200,200);
            createIcon("documents.png",50,50);
            createIcon("documents.png",60,60);
            repaint();
      }

      public static void main(String[] args) {
            new SandBox();
      }
      
    private void createIcon(String file, int x, int y) {
             Icon icon = new Icon(file);
             icon.setLocation(x,y);
             icon.addMouseMotionListener(il);
             icon.addMouseListener(il);
             getContentPane().add(icon);
    }


      public class Icon extends JComponent {

            private static final long serialVersionUID = 4881039535394991956L;
            ImageIcon icon;

          public Icon(String imagem)
          {
              icon = new ImageIcon(imagem);
              this.setSize(icon.getIconWidth(),icon.getIconHeight());
          }

          protected void paintComponent(Graphics g) {
              icon.paintIcon(this, g, 0, 0);
              g.setColor(Color.BLACK);
              g.drawRect(0,0,icon.getIconWidth()-1,icon.getIconWidth()-1);
          }
          
          
          
      }

    class IconListener implements java.awt.event.MouseListener, MouseMotionListener {

          JComponent component;
          boolean locked = false;
          int xpick, ypick;
          
          public void mouseClicked(MouseEvent e) {
                setTarget(e);
          }

          public void mouseEntered(MouseEvent e) {
                setTarget(e);
          }

          public void mouseExited(MouseEvent e) {
                setTarget(e);
          }

          public void mousePressed(MouseEvent e) {
                locked = true;
                setTarget(e);
                xpick = e.getX();
                ypick = e.getY();
                Container parent = component.getParent();
                parent.remove(component);
                parent.add(component,0);
                parent.repaint();
                System.out.println(getPixelColor(component,xpick,ypick));
          }
          
          private Color getPixelColor(Component component, int x, int y){
                int w = component.getWidth();
                int h = component.getHeight();

                Image img = component.createImage(w, h);
                Graphics g = img.getGraphics();
                g.setColor(new Color(0,0,0));
                component.paint(g);

                int[] pixels = new int[1];
                PixelGrabber grabber =      new PixelGrabber(img, x, y, 1, 1, pixels, 0, 1);
                try {
                      grabber.grabPixels();
                } catch (InterruptedException e) {
                      System.err.println("interrupted waiting for pixels!");
                }
                int pixel = pixels[0];
                int red = (pixel >> 16) & 0xff;
                int green = (pixel >> 8) & 0xff;
                int blue = (pixel ) & 0xff;
                g.dispose();
                return new Color(red, green, blue);
          }

          public void mouseReleased(MouseEvent e) {
                locked = false;
                setTarget(e);
          }

          public void mouseDragged(MouseEvent e) {
                setTarget(e);
                if(locked)
                      component.setLocation(component.getX()+e.getX()-xpick,component.getY()+e.getY()-ypick);
          }

          public void mouseMoved(MouseEvent e) {
                setTarget(e);
          }
          
          private void setTarget( MouseEvent e) {
                if( !locked )
                      component = ((JComponent)e.getSource());
          }
    }
      
}
0
 
objectsCommented:
Sounds like you need to loop thru all the parent components children checking if the click falls within the bounds of the child and if so checking the pixel colour as you are currently doing.

> I have a custom JCompnent that draw a image whit transparency.

you can actually use a JLable for that.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rubentrancosoAuthor Commented:
Sure, but as I sad, pick the color value is not safe cause the transparent color is translataded to the system background color, not to say about a frame bakground picture which I not  have tested yet.
0
 
hoomanvCommented:
you can use Robot http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html
to get the color of a givven coordinate
0
 
rubentrancosoAuthor Commented:
It's allready been done by getPixelColor() that uses PixelGrabber. The problem is 'how can I know that a pixel is a transparent pixel' since when the component is painted the background assumes the system default color that can be used as a regular color in any image composition? Maybe I need to generate a transparent mask the point a want to test, but I have no idea on how it's done.
0
 
objectsCommented:
Sorry misunderstood what you were saying. To get the color I think you'll have to get colour from the actual image and not the component.
Perhaps add a method to your Icon class that returns the pixel colour (from the image) at a specifid x,y.

0
 
rubentrancosoAuthor Commented:
I found it
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20164342.html?query=transparent+mask&topics=86

I'll test it as soon as possible. It's near to your idea, objects.
0
 
rubentrancosoAuthor Commented:
It's done. Plese close and refund.
thanks.

      private boolean isPixelTransparent(Component component, int x, int y){
            int w = component.getWidth();
            int h = component.getHeight();

            BufferedImage bimage = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D bimageg = (Graphics2D)bimage.createGraphics();
            component.paint(bimageg);
            
            int[] pixels = new int[1];
            PixelGrabber grabber =      new PixelGrabber(bimage, x, y, 1, 1, pixels, 0, 1);
            try {
                  grabber.grabPixels();
            } catch (InterruptedException e) {
                  System.err.println("interrupted waiting for pixels!");
            }
            int pixel = pixels[0];
            int alpha = (pixel >> 24) & 0xff;
            bimageg.dispose();
            if ( alpha == 0 )
                  return true;
            else
                  return false;
      }
0
 
CetusMODCommented:
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now