⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abxcontrolpanel.java

📁 Java version of ABC/HR comparator v0.5. by schnofler. Runs on Sun JRE 1.5 or later
💻 JAVA
字号:
package abchr.gui;

import abchr.ABXTest;
import abchr.MersenneTwister;
import abchr.ABXTrial;
import abchr.audio.PlaybackThread;
import abchr.audio.Sample;
import guiutils.ComponentGroup;
import guiutils.LineLayout;
import jlfgr.GraphicsRepository;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ABXControlPanel extends JPanel {
	private static Random rand=new MersenneTwister();

	private PlaybackThread playbackThread=PlaybackThread.getInstance();

	private List listeners=new ArrayList(2);

	private ABXTest test;

	private JButton aButton;
	private JButton bButton;
	private JButton xButton;
	private JButton stopButton;

	private Sample aSample;
	private Sample bSample;
	private Sample xSample;

	private JRadioButton aRadioButton=new JRadioButton("X is A");
	private JRadioButton bRadioButton=new JRadioButton("X is B");
	private JRadioButton phantomButton=new JRadioButton();
	private JButton okButton=new JButton("Next Trial");

	private ComponentGroup group=new ComponentGroup();

	public ABXControlPanel() {
		super(new LineLayout(5,5,LineLayout.CENTER,LineLayout.TOP_ALIGN,false));

		Action action=new SimpleAction("Play A (q)",GraphicsRepository.getToolbarIcon("media/Play24.gif"),new ActionListener() {
			public void actionPerformed(ActionEvent e){playA();}
		});
		aButton=new JButton(action);
		aButton.setMnemonic('q');
		action=new SimpleAction("Play B (w)",GraphicsRepository.getToolbarIcon("media/Play24.gif"),new ActionListener() {
			public void actionPerformed(ActionEvent e){playB();}
		});
		bButton=new JButton(action);
		bButton.setMnemonic('w');
		action=new SimpleAction("Play X (e)",GraphicsRepository.getToolbarIcon("media/Play24.gif"),new ActionListener() {
			public void actionPerformed(ActionEvent e){playX();}
		});
		xButton=new JButton(action);
		xButton.setMnemonic('e');
		action=new SimpleAction("Stop (d)",GraphicsRepository.getToolbarIcon("media/Stop24.gif"),new ActionListener() {
			public void actionPerformed(ActionEvent e){stop();}
		});
		stopButton=new JButton(action);
		stopButton.setMnemonic('d');

		group.add(aButton);
		group.add(bButton);
		group.add(xButton);
		group.add(stopButton);

		aRadioButton.setMnemonic('a');
		bRadioButton.setMnemonic('s');
		ButtonGroup radioGroup=new ButtonGroup();
		radioGroup.add(aRadioButton);
		radioGroup.add(bRadioButton);
		radioGroup.add(phantomButton);
		group.add(aRadioButton);
		group.add(bRadioButton);
		ActionListener l=new ActionListener() {
			public void actionPerformed(ActionEvent e){sampleChosen();}
		};
		aRadioButton.addActionListener(l);
		bRadioButton.addActionListener(l);

		okButton.setEnabled(false);
		okButton.setMnemonic('n');
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){nextTrial();}
		});

		group.setEnabled(false);

		this.add(LineLayout.lineBreak());
		this.add(aButton);
		this.add(bButton);
		this.add(LineLayout.lineBreak());
		this.add(xButton);
		this.add(LineLayout.lineBreak());
		this.add(stopButton);
		this.add(LineLayout.lineBreak());
		this.add(aRadioButton);
		this.add(bRadioButton);
		this.add(LineLayout.lineBreak());
		this.add(okButton);

		InputMap im=this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		ActionMap am=this.getActionMap();
		im.put(KeyStroke.getKeyStroke('q'),"aButton");
		am.put("aButton",aButton.getAction());
		im.put(KeyStroke.getKeyStroke('w'),"bButton");
		am.put("bButton",bButton.getAction());
		im.put(KeyStroke.getKeyStroke('e'),"xButton");
		am.put("xButton",xButton.getAction());
		im.put(KeyStroke.getKeyStroke('d'),"stopButton");
		am.put("stopButton",stopButton.getAction());
		im.put(KeyStroke.getKeyStroke('a'),"aRadio");
		am.put("aRadio",new AbstractAction() {
			public void actionPerformed(ActionEvent e){aRadioButton.doClick();}
		});
		im.put(KeyStroke.getKeyStroke('s'),"bRadio");
		am.put("bRadio",new AbstractAction() {
			public void actionPerformed(ActionEvent e){bRadioButton.doClick();}
		});
		im.put(KeyStroke.getKeyStroke('n'),"okButton");
		am.put("okButton",new AbstractAction() {
			public void actionPerformed(ActionEvent e){okButton.doClick();}
		});
	}

	public void addListener(ABXPanelListener listener){listeners.add(listener);}
	public void removeListener(ABXPanelListener listener){listeners.remove(listener);}

	public void notifyListeners(ABXEvent event) {
		for(int i=0,n=listeners.size();i<n;i++) {
			((ABXPanelListener)listeners.get(i)).abxEvent(event);
		}
	}

	private void playA() {
		playbackThread.play(aSample);
	}

	private void playB() {
		playbackThread.play(bSample);
	}

	private void playX() {
		playbackThread.play(xSample);
	}

	private void stop(){playbackThread.stopPlayback();}

	private void sampleChosen(){okButton.setEnabled(true);}

	private void nextTrial() {
		playbackThread.stopPlayback();
		notifyListeners(new TrialAnsweredEvent(this,xSample==aSample?ABXSampleType.A_SAMPLE:ABXSampleType.B_SAMPLE,aRadioButton.isSelected()?ABXSampleType.A_SAMPLE:ABXSampleType.B_SAMPLE));
		BoundedRangeModel rangeModel=playbackThread.getRangeModel();
		test.addTrial(new ABXTrial(xSample==aSample ^ bRadioButton.isSelected(),System.currentTimeMillis(),rangeModel.getValue(),rangeModel.getValue()+rangeModel.getExtent()));
		newTest();
	}

	private void newTest() {
		if(test==null){return;}
		okButton.setEnabled(false);
		phantomButton.setSelected(true);
		/*this.aSample=test.getASample();
		this.bSample=test.getBSample();*/
		xSample=rand.nextInt()<0?aSample:bSample;
		aButton.requestFocus();
	}

	public void setTest(ABXTest test) {
		setTest(test,test!=null?test.getASample():null,test!=null?test.getBSample():null);
	}

	public void setTest(ABXTest test,Sample aSample,Sample bSample) {
		this.test=test;
		this.aSample=aSample;
		this.bSample=bSample;
		if(test!=null) {
			group.setEnabled(true);
			newTest();
		} else {
			group.setEnabled(false);
		}
	}

	public ABXTest getTest(){return test;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -