Home Java javaTutorial java drawing merge image AlphaComposite mode test

java drawing merge image AlphaComposite mode test

Oct 12, 2018 pm 02:24 PM

The content of this article is about the AlphaComposite mode test of java drawing and merging images. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

package com.hdwang.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by hdwang on 2018/10/11.
 */
public class TestDrawing {
    public static void main(String[] args) {
        testComposite();
    }

    /**
     * 合成测试
     */
    public static void testComposite() {
        //创建背景图片(带透明分量的)
        BufferedImage bg = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bgGraphics = (Graphics2D) bg.getGraphics();
        bgGraphics.setColor(Color.yellow); //设置颜色
        bgGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //边缘抗锯齿
        bgGraphics.fillRect(0, 0, bg.getWidth(), bg.getHeight()); //填充矩形
        bgGraphics.setColor(Color.BLACK);
        bgGraphics.setFont(new Font("楷体", Font.ITALIC, 50));
        bgGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //文本抗锯齿
        bgGraphics.drawString("背景黄色", 50, 150); //画文本
        bgGraphics.dispose();

        //创建第二张图片
        BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
        Graphics2D imageGraphics = (Graphics2D) image.getGraphics();
        imageGraphics.setColor(Color.GREEN);
        imageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        imageGraphics.fillRoundRect(0, 0, image.getWidth(), image.getHeight(), image.getWidth(), image.getHeight());
        imageGraphics.setColor(Color.BLACK);
        imageGraphics.setFont(new Font("楷体", Font.ITALIC, 50));
        imageGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        imageGraphics.drawString("前景绿色", 50, 200);
        imageGraphics.dispose();

        JFrame jf = new JFrame(); //窗体
        JPanel contentPanel = new JPanel(); //内容面板
        contentPanel.setBorder(BorderFactory.createLineBorder(Color.blue)); //设置边框
        //contentPanel.setLayout(new BorderLayout());

        JLabel label = new JLabel();
        label.setText("组合模式:");
        contentPanel.add(label);

        JComboBox comboBox = new JComboBox();
        comboBox.addItem("默认");
        comboBox.addItem("CLEAR");
        comboBox.addItem("SRC");
        comboBox.addItem("DST");
        comboBox.addItem("SRC_OVER");
        comboBox.addItem("DST_OVER");
        comboBox.addItem("SRC_IN");
        comboBox.addItem("DST_IN");
        comboBox.addItem("SRC_OUT");
        comboBox.addItem("DST_OUT");
        comboBox.addItem("SRC_ATOP");
        comboBox.addItem("DST_ATOP");
        comboBox.addItem("XOR");
        contentPanel.add(comboBox);

        jf.setContentPane(contentPanel); //窗体设置内容面板为jp
        jf.setBounds(200, 200, 500, 500);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true); //窗体可见


        DrawingPanel drawPanel = new DrawingPanel();
        drawPanel.setBounds(0,35,500,440);
        drawPanel.setPreferredSize(new Dimension(500,440));
        drawPanel.setBorder(BorderFactory.createLineBorder(Color.red)); //设置边框
        drawPanel.setBg(bg);
        drawPanel.setImage(image);
       // drawPanel.setAlphaComposite(AlphaComposite.SrcAtop);
        contentPanel.add(drawPanel);

        Map<String,AlphaComposite> compositeMap = new HashMap<>();
        compositeMap.put("CLEAR",AlphaComposite.Clear);
        compositeMap.put("SRC",AlphaComposite.Src);
        compositeMap.put("DST",AlphaComposite.Dst);
        compositeMap.put("SRC_OVER",AlphaComposite.SrcOver);
        compositeMap.put("DST_OVER",AlphaComposite.DstOver);
        compositeMap.put("SRC_IN",AlphaComposite.SrcIn);
        compositeMap.put("DST_IN",AlphaComposite.DstIn);
        compositeMap.put("SRC_OUT",AlphaComposite.SrcOut);
        compositeMap.put("DST_OUT",AlphaComposite.DstOut);
        compositeMap.put("SRC_ATOP",AlphaComposite.SrcAtop);
        compositeMap.put("DST_ATOP",AlphaComposite.DstAtop);
        compositeMap.put("XOR",AlphaComposite.Xor);
        //下拉框选中事件
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED){
                    String selected =  e.getItem().toString();
                    System.out.println(selected);
                    drawPanel.setAlphaComposite(compositeMap.get(selected));
                    drawPanel.repaint(); //重画
                }
            }
        });

        //窗体改变事件
        jf.addWindowStateListener(new WindowStateListener() {

            @Override
            public void windowStateChanged(WindowEvent e) {
                System.out.println("window state:"+e.paramString());
            }
        });
    }


    static class DrawingPanel extends JPanel{

        BufferedImage bg;
        BufferedImage image;
        AlphaComposite alphaComposite;

        public BufferedImage getBg() {
            return bg;
        }

        public void setBg(BufferedImage bg) {
            this.bg = bg;
        }

        public BufferedImage getImage() {
            return image;
        }

        public void setImage(BufferedImage image) {
            this.image = image;
        }

        public AlphaComposite getAlphaComposite() {
            return alphaComposite;
        }

        public void setAlphaComposite(AlphaComposite alphaComposite) {
            this.alphaComposite = alphaComposite;
        }

        /**
         * 重写paint方法
         * @param g
         */
        @Override
        public  void paint(Graphics g){
            //调用的super.paint(g),让父类做一些事前的工作,如刷新屏幕
            super.paint(g);

            //在面板上画画
            Graphics2D g2d = (Graphics2D)g;
            g2d.setComposite(AlphaComposite.Src);
            g2d.drawImage(bg,100,100,null); //背景图
            if(alphaComposite!=null) {
                g2d.setComposite(alphaComposite);
            }else{
                //默认SrcOver
                g2d.setComposite(AlphaComposite.SrcOver);
            }
            g2d.drawImage(image,100,100,null); //叠加图

            //g2d.setColor(Color.GREEN);
            //g2d.fillRoundRect(100,100,image.getWidth(),image.getHeight(),image.getWidth(),image.getHeight()); //叠加图层
            g2d.dispose();
        }
    }
}
Copy after login

The above is the entire content of this article. For more exciting information about Java, you can pay attention to PHP Chinese The

Java video tutorial and Java tutorial column of the Internet! ! !

The above is the detailed content of java drawing merge image AlphaComposite mode test. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

In Java remote debugging, how to correctly obtain constant values ​​on remote servers? In Java remote debugging, how to correctly obtain constant values ​​on remote servers? Apr 19, 2025 pm 01:54 PM

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

See all articles