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

本文详细介绍了如何在Swing应用中使用Color类创建各种颜色效果,包括RGB、十六进制和加工颜色,以及如何通过GradientPaint实现渐变色,并展示了如何结合SwingX painter包扩展容器背景。

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);

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

在这里插入图片描述

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);
    }
}

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);
    }

在这里插入图片描述

END

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值