Java Tutorial/2D Graphics/AlphaComposite
Содержание
- 1 AlphaComposite.DST_IN
- 2 AlphaComposite.DST_OUT
- 3 AlphaComposite.DST_OVER
- 4 AlphaComposite.SRC
- 5 AlphaComposite.SRC_IN
- 6 AlphaComposite.SRC_OUT
- 7 AlphaComposite.SRC_OVER
- 8 Alpha Composite with BufferedImage
- 9 Composite Demo
- 10 Compositing is the combining of elements from separate sources into single images
- 11 Composition technique in this animation.
- 12 Draw ten rectangles with different levels of transparency
AlphaComposite.DST_IN
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeDSTIN extends JFrame {
public AlphaCompositeDSTIN() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeDSTIN();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.DST_IN;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
AlphaComposite.DST_OUT
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeDSTOUT extends JFrame {
public AlphaCompositeDSTOUT() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeDSTOUT();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.DST_OUT;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
AlphaComposite.DST_OVER
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeDSTOVER extends JFrame {
public AlphaCompositeDSTOVER() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeDSTOVER();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.DST_OVER;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
AlphaComposite.SRC
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeSRC extends JFrame {
public AlphaCompositeSRC() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeSRC();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.SRC;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
AlphaComposite.SRC_IN
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeSRCIN extends JFrame {
public AlphaCompositeSRCIN() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeSRCIN();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.SRC_IN;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
AlphaComposite.SRC_OUT
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeSRCOUT extends JFrame {
public AlphaCompositeSRCOUT() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeSRCOUT();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.SRC_OUT;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
AlphaComposite.SRC_OVER
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class AlphaCompositeSRCOVER extends JFrame {
public AlphaCompositeSRCOVER() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeSRCOVER();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.SRC_OVER;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
Alpha Composite with BufferedImage
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AlphaCompositeCLEAR extends JFrame {
public AlphaCompositeCLEAR() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);
}
public static void main(String arg[]) {
new AlphaCompositeCLEAR();
}
}
class DrawingCanvas extends JPanel{
float alphaValue = 1.0f;
int compositeRule = AlphaComposite.CLEAR;
AlphaComposite ac;
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D big = bi.createGraphics();
ac = AlphaComposite.getInstance(compositeRule, alphaValue);
big.setColor(Color.red);
big.drawString("Destination", w / 4, h / 4);
big.fill(new Ellipse2D.Double(0, h / 3, 2 * w / 3, h / 3));
big.setColor(Color.blue);
big.drawString("Source", 3 * w / 4, h / 4);
big.setComposite(ac);
big.fill(new Ellipse2D.Double(w / 3, h / 3, 2 * w / 3, h / 3));
g2D.drawImage(bi, null, 0, 0);
}
}
Composite Demo
import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class MainClass extends JFrame {
public static void main(String args[]) {
MainClass app = new MainClass();
}
public MainClass() {
super("Draw2D");
add("Center", new MyCanvas());
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class MyCanvas extends Canvas {
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
float alpha = 0.5f;
AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g.setComposite(composite);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image0 = toolkit.getImage("image0.gif");
Image image1 = toolkit.getImage("image1.gif");
g.drawImage(image0, 200, 100, this);
g.drawImage(image1, 100, 150, this);
}
}
}
Compositing is the combining of elements from separate sources into single images
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 CompositingDST extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST, 0.5f);
BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
gbi.setPaint(Color.red);
gbi.fillRect(0, 0, 40, 40);
gbi.setComposite(ac);
gbi.setPaint(Color.green);
gbi.fillRect(5, 5, 40, 40);
g2d.drawImage(buffImg, 20, 20, null);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Composition");
frame.add(new CompositingDST());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 120);
frame.setVisible(true);
}
}
Composition technique in this animation.
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 ImageComposite extends JPanel implements ActionListener {
Image a = new ImageIcon(this.getClass().getResource("a.png")).getImage();
Image b = new ImageIcon(this.getClass().getResource("b.png")).getImage();
Timer timer = new Timer(800, this);
float alpha = 1f;
public ImageComposite() {
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
BufferedImage buffImg = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D gbi = buffImg.createGraphics();
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
gbi.drawImage(a, 40, 30, null);
gbi.setComposite(ac);
gbi.drawImage(b, 0, 0, null);
g2d.drawImage(buffImg, 20, 20, null);
}
public void actionPerformed(ActionEvent e) {
alpha -= 0.1;
if (alpha <= 0) {
alpha = 0;
timer.stop();
}
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new ImageComposite());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 210);
frame.setVisible(true);
}
}
Draw ten rectangles with different levels of transparency
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TransparentRectangles extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for (int i = 1; i <= 10; i++) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, i * 0.1f));
g2d.fillRect(50 * i, 20, 50, 50);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Transparent Rectangles");
frame.add(new TransparentRectangles());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 100);
frame.setVisible(true);
}
}