📄 drawingpanel.java
字号:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel implements Runnable ,KeyListener
{
/**/
private static final long serialVersionUID = 1L;
Ellipse2D.Double
ball = null; // bouncing ball
int
nCurrentTrial, // trial counter
nResponse,
nR, // radius of ball
nDx, // x incremental
nDy, // y incremental
nDeltaDis =0, // Incremental Distance
nDeltaR =0; // Incremental Radius
Point
pCurrent,
pZero,
pCenter;
Thread
thread = null;
BouncingTrial
oTrial = null;
long
lResponseTime = 0;
int
nCanvasWidth, // Canvas Width
nCanvasHeight; // Canvas Height
Color
bgColor; // Background Color
boolean
isDone, // Check the Trial Has been completed
isResponseMade,
isTurningPointPassed;
StopWatch
watch = null; // Watch
Vector<BouncingTrial>
expTrials= null; //Experimental Trials
LogFile
outputFile = null;
JButton
startButton = null;
public DrawingPanel(JButton startButton, // StartButton
LogFile outputFile, // Output FileName String
Vector<BouncingTrial> expTrials,// Experiment Trials
int nCanvasWidth, // Canvas Width
int nCanvasHeight, // Canvas Height
Color bgColor, // Background Color
int nDeltaR) // Incremental Radius
{
this.nDeltaR = nDeltaR;
this.nCanvasWidth = nCanvasWidth;
this.nCanvasHeight = nCanvasHeight;
this.bgColor = bgColor;
this.expTrials = expTrials;
this.outputFile = outputFile;
this.startButton = startButton;
// Create A watch
watch = new StopWatch();
// Add KeyListener
addKeyListener(this);
}//End constructor
public void start()
{
//Initialize trial counter to zero
nCurrentTrial = 0;
//Disable startButton
if(startButton.isEnabled())
{
startButton.setEnabled(false);
}
// start the thread to continuous trials
if(thread == null)
{
thread = new Thread(this);
thread.start();
this.requestFocusInWindow();
}
return;
}// End start
/**
* Continuously Running the Trials
*/
public void run()
{
Iterator<BouncingTrial>
iter = expTrials.iterator();
nCurrentTrial=0;
//for each trial
while(iter.hasNext())
{
this.oTrial= iter.next();
//intialize the trial varaible
//and initialize time and response
iniTrial();
//start the trial
runTrial();
//wait a 500ms
try
{
// frame during
Thread.sleep(500);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//record the trial and response time ****************Needs implementation
writeTrialOutput();
//increment trial count++;
nCurrentTrial++;
}//End while
// set thread to null
thread = null;
// Enable startButton Again
if(!startButton.isEnabled())
{
startButton.setEnabled(true);
}
return;
}//End run Method
public void writeTrialOutput()
{
//Write to File
this.outputFile.write( (nCurrentTrial+1) +"\t");
this.outputFile.write(oTrial.getReponse()+"\t");
this.outputFile.write(oTrial.getResponseTime());
this.outputFile.write("\n");
return;
}//End writeTrialOutput
/**
* Initialize varaibles
* @param oTrial Current TrialInfo
*/
public void iniTrial()
{
// Initialize variables
this.nDeltaDis = oTrial.getIncrementDis(); // Incremental Distance
this.nDx = oTrial.getDx(); // Incremental Dx
this.nDy = oTrial.getDy(); // Incremental Dy
this.nR = oTrial.getIniRadius(); // Initial Radius
this.bgColor = oTrial.getBgColor(); // Current Background Color
this.pZero = new Point(oTrial.getP1());
this.pCenter = new Point( oTrial.getP2());
this.ball = new Ellipse2D.Double( pZero.x-nR, // upper left corner x of ball
pZero.y-nR, // upper left corner y of ball
nR*2,nR*2); // width, height of ball
// current drawing point is same as pZero
pCurrent = new Point(pZero);
// set turning point is not passed
isTurningPointPassed = false;
isResponseMade = false;
// set initial response time
this.lResponseTime = 0;
// set initial response key
this.nResponse = -1;
this.isDone = false;
return;
}//End iniTrial Method
public void runTrial()
{
if(oTrial == null)
{
System.out.println("oTrial in runTrial is null");
}
while(!isDone)
{
try
{
// frame during
Thread.sleep(oTrial.getFrameDuration());
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// draw the current ball
repaint();
// Check Turning Point has reached
if(pCurrent.y > pCenter.y )
{
if(!isTurningPointPassed)
{
//if just reached, record starting time
watch.start();
// set flag to just pass turning point
isTurningPointPassed = true;
//System.out.println("Turning Point Just Passed:");
}
}
// calculate new postion of pCurrent;
calPos();
// if pCurrent is out of boundary
if(pCurrent.y > oTrial.getBottomY())
{
//set flag to finish the trial when the last frame is drawn
isDone = true;
}
else//
{
// clear the ball at previous position
// reshape the ball with pCurrent, and new Radius
nR += nDeltaR;
if(nR> oTrial.getEndRadius())
{
nR = oTrial.getEndRadius();
}
ball = new Ellipse2D.Double(pCurrent.x-nR, // upper left corner x of ball
pCurrent.y-nR, // upper left corner y of ball
nR*2,nR*2); // width, height of ball
}
}//End while
return;
}//End runEachTrial Method
// calculate the postion of pCurrent
private void calPos()
{
// if pCurrent is between pZero and pCenter or If current is straight trial
if(pCurrent.y < pCenter.y )
{
// no turning point, straight to the center
pCurrent.y += nDeltaDis;
}
else
{
// turning point
if(oTrial.getTrialType() == BouncingTrial.STRAIGHT_TRIAL)
{
// at turning point, straight to the center
pCurrent.y += nDeltaDis;
}
else
{
pCurrent.x += nDx;
pCurrent.y += nDy;
}
}// End if(pCurrent.y < pCenter.y )
return;
}//End calPos
/** Draw the ball at its current position. **/
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
// Antialiasing for smooth surfaces.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(this.bgColor);
g2.fillRect(0, 0,this.nCanvasWidth, this.nCanvasHeight);
if(ball!=null)
{
g2.setColor(oTrial.getBallColor());
g2.fill(ball);
// Now draw the ball.
g2.draw (ball);
}
} // paintComponent
public void keyPressed(KeyEvent event)
{
int keyCode = event.getKeyCode(); // Get KeyCode Pressed
int location = event.getKeyLocation(); // Get Key Location from the Keyboard
if(keyCode == KeyEvent.VK_SHIFT)
{
if(!isResponseMade && isTurningPointPassed)
{
if (location == KeyEvent.KEY_LOCATION_LEFT)
{
// Left Shift Key is Pressed
//System.out.println("Left Shift Pressed");
this.nResponse = BouncingTrial.LEFT_SHIFT_RESP;
}
else if (location == KeyEvent.KEY_LOCATION_RIGHT)
{
// Right Shift Key is Pressed
//System.out.println("Right Shift Pressed");
this.nResponse = BouncingTrial.RIGHT_SHIFT_RESP;
}
// Check Trial and Type and Response
watch.end(); // capture end time
//Record the Response Time
lResponseTime = watch.elapsedMillis();
// Check Sign
if( oTrial.getTrialType() == BouncingTrial.LEFT_TRIAL &&
this.nResponse == BouncingTrial.RIGHT_SHIFT_RESP)
{
lResponseTime = -lResponseTime;
}
if( oTrial.getTrialType() == BouncingTrial.RIGHT_TRIAL &&
this.nResponse == BouncingTrial.LEFT_SHIFT_RESP)
{
lResponseTime = -lResponseTime;
}
//Set response time and response key in oTrial
oTrial.setResponse(nResponse);
oTrial.setResponseTime(lResponseTime);
// set isResponseMade true
isResponseMade = true;
}//End if(!isResponseMade)
}
return;
}// End keyPressed Method
public void keyReleased(KeyEvent e)
{
}//
public void keyTyped(KeyEvent e)
{
}
}//End DrawingPanel Class Defintion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -