Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2008

Tip: Looking for answers? Try searching our database.

Painting Negative

Thread view: 
Flo 'Irian' Schaetz - 17 May 2008 22:22 GMT
Hi,

I've got a JLayeredPane, on which I put an "ImagePanel" (which simply
shows an image, nothing special) on layer 0.

Then I want to add another component on layer 1, so that the whole
ImagePanel will be covered by, let's say a black rectangle. Still easy.

But then I want to be able to define Polygons and this polygons should
NOT be drawn on the layer 1 component, so that in this polygons the
ImagePanel below is visible...

So the big question is: How can I fill everything EXCEPT the polygons?

Flo
Roedy Green - 18 May 2008 00:52 GMT
>So the big question is: How can I fill everything EXCEPT the polygons?

fill the entire rectangle to start, then draw on top. Try
experimenting with various transparencies of paint till you see all
the effects possible.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Knute Johnson - 18 May 2008 05:44 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> Flo

There are a couple of ways to do this.  The important thing to note is
that it is not possible to draw on a component and clear the alpha.  You
can draw on a BufferedImage and do that however.  In the example below,
I create a mask image that is black with an alpha hole in the middle.
Then in the paintComponent() method, I draw my subject image and then
draw the mask over the top of that image.  In the portion where I have
the alpha hole, the image underneath shows through.  You could put the
mask in a separate JPanel as long as you made sure that the JPanel was
set to transparent (setOpaque(false)).

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test1 extends JPanel {
    final BufferedImage img;
    final BufferedImage mask;

    public test1() throws IOException {
        // read the subject image
        img = ImageIO.read(new File("kittens.jpg"));
        Dimension d = new Dimension(img.getWidth(),img.getHeight());
        setPreferredSize(d);

        // create a buffered image with alpha for the mask
        mask = new BufferedImage(d.width,d.height,
         BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = mask.createGraphics();

        // set anti-aliasing on for a smooth edge
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);

        // draw black on the whole image
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0,0,d.width,d.height);

        // use AlphaComposite Clear to cut an alpha hole in the mask image
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillOval(d.width/2-100,d.height/2-100,200,200);

        g2d.dispose();
    }

    public void paintComponent(Graphics g) {
        // draw the image
        g.drawImage(img,0,0,null);
        // draw the mask over the top of the image
        g.drawImage(mask,0,0,null);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    test1 t = new test1();

                    f.add(t,BorderLayout.CENTER);
                    f.pack();
                    f.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/linux/

Flo 'Irian' Schaetz - 18 May 2008 11:38 GMT
And thus spoke Knute Johnson...

>> So the big question is: How can I fill everything EXCEPT the polygons?
>>
[quoted text clipped - 9 lines]
> mask in a separate JPanel as long as you made sure that the JPanel was
> set to transparent (setOpaque(false)).

Works like a charm, thanks.

Flo
hiwa - 26 May 2008 02:54 GMT
On May 18, 1:44 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > Hi,
>
[quoted text clipped - 96 lines]
>       ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

I can't move your alpha hole by mouse dragging
unless I add the line in the mouseDragged()
method:

g2d = mask.createGraphics();

Why is it that? I do not dispose() the g2d in
the code below.
-------------------------------------
/* failed example */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.event.*;

public class Mask extends JPanel {
 final BufferedImage img;
 final BufferedImage mask;
 Graphics2D g2d;
 int x, y, x0, y0, x1, y1, size;
 Dimension d;

 public Mask() throws IOException {
   img = ImageIO.read(new File("images/ari3.png"));
   d = new Dimension(img.getWidth(),img.getHeight());
   setPreferredSize(d);

   mask = new BufferedImage(d.width,d.height,
       BufferedImage.TYPE_INT_ARGB);
   g2d = mask.createGraphics();

   g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
       RenderingHints.VALUE_ANTIALIAS_ON);

   g2d.setColor(Color.BLACK);
   g2d.fillRect(0,0,d.width,d.height);

   g2d.setComposite(AlphaComposite.Clear);
   x = d.width / 2 -100;
   y = d.height / 2 - 100;
   size = 200;
   g2d.fillOval(x, y, size, size);

   Mover mov = new Mover(this);
   addMouseListener(mov);
   addMouseMotionListener(mov);
 }

 public void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.drawImage(img, 0, 0, this);
   g.drawImage(mask, 0, 0, this);
 }

 public static void main(String[] args) {
   EventQueue.invokeLater(new Runnable() {
     public void run() {
       try {
         JFrame f = new JFrame();
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         Mask t = new Mask();

         f.add(t,BorderLayout.CENTER);
         f.pack();
         f.setVisible(true);
       }
       catch (Exception e) {
         e.printStackTrace();
       }
     }
   });
 }

 class Mover extends MouseInputAdapter{
   Mask m;

   public Mover(Mask mask){
     m = mask;
   }

   public void mousePressed(MouseEvent e){
     x0 = x1 = e.getX();
     y0 = y1 = e.getY();
   }

   public void mouseDragged(MouseEvent e){
     x1 = x0;
     y1 = y0;
     x0 = e.getX();
     y0 = e.getY();
     g2d.setColor(Color.BLACK);
     g2d.fillRect(0, 0, d.width, d.height);
     g2d.setComposite(AlphaComposite.Clear);
     x = x + (x0 - x1);
     y = y + (y0 - y1);
     g2d.fillOval(x, y, size, size);
     m.repaint();
   }
 }
}
---------------------------------------
Knute Johnson - 26 May 2008 04:47 GMT
> On May 18, 1:44 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
[quoted text clipped - 101 lines]
> Why is it that? I do not dispose() the g2d in
> the code below.

The AlphaCompsoite is killing you.  Add the lines below.

> -------------------------------------
> /* failed example */
[quoted text clipped - 27 lines]
>     g2d.setColor(Color.BLACK);
>     g2d.fillRect(0,0,d.width,d.height);

Composite c = g2d.getComposite();
>     g2d.setComposite(AlphaComposite.Clear);
>     x = d.width / 2 -100;
>     y = d.height / 2 - 100;
>     size = 200;
>     g2d.fillOval(x, y, size, size);
g2d.setComposite(c);

>     Mover mov = new Mover(this);
>     addMouseListener(mov);
[quoted text clipped - 46 lines]
>       g2d.setColor(Color.BLACK);
>       g2d.fillRect(0, 0, d.width, d.height);
Composite c = g2d.getComposite();
>       g2d.setComposite(AlphaComposite.Clear);
>       x = x + (x0 - x1);
>       y = y + (y0 - y1);
>       g2d.fillOval(x, y, size, size);
g2d.setComposite(c);
>       m.repaint();
>     }
>   }
> }
> ---------------------------------------

Just like using an AffineTransform, if you don't want it used any more
you have to set it back to the original.

Signature

Knute Johnson
email s/knute/nospam/

hiwa - 26 May 2008 11:40 GMT
On May 26, 12:47 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > On May 18, 1:44 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> > wrote:
[quoted text clipped - 220 lines]
>       ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

BIG THANKS!
BTW, isn't the name of the polar bear at the Berlin zoo Knute? I love
the sound, or the phonetic.
Knute Johnson - 26 May 2008 18:00 GMT
> On May 26, 12:47 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
[quoted text clipped - 188 lines]
> BTW, isn't the name of the polar bear at the Berlin zoo Knute? I love
> the sound, or the phonetic.

Yes he is but his name is spelled in the usual German way, Knut.

Signature

Knute Johnson
email s/knute/nospam/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.