CheckAchromatopsiat色盲检测程序

package com.shrimpking.t1;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Shrimpking
 * @create 2024/11/1 21:29
 */
public class CheckAchromatopsiat
{
    private final static int KIND = 3; //三种颜色
    private final static int MIN = 0; //最小值
    private final static int MAX = 256; //最大值
    private int tolerance = 20; //允许的误差 ,越小越难
    private int standardRed; //标准色
    private int standardBlue; //标准色
    private int standardGreen; //标准色
    private final static String[] COLOR_MSG = {"红 最小","绿 最小","蓝 最小"};
    protected JTextArea modelArea; //标准色彩
    protected JTextArea userArea; // 用户混合色彩
    protected JScrollBar[] colorBar; //滑块
    protected JButton btnNext;
    protected JButton btnOk;
    protected JLabel[] colorMinLabel; //
    protected JLabel[] colorMaxLabel; //
    protected JPanel panelArea;  //容器
    protected JPanel[] panelBar;
    protected JPanel panelBtn;
    protected Container container;
    protected JFrame frame;
    private JLabel labelTip;
    private HandleButtonClick handleBtn;
    private HandleScrollbarChange handleScrollbarChange;

    public CheckAchromatopsiat(){
        handleBtn = new HandleButtonClick();
        handleScrollbarChange = new HandleScrollbarChange();

        frame = new JFrame("色盲检测");
        container = frame.getContentPane();
        //两个只读的文本区域显示颜色
        modelArea = new JTextArea();
        modelArea.setColumns(30);
        modelArea.setRows(32);
        modelArea.setEnabled(false);
        userArea = new JTextArea();
        userArea.setColumns(30);
        userArea.setRows(32);
        userArea.setEnabled(false);
        panelArea = new JPanel();
        FlowLayout tempLayout = new FlowLayout();
        tempLayout.setHgap(40); //垂直间距
        panelArea.setLayout(tempLayout);
        panelArea.add(modelArea);
        panelArea.add(userArea);

        //创建滚动条和提示标签
        colorMinLabel = new JLabel[KIND];
        colorMaxLabel = new JLabel[KIND];
        panelBar = new JPanel[KIND];
        colorBar = new JScrollBar[KIND];
        for (int i = 0; i < KIND; i++)
        {
            colorMinLabel[i] = new JLabel(COLOR_MSG[i]);
            colorMaxLabel[i] = new JLabel("最大");
            colorBar[i] = new JScrollBar(JScrollBar.HORIZONTAL,MAX-1,1,MIN,MAX);
            colorBar[i].addAdjustmentListener(handleScrollbarChange);
            colorBar[i].setEnabled(false); //不可用
            panelBar[i] = new JPanel();
            panelBar[i].setLayout(new BorderLayout());
            panelBar[i].add(colorMinLabel[i],BorderLayout.WEST); //靠左
            panelBar[i].add(colorBar[i],BorderLayout.CENTER); //居中
            panelBar[i].add(colorMaxLabel[i],BorderLayout.EAST); //靠右
        }

        //创建两个按钮
        btnNext = new JButton("开始");
        btnOk = new JButton("确定");
        btnOk.setEnabled(false);
        btnNext.addActionListener(handleBtn);
        btnOk.addActionListener(handleBtn);
        labelTip = new JLabel("RGB()");
        panelBtn = new JPanel();
        panelBtn.setLayout(new FlowLayout());
        panelBtn.add(btnNext);
        panelBtn.add(btnOk);
        panelBtn.add(labelTip);

        container.setLayout(new BoxLayout(container,BoxLayout.Y_AXIS)); //垂直布局
        container.add(panelArea);
        container.add(panelBar[0]);
        container.add(panelBar[1]);
        container.add(panelBar[2]);
        container.add(panelBtn);

        frame.setSize(800,700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public class HandleButtonClick implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == btnNext){
                //随机产生标准色彩
                standardRed = (int)(Math.random() * 1000) % MAX;
                standardGreen = (int) (Math.random() * 1000) % MAX;
                standardBlue = (int) (Math.random() * 1000) % MAX;
                for (int i = 0; i < KIND; i++)
                {
                    colorBar[i].setValue(MAX - 1); //滑块设定值
                }
                //显示标准色
                modelArea.setBackground(new Color(standardRed,standardGreen,standardBlue));
                //显示用户的颜色为白色
                userArea.setBackground(new Color(MAX-1,MAX-1,MAX-1));
                if (!btnOk.isEnabled()){
                    //确定按钮不可用时
                    btnOk.setEnabled(true);
                    btnNext.setText("下一幅");
                    colorBar[0].setEnabled(true);
                    colorBar[1].setEnabled(true);
                    colorBar[2].setEnabled(true);
                }
            }else if (e.getSource() == btnOk){
                //确定按钮
                boolean correct = true;
                if (Math.abs(colorBar[0].getValue() - standardRed) > tolerance){
                    correct = false;
                }
                if (Math.abs(colorBar[1].getValue() - standardGreen) > tolerance){
                    correct = false;
                }
                if (Math.abs(colorBar[2].getValue() - standardBlue) > tolerance){
                    correct = false;
                }
                if (correct){
                    JOptionPane.showMessageDialog(frame,"颜色匹配正确");
                }else {
                    JOptionPane.showMessageDialog(frame,"颜色匹配错误");
                }
                btnOk.setEnabled(false);
                colorBar[0].setEnabled(false);
                colorBar[1].setEnabled(false);
                colorBar[2].setEnabled(false);
                labelTip.setText("RGB()");
            }
        }
    }

    public class HandleScrollbarChange implements AdjustmentListener{

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            int red = colorBar[0].getValue();
            int green = colorBar[1].getValue();
            int blue = colorBar[2].getValue();
            //提示用户
            String msg = String.format("RGB1(%d,%d,%d)----RGB2(%d,%d,%d)"
                    ,standardRed,standardGreen,standardBlue
                    ,red,green,blue);
            labelTip.setText(msg);
            //设置用户颜色
            userArea.setBackground(new Color(red,green,blue));
        }
    }

    public static void main(String[] args)
    {
        new CheckAchromatopsiat();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值