• swing- 使用颜色画笔装饰你的容器背景


    swing使用颜色画笔来装填你的容器背景

    最重要的颜色工具类Color类

    1.简单的颜色使用

    使用setBackground 可以很容易的改变面板的颜色
    最简单的使用方式, 莫过于使用Color的静态属性, 比如Color.RED,Color.ORANGE 等等

    2.使用rgb颜色

    Color类同样支持使用Rgb颜色, 使用方式如:
    new Color(255,200,200);

    3.使用16进制颜色

    使用16进制颜色,只需要调用decode方法创建即可
    Color hoverColor = Color.decode(“#E91E63”);

    4. 使用darken/brighter 等方法来加工颜色

    比如你想等到一个深红色的话,只需要使用
    Color darkRed=Color.RED.darken();

    5. 使用RGBA来虚化一些颜色

    只需要使用4位的构造函数,最后一位来指定 alpha 值即可
    new Color(0,0,0,0)

    在这里插入图片描述

    使用GradientPaint创建渐变色

    但是有时候,你可能不能满足纯色来装填你的容器, 不管你是在开发你的swing小工具或者游戏. 可能第一时间想到的使用图片来填充背景 .

    1.GradientPaint 使用方式

    GradientPaint 可以让你创建渐变色, 使用方式同样简单 ,指定初始坐标至终点坐标 之间,使用颜色1 填充至颜色2. 示例如下:

    GradientPaint gradientPaint = new GradientPaint(x1, y1, Color.RED,
                    x2, y2, Color.BLUE);
    
    • 1
    • 2

    你可以用它来创建, 渐变面板 ,边框 等等其他你想扩展的元素

    在这里插入图片描述

    2.GradientPaint 示例代码

    依赖于swingx 因为它对jxpanel 扩展了setBackgroundPainter ,支持让你使用Painte 来绘制颜色

    package cn.note.swing.example.demo.level6;
    
    import cn.hutool.core.util.StrUtil;
    import cn.note.swing.core.util.FrameUtil;
    import cn.note.swing.core.view.AbstractMigView;
    import cn.note.swing.core.view.theme.ThemeFlatLaf;
    import net.miginfocom.swing.MigLayout;
    import org.jdesktop.swingx.JXPanel;
    import org.jdesktop.swingx.painter.*;
    
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    import java.awt.*;
    import java.awt.geom.Area;
    import java.awt.geom.RoundRectangle2D;
    
    /**
     * 渐变面板 及渐变边框
     */
    public class GradientPanelTest extends AbstractMigView {
    
    
        @Override
        protected MigLayout defineMigLayout() {
            return new MigLayout("gap 50,insets 10");
        }
    
        @Override
        protected void render() {
            // 渐变面板
            JPanel leftGP = createGradientPanel(0.f, 0.f, 1.f, 0.f);
            JPanel rightGP = createGradientPanel(1.f, 0.f, 0.f, 0.f);
            JPanel topGP = createGradientPanel(0.f, 0.f, 0.f, 1.f);
            JPanel bottomGP = createGradientPanel(0.f, 1.f, 0.f, 0.f);
            JPanel diagonalGP = createGradientPanel(0.f, 0.f, 1.f, 1.f);
            view.add(leftGP);
            view.add(rightGP);
            view.add(topGP);
            view.add(bottomGP);
            view.add(diagonalGP, "wrap");
    
            // 渐变边框
            view.add(createGradientBorder(SwingConstants.LEFT));
            view.add(createGradientBorder(SwingConstants.RIGHT));
            view.add(createGradientBorder(SwingConstants.TOP));
            view.add(createGradientBorder(SwingConstants.BOTTOM));
            view.add(createGradientBorder(SwingConstants.CENTER), "wrap");
        }
    
        private int toInt(float v) {
            return new Float(v).intValue();
        }
    
        private JXPanel createPanel() {
            JXPanel panel = new JXPanel();
            panel.setPreferredSize(new Dimension(150, 150));
            return panel;
        }
    
        private JPanel createTitlePanel(String title, JXPanel jxPanel) {
            JPanel panel = new JPanel();
            panel.setBorder(new TitledBorder(title));
            panel.add(jxPanel);
            return panel;
        }
    
        /**
         * 创建渐变面板
         */
        private JPanel createGradientPanel(float x1, float y1, float x2, float y2) {
            String title = StrUtil.format("({},{})-({},{})", toInt(x1), toInt(y1), toInt(x2), toInt(y2));
            JXPanel gradientPanel = createPanel();
            gradientPanel.setPreferredSize(new Dimension(150, 150));
            GradientPaint gradientPaint = new GradientPaint(x1, y1, Color.RED,
                    x2, y2, Color.BLUE);
            gradientPanel.setBackgroundPainter(new MattePainter(gradientPaint, true));
            return createTitlePanel(title, gradientPanel);
        }
    
    
        /**
         * 创建渐变border
         */
        private JPanel createGradientBorder(int type) {
            JPanel panel = createPanel();
            GradientBorder border = new GradientBorder(2, type);
            panel.setBorder(border);
            return panel;
        }
    
        /**
         * 渐变border
         */
        class GradientBorder implements Border {
            private Insets margin;
            private GradientPaint gradientPaint;
            private int type;
            private int x1;
            private int y1;
            private int x2;
            private int y2;
    
    
            public GradientBorder() {
                this(2, SwingConstants.CENTER);
            }
    
            public GradientBorder(int size, int type) {
                super();
                this.margin = new Insets(size, size, size, size);
                this.type = type;
            }
    
            public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
                Graphics2D g2d = (Graphics2D) g;
                x1 = x;
                y1 = y;
                x2 = x;
                y2 = y;
                switch (type) {
                    case SwingConstants.LEFT:
                        x2 = x2 + width;
                        break;
                    case SwingConstants.RIGHT:
                        x1 = x1 + width;
                        break;
                    case SwingConstants.TOP:
                        y2 = y2 + height;
                        break;
                    case SwingConstants.BOTTOM:
                        y1 = y1 + height;
                        break;
                    default:
                        x2 = x2 + width;
                        y2 = y2 + width;
                        break;
                }
    //            GradientPaint gradientPaint=  new GradientPaint(x, y, Color.RED, x + width, y + height, Color.BLUE)
                GradientPaint gradientPaint = new GradientPaint(x1, y1, Color.RED, x2, y2, Color.BLUE);
                g2d.setPaint(gradientPaint);
                Area border = new Area(new Rectangle(x, y, width, height));
                border.subtract(new Area(new Rectangle(x + margin.left, y + margin.top,
                        width - margin.left - margin.right, height - margin.top - margin.bottom)));
                g2d.fill(border);
    
                // 辅助说明
                g2d.setPaint(Color.BLACK);
                String position = StrUtil.format("({},{})-({},{})", x1, y1, x2, y2);
                g2d.drawString(position, 30, 30);
            }
    
            public Insets getBorderInsets(Component c) {
                return margin;
            }
    
            public boolean isBorderOpaque() {
                return true;
            }
        }
    
        public static void main(String[] args) {
            ThemeFlatLaf.install();
            FrameUtil.launchTime(GradientPanelTest.class);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167

    swingx 的painter 可以更丰富的扩展你的容器背景

    1. painter包介绍

    swingx的painter包(org.jdesktop.swingx.painter) 封装了很多painter, 这些画笔可以让你更简单创建更丰富的容器背景, 而你没必要自定义UI 或者继承JPanel 覆写paintComponent 方法

    在这里插入图片描述

    2. 部分画笔示例

    你可以使用一些你关注的画笔来轻松做一些事情, 比如ImagePainter 可以让你轻松用图片 填充背景, 或者CompoundPainter 可以组合叠加画笔的使用

    
        /**
         * 形状画笔
         */
        private JPanel createShapePanel() {
            JXPanel panel = createPanel();
            RoundRectangle2D.Float rect = new RoundRectangle2D.Float(0.f, 0.f, 100.f, 100.f, 100.f, 100.f);
            ShapePainter p = new ShapePainter(rect);
            panel.setBackgroundPainter(p);
            return createTitlePanel("ShapePainter", panel);
        }
    
        /**
         * 矩形画笔
         */
        private JPanel createRectanglePanel() {
            JXPanel panel = createPanel();
            RectanglePainter rp = new RectanglePainter(20, 20, 20,
                    20, 20, 20);
            rp.setFillPaint(Color.RED);
            rp.setBorderPaint(Color.BLACK);
            rp.setStyle(RectanglePainter.Style.BOTH);
            rp.setBorderWidth(5);
            rp.setAntialiasing(true);
            panel.setBackgroundPainter(rp);
            return createTitlePanel("RectanglePainter", panel);
        }
    
        /**
         * 组合画笔
         */
        private JPanel createCompoundPainterPanel() {
            JXPanel panel = createPanel();
            MattePainter black = new MattePainter(Color.BLACK);
            PinstripePainter pp = new PinstripePainter(Color.WHITE, 45, 1, 10);
            CompoundPainter<Object> cp = new CompoundPainter<Object>(black, pp);
            panel.setBackgroundPainter(cp);
            return createTitlePanel("CompoundPainter", panel);
        }
    
    
        /**
         * 交叉画笔
         */
        private JPanel createCheckerboardPanel() {
            JXPanel panel = createPanel();
            CheckerboardPainter p = new CheckerboardPainter();
            p.setDarkPaint(Color.BLACK);
            p.setLightPaint(Color.WHITE);
            p.setSquareSize(25);
            panel.setBackgroundPainter(p);
            return createTitlePanel("CheckerboardPainter", panel);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述

    END

    swingx 可以方便你在swing 工具或游戏开发上 走一些捷径, 如果你对于修改swing 一些UI 力不从心时, 可以尝试使用一下

  • 相关阅读:
    java基础之字节输入与输出流[48]
    震惊!阿里卷成这样?不吃饭了,上厕所、团建都要聊工作,人均上厕所小于一天三次...
    利用神器Nginx + X-Accel,实现PHP大文件下载统计、权限判断、速度限制
    Oracle SQL执行计划操作(1)——表相关操作
    TCP/IP Illustrated Episode 10
    Vue3待办列表-日记与便签-LOL英雄资料-课程大作业
    N9000A 安捷伦Agilent信号分析仪
    【LeetCode】 哈希表的使用
    Bean实例
    SQL当前查询条件数据需要调用其他数据时创建临时表实现
  • 原文地址:https://blog.csdn.net/x308561498/article/details/127780475