Java学习之路-GUI-RGB滑动条调色板

用滑动条实现RGB调色板功能
package GUI;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class RGBPalette {

    public JFrame jf;
    public JPanel jp, jp_R, jp_G, jp_B, jp_Label;
    public JLabel jLabel_R, jLabel_G, jLabel_B, jLabel_BG, jLabel_Rt, jLabel_Gt, jLabel_Bt;
    public JSlider jSlider_R, jSlider_G, jSlider_B;
    public int r, g, b;

    public RGBPalette(){

        jf = new JFrame("RGB调色板");
        jf.setSize(300, 250);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);

        jp = new JPanel(new FlowLayout());
        jp_R = new JPanel(new FlowLayout());
        jp_G = new JPanel(new FlowLayout());
        jp_B = new JPanel(new FlowLayout());
        jp_Label = new JPanel(new GridLayout(1,3,5,5));

        jp_R = new JPanel(new FlowLayout());
        jp_G = new JPanel(new FlowLayout());
        jp_B = new JPanel(new FlowLayout());

        jLabel_R = new JLabel("Red~~");
        jLabel_G = new JLabel("Green");
        jLabel_B = new JLabel("Blue~");
        jLabel_Rt = new JLabel("R: 255");
        jLabel_Gt = new JLabel("G: 255");
        jLabel_Bt = new JLabel("B: 255");
        jLabel_BG = new JLabel("#000000");
        jLabel_Rt.setFont(new Font(null, Font.PLAIN, 24));
        jLabel_Gt.setFont(new Font(null, Font.PLAIN, 24));
        jLabel_Bt.setFont(new Font(null, Font.PLAIN, 24));
        jLabel_BG.setFont(new Font(null, Font.PLAIN, 40));
        jLabel_BG.setBackground(new Color(255, 255, 255));
        jLabel_BG.setOpaque(true);

        jSlider_R = new JSlider(0,255,255);
        jSlider_G = new JSlider(0,255,255);
        jSlider_B = new JSlider(0,255,255);

        RGBListener listener = new RGBListener();

        jSlider_R.addChangeListener(listener);
        jSlider_G.addChangeListener(listener);
        jSlider_B.addChangeListener(listener);

        jp_R.add(jLabel_R);
        jp_R.add(jSlider_R);

        jp_G.add(jLabel_G);
        jp_G.add(jSlider_G);

        jp_B.add(jLabel_B);
        jp_B.add(jSlider_B);

        jp_Label.add(jLabel_Rt);
        jp_Label.add(jLabel_Gt);
        jp_Label.add(jLabel_Bt);

        jp.add(jp_R);
        jp.add(jp_G);
        jp.add(jp_B);
        jp.add(jLabel_BG);
        jp.add(jp_Label);

        jf.setContentPane(jp);
        jf.setVisible(true);

    }

    class RGBListener implements ChangeListener{

        @Override
        public void stateChanged(ChangeEvent changeEvent) {
            r = jSlider_R.getValue();
            g = jSlider_G.getValue();
            b = jSlider_B.getValue();
            jLabel_Rt.setText("R: " + r);
            jLabel_Gt.setText("G: " + g);
            jLabel_Bt.setText("B: " + b);
            jLabel_BG.setText(convertRGBToHex(r, g, b));
            jLabel_BG.setBackground(new Color(r, g, b));
            jLabel_BG.setForeground(new Color(255 - r, 255 - g, 255 - b));
        }
    }

    //**将rgb色彩值转成16进制代码**
    //**来自CSDN https://blog.csdn.net/Jason763/article/details/72844092 **

    public  String convertRGBToHex(int r, int g, int b) {
        String rFString, rSString, gFString, gSString, bFString, bSString, result;
        int red, green, blue;
        int r_red, r_green, r_blue;
        red = r / 16;
        r_red = r % 16;
        if (red == 10) rFString = "A";
        else if (red == 11) rFString = "B";
        else if (red == 12) rFString = "C";
        else if (red == 13) rFString = "D";
        else if (red == 14) rFString = "E";
        else if (red == 15) rFString = "F";
        else rFString = String.valueOf(red);

        if (r_red == 10) rSString = "A";
        else if (r_red == 11) rSString = "B";
        else if (r_red == 12) rSString = "C";
        else if (r_red == 13) rSString = "D";
        else if (r_red == 14) rSString = "E";
        else if (r_red == 15) rSString = "F";
        else rSString = String.valueOf(r_red);

        rFString = rFString + rSString;

        green = g / 16;
        r_green = g % 16;

        if (green == 10) gFString = "A";
        else if (green == 11) gFString = "B";
        else if (green == 12) gFString = "C";
        else if (green == 13) gFString = "D";
        else if (green == 14) gFString = "E";
        else if (green == 15) gFString = "F";
        else gFString = String.valueOf(green);

        if (r_green == 10) gSString = "A";
        else if (r_green == 11) gSString = "B";
        else if (r_green == 12) gSString = "C";
        else if (r_green == 13) gSString = "D";
        else if (r_green == 14) gSString = "E";
        else if (r_green == 15) gSString = "F";
        else gSString = String.valueOf(r_green);

        gFString = gFString + gSString;

        blue = b / 16;
        r_blue = b % 16;

        if (blue == 10) bFString = "A";
        else if (blue == 11) bFString = "B";
        else if (blue == 12) bFString = "C";
        else if (blue == 13) bFString = "D";
        else if (blue == 14) bFString = "E";
        else if (blue == 15) bFString = "F";
        else bFString = String.valueOf(blue);

        if (r_blue == 10) bSString = "A";
        else if (r_blue == 11) bSString = "B";
        else if (r_blue == 12) bSString = "C";
        else if (r_blue == 13) bSString = "D";
        else if (r_blue == 14) bSString = "E";
        else if (r_blue == 15) bSString = "F";
        else bSString = String.valueOf(r_blue);
        bFString = bFString + bSString;
        result = "#" + rFString + gFString + bFString;
        return result;

    }

    public static void main (String[] args){
        new RGBPalette();
    }
}
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注