Composition
In this part of the Java 2D programming tutorial, we will define compositing operations.Compositing is the combining of visual elements from separate sources into single images. They are used to create the illusion that all those elements are parts of the same scene. Compositing is widely used in film industry to create crowds, entire new worlds which would be expensive or impossible to create otherwise. (wikipedia.org)
Operations
There are several compositing operations. We will show some of them in the next code example. TheAlphaComposite
class implements basic alpha compositing rules for combining source and destination colors to achieve blending and transparency effects with graphics and images. Say we want to draw two objects on a panel. The first object drawn is called a destination. The second one a source. The
AlphaComposite
class determines how these two are going to be blended together. If we have a AlphaComposite.SRC_OVER rule, the pixels of the source object will be drawn, where the two objects overlap. Compositing.java
package com.zetcode;We draw two rectangles and combine them with six different compositing operations.
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Compositing extends JPanel {
int rules[] = {
AlphaComposite.DST,
AlphaComposite.DST_ATOP,
AlphaComposite.DST_OUT,
AlphaComposite.SRC,
AlphaComposite.SRC_ATOP,
AlphaComposite.SRC_OUT,
};
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
for (int x=20, y=20, i=0; i < rules.length; x+=60, i++) {
AlphaComposite ac = AlphaComposite.getInstance(rules[i], 0.8f);
BufferedImage buffImg = new BufferedImage(60, 60,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
gbi.setPaint(Color.blue);
gbi.fillRect(0, 0, 40, 40);
gbi.setComposite(ac);
gbi.setPaint(Color.green);
gbi.fillRect(5, 5, 40, 40);
g2d.drawImage(buffImg, x, y, null);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Composition");
frame.add(new Compositing());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 120);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
int rules[] = {Here we have six different compositing rules. Only two of these have practical significance. If not sure what compositing rule to use, pick up the SRC_OVER rule.
AlphaComposite.DST,
AlphaComposite.DST_ATOP,
AlphaComposite.DST_OUT,
AlphaComposite.SRC,
AlphaComposite.SRC_ATOP,
AlphaComposite.SRC_OUT,
};
AlphaComposite ac = AlphaComposite.getInstance(rules[i]);Here we get the
AlphaComposite
class. BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);We use a buffer image to perform the composition operations.
Graphics2D gbi = buffImg.createGraphics();
gbi.setComposite(ac);Sets the Composite for the Graphics2D context.
g2d.drawImage(buffImg, x, y, null);We draw the image on the panel.
Figure: Composition
Sun and Cloud
In the next example we show the Sun coming from behind a cloud. We will use composition technique in this animation.SunAndCloud.java
package com.zetcode;The Sun comes from behind the cloud. The cloud finally disappears.
import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SunAndCloud extends JPanel implements ActionListener {
Image sun;
Image cloud;
Timer timer;
private float alpha;
public SunAndCloud() {
sun = new ImageIcon(this.getClass().getResource("sun.png")).getImage();
cloud = new ImageIcon(this.getClass().getResource("cloud.png")).getImage();
timer = new Timer(800, this);
timer.start();
alpha = 1f;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
BufferedImage buffImg = new BufferedImage(220, 140,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
gbi.drawImage(sun, 40, 30, null);
gbi.setComposite(ac);
gbi.drawImage(cloud, 0, 0, null);
g2d.drawImage(buffImg, 20, 20, null);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Sun & Cloud");
frame.add(new SunAndCloud());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 210);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
alpha -= 0.1;
if (alpha <= 0) {
alpha = 0;
timer.stop();
}
repaint();
}
}
sun = new ImageIcon(this.getClass().getResource("sun.png")).getImage();We have two images, the Sun and the cloud.
cloud = new ImageIcon(this.getClass().getResource("cloud.png")).getImage();
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);We use the
AlphaComposite.SRC_OVER
rule. Here the source blends with destination and overwrites empty pixels. Figure: Sun & Cloud
In this part of the Java 2D tutorial, we have talked about image composition.
0 comments:
Post a Comment