Re: Painting Negative
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 25 May 2008 20:47:43 -0700
hiwa wrote:
On May 18, 1:44 pm, Knute Johnson <nos...@xxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Flo 'Irian' Schaetz wrote:Hi,There are a couple of ways to do this. The important thing to note is
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
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
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.
The AlphaCompsoite is killing you. Add the lines below.
-------------------------------------Composite c = g2d.getComposite();
/* 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);g2d.setComposite(c);
x = d.width / 2 -100;
y = d.height / 2 - 100;
size = 200;
g2d.fillOval(x, y, size, size);
Composite c = g2d.getComposite();
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);g2d.setComposite(c);
x = x + (x0 - x1);
y = y + (y0 - y1);
g2d.fillOval(x, y, size, size);
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.
--
Knute Johnson
email s/knute/nospam/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.
- Follow-Ups:
- Re: Painting Negative
- From: hiwa
- Re: Painting Negative
- References:
- Painting Negative
- From: Flo 'Irian' Schaetz
- Re: Painting Negative
- From: Knute Johnson
- Re: Painting Negative
- From: hiwa
- Painting Negative
- Prev by Date: Re: Painting Negative
- Next by Date: Re: Painting Negative
- Previous by thread: Re: Painting Negative
- Next by thread: Re: Painting Negative
- Index(es):
Relevant Pages
|