【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“CheckAchromatopsiat.java”一文,供大家参考学习!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class CheckAchromatopsiat {
private final static int KIND=3,MIN=0,MAX=256;
private int tolerance=15; //允许的误差
private int standardRed,standardBlue,standardGreen; //机器产生的标准颜色
private final static String colorMsg[]={"红 最小","绿 最小","蓝 最小"};
protected JTextArea modelArea,userArea;
protected JScrollBar colorBar[];
protected JButton nextBtn,OKBtn;
protected JLabel color_min_Label[],maxLabel[];
protected JPanel areaPanel,barPanel[],btnPanel;
protected Container con;
protected JFrame mainJframe;
private HandleButtonClick handleBtn;
private HandleScrollbarChange handleScrollbar;
public CheckAchromatopsiat() {
mainJframe=new JFrame("色盲检测");
modelArea=new JTextArea();
modelArea.setColumns(12);
modelArea.setRows(8);
modelArea.setEditable(false);
userArea=new JTextArea();
userArea.setColumns(12);
userArea.setRows(8);
userArea.setEditable(false);
areaPanel=new JPanel();
FlowLayout tmpLayout=new FlowLayout();
tmpLayout.setHgap(20);
areaPanel.setLayout(tmpLayout);
areaPanel.add(modelArea);
areaPanel.add(userArea);
handleScrollbar=new HandleScrollbarChange();
color_min_Label=new JLabel[KIND];
maxLabel=new JLabel[KIND];
barPanel=new JPanel[KIND];
colorBar=new JScrollBar[KIND];
for(int i=0;i<3;i++){
color_min_Label[i]=new JLabel(colorMsg[i]);
maxLabel[i]=new JLabel("最大");
colorBar[i]=new JScrollBar(JScrollBar.HORIZONTAL,MAX-1,1,MIN,MAX);
colorBar[i].addAdjustmentListener(handleScrollbar);
barPanel[i]=new JPanel();
barPanel[i].setLayout(new BorderLayout());
barPanel[i].add(color_min_Label[i],BorderLayout.WEST);
barPanel[i].add(colorBar[i],BorderLayout.NORTH);
barPanel[i].add(maxLabel[i],BorderLayout.EAST);
}
handleBtn=new HandleButtonClick();
nextBtn=new JButton("开始");
nextBtn.addActionListener(handleBtn);
OKBtn=new JButton("确定");
OKBtn.setEnabled(false);
OKBtn.addActionListener(handleBtn);
btnPanel=new JPanel();
btnPanel.setLayout(new FlowLayout());
btnPanel.add(nextBtn);
btnPanel.add(OKBtn);
con=mainJframe.getContentPane();
con.setLayout(new BoxLayout(con,BoxLayout.Y_AXIS));
con.add(areaPanel);
con.add(barPanel[0]);
con.add(barPanel[1]);
con.add(barPanel[2]);
con.add(btnPanel);
mainJframe.setSize(400,350);
mainJframe.setVisible(true);
mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new CheckAchromatopsiat();
}
public class HandleButtonClick implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource()==nextBtn){
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 (!OKBtn.isEnabled()){
OKBtn.setEnabled(true);
nextBtn.setText("下一幅");
}
}else{
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(mainJframe,"颜色匹配正确");
}else{
JOptionPane.showMessageDialog(mainJframe,"颜色匹配错误");
}
}
}
}
public class HandleScrollbarChange implements AdjustmentListener{
public void adjustmentValueChanged(AdjustmentEvent e){
int red,green,blue;
red=colorBar[0].getValue();
green=colorBar[1].getValue();
blue=colorBar[2].getValue();
userArea.setBackground(new Color(red,green,blue));
}
}
}