Java by API/java.awt/AlphaComposite
Содержание
- 1 AlphaComposite.CLEAR
- 2 AlphaComposite.DST_ATOP
- 3 AlphaComposite.DST_IN
- 4 AlphaComposite.DST_OUT
- 5 AlphaComposite.DST_OVER
- 6 AlphaComposite: getInstance(int rule, float alpha)
- 7 AlphaComposite.SRC
- 8 AlphaComposite.SRC_ATOP
- 9 AlphaComposite.SRC_IN
- 10 AlphaComposite.SRC_OUT
- 11 AlphaComposite.SRC_OVER
AlphaComposite.CLEAR
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.CLEAR", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite.DST_ATOP
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 Main extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 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 Main());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 120);
frame.setVisible(true);
}
}
AlphaComposite.DST_IN
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.DST_IN", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite.DST_OUT
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OUT));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.DST_OUT", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite.DST_OVER
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OVER));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.DST_OVER", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite: getInstance(int rule, float alpha)
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
g2.setPaint(Color.red);
g2.fill(r);
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
.4f);
g2.setComposite(c);
g2.setPaint(Color.blue);
g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
g2.drawString("www.jexp.ru", 25, 130);
}
}
AlphaComposite.SRC
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.SRC", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite.SRC_ATOP
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 Main extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 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_ATOP());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 120);
frame.setVisible(true);
}
}
AlphaComposite.SRC_IN
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.SRC_IN", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite.SRC_OUT
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OUT));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.SRC_OUT", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
AlphaComposite.SRC_OVER
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.ruposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = g2.getRenderingHints();
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(rh);
int x = 40, y = 40;
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.setColor(Color.green);
g.fillOval(x + 30, y + 30, 30, 30);
g2.setComposite(old);
g.setColor(Color.black);
g.drawString("AlphaComposite.SRC_OVER", x, y + 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}