Re: Painting Negative



Flo 'Irian' Schaetz wrote:
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

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();
}
}
});
}
}

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • Re: Painting Negative
    ... that it is not possible to draw on a component and clear the alpha. ... I create a mask image that is black with an alpha hole in the middle. ... mask in a separate JPanel as long as you made sure that the JPanel was ...
    (comp.lang.java.gui)
  • Re: change the opacity of a layer selectively
    ... > into an additional layer to image A. ... Add a layer mask to ... the effect to the layer's alpha channel). ... in the channel column right click on the mask and choose to make a selection ...
    (comp.graphics.apps.gimp)
  • Re: blending one photo into another?
    ... luckily ps7 does have the Layer mask icon, so it makes it that little bit ... > I'd proceed in CS2 (can't remember when the Layer Mask icon was added to ... > and add a horizontal Gradient to this mask. ... > If you find that the Gradient Tool made too abrupt a transition, ...
    (alt.graphics.photoshop)
  • Re: Matching Color
    ... Assuming that I mask the penny ... can put a layer underneath the object layer with a fill, ... If I mask and tweak, without doing a knock-out, the object mask can be ... own use and not a commercial project, but PS is a continual learning ...
    (alt.graphics.photoshop)
  • Re: Matching Color
    ... is two different photographs taken on the same background. ... can put a layer underneath the object layer with a fill, ... However - and Photoshop users will understand this - changing the ... If I mask and tweak, without doing a knock-out, the object mask can be ...
    (alt.graphics.photoshop)