bouncingtest.java
来自「简单的多字符java动画,方便理解Multi-threaded 的概念.」· Java 代码 · 共 332 行
JAVA
332 行
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class BouncingTest extends JFrame
{
private static final long serialVersionUID = 1L;
public static final int CANVAS_WIDTH = 700; /* Canvas Width */
public static final int CANVAS_HEIGHT = 500; /* Canvas Height */
public static final int CANVAS_UPDATE_INTERVAL = 100; /*
* Canvas Update
* Interval
*/
public static final long CROSS_START_TIME = 5000; /* Cross Sign Start Time */
public static final long CROSS_INTERVAL = 5000; /* Cross END TIME */
public static final long TOTAL_TRIAL_TIME = 15000; /* Total Trial Time */
float
SINGLE_LINE_SPACING = 1, // Single Line Spacing
ONE_AND_HALF_LINE_SPACING = (float) 1.50, // One and half Line spacing
DOUBLE_LINE_SPACING = 2 ; // Double Line spacing
BouncingPanel panel = null; /* Bouncing Shapes Panel */
JButton startButton = null; /* Start Button */
JLabel countLabel = null; /* Set Group Label */
JPanel controlPanel = null; /* Control Panel */
int nCurrent_Trial = 0, nNum_Of_Trials = 0;
SetReader
setProps = null;
TrialInfo currentTrial = null;
FixationShape fixShape = null;
LogFile outputFile = null; /* Output File */
ScreenInstru
startSetInstru = null,
endTrialInstru = null;
Color
instruColor = Color.CYAN; // Instruction Color
Font
instruFont = new Font("Serif", Font.BOLD, 22);
int
nStartX = 100,
nStartY = 100;
public BouncingTest(String sTitle, String ID,SetReader setInfo)
{
// Get Application Title
super(sTitle);
String outputFileName = ID + ".dat";
/* Create OutputFile Writer */
outputFile = new LogFile(outputFileName);
setProps = setInfo;
/* Create Instrucitons */
setupStartSetInstru(nStartX,nStartY,instruColor,instruFont);
setupEndTrialInstru(nStartX,nStartY,instruColor,instruFont);
/* Write Current ID and Type */
writeHeader(ID);
try
{
/* Setup Fixation Shape */
Image img = ImageIO.read(new File("Fixation.JPG"));
fixShape = new FixationShape(img);
fixShape.setX(CANVAS_WIDTH / 2);
fixShape.setY(CANVAS_HEIGHT / 2);
}
catch (IOException e)
{
e.printStackTrace();
}
/* Setup GUI */
setupGUI();
return;
}// End BouncingTest Constructor
// Write Header
public void writeHeader(String ID)
{
System.out.println("Write Header Function.");
/* Write ID, Date, Time and Set Number */
outputFile.write("ID: " + ID + "\t\t");
outputFile.write("Time: " + easyDateFormat("dd-MM-yyyy HH:mm") +"\t\t");
outputFile.write("Set Number: " + setProps.getSetNum());
outputFile.write("\n");
outputFile.write("Trial Number \t\t");
outputFile.write("Black Hits \t\t");
outputFile.write("White Hits \n");
}// End writeHeader Method
public String easyDateFormat(String format)
{
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
/* Setup Graphic User Interface */
public void setupGUI()
{
Container content = this.getContentPane();
startButton = new JButton("Next");
startButton.setEnabled(true);
startButton.addActionListener(new ButtonListener());
/* Setup Drawing Panel */
panel = new BouncingPanel(CANVAS_WIDTH, CANVAS_HEIGHT, fixShape,
startButton,outputFile,startSetInstru,endTrialInstru);
content.add(panel, BorderLayout.CENTER);
/* Setup Control panel */
controlPanel = new JPanel();
controlPanel
.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS));
countLabel = new JLabel(nCurrent_Trial + " / " + nNum_Of_Trials
+ " trials ");
controlPanel.add(countLabel);
controlPanel.add(startButton);
/* Add some space between two components */
controlPanel.add(Box.createRigidArea(new Dimension(0, 10)));
content.add(controlPanel, BorderLayout.EAST);
return;
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
if (nCurrent_Trial < setProps.getTrials().size())
{
/* Get Current Trial */
currentTrial = setProps.getTrials().elementAt(
nCurrent_Trial);
/* Start Trial */
panel.start(currentTrial);
/* Move to Next Trial */
nCurrent_Trial++;
/* Update Trial Label */
countLabel.setText(nCurrent_Trial + " / "
+ setProps.getTrials().size() + " trials ");
}// Else End of Trial Reached Disable StartButton
else
{
startButton.setEnabled(false);
}// End if
}// End if
}// End actionPerformed
}// End Inner Class ButtonListener
public void setupEndTrialInstru(int startX, int startY, Color c, Font font)
{
int
nStrWidth = 0;
Vector<Instruction> vInstruc = new Vector<Instruction>();
int x=0, y = startY, nFontHeight = 0;
String str = null;
Instruction instru = null;
FontMetrics fontMetrics = getFontMetrics(font);
nFontHeight = fontMetrics.getHeight();
y += (nFontHeight * DOUBLE_LINE_SPACING);
str = "Current Trial is Finished: ";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * ONE_AND_HALF_LINE_SPACING);
str = "Click Next Button to continue next Trial";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
/* Create End Trial Screen */
endTrialInstru = new ScreenInstru(vInstruc);
}//End setupPracticeFeedback
public void setupStartSetInstru(int startX, int startY, Color c, Font font)
{
int
nStrWidth = 0;
Vector<Instruction> vInstruc = new Vector<Instruction>();
/*
* For this task you need to focus on the square in the middle of the screen
* and silently count the number of times
* the black letters bounce off the sides of the screen.
* You should only count the black letters.
* At the end you will be asked how many times
* the black letters bounced off the sides of the screens.
*/
int x=0, y = startY, nFontHeight = 0;
String str = null;
Instruction instru = null;
FontMetrics fontMetrics = getFontMetrics(font);
nFontHeight = fontMetrics.getHeight();
//y += (nFontHeight * DOUBLE_LINE_SPACING);
str = "Experiment Starts : ";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * ONE_AND_HALF_LINE_SPACING);
str = "For this task you need to focus on the square in the middle of the screen";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * SINGLE_LINE_SPACING);
str = "and silently count the number of times";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * SINGLE_LINE_SPACING);
str = "the black letters touch the sides of the screen.";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * SINGLE_LINE_SPACING);
str = "You should only count the black letters.";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * SINGLE_LINE_SPACING);
str = "At the end you will be asked how many times";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * SINGLE_LINE_SPACING);
str = "the black letters touch the sides of the screen.";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
y += (nFontHeight * ONE_AND_HALF_LINE_SPACING);
str = "Click Next Button to continue first Trial";
nStrWidth = fontMetrics.stringWidth(str);
x = (CANVAS_WIDTH - nStrWidth)/2;
instru = new Instruction(font, str, c, x, y);
vInstruc.add(instru);
/* Create End Trial Screen */
startSetInstru = new ScreenInstru(vInstruc);
}//End setupPracticeFeedback
} // end of class BouncingCircles
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?