欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java绘图合并图像AlphaComposite模式测试

程序员文章站 2024-01-29 12:02:40
package com.hdwang.test; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.util.HashM... ......
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();
        }
    }
}

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试

java绘图合并图像AlphaComposite模式测试