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

📄 bouncingpanel.java

📁 简单的多字符java动画,方便理解Multi-threaded 的概念.
💻 JAVA
字号:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;

/*
 * Created on 12/04/2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author user
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class BouncingPanel extends JPanel implements Runnable
{

    private static final long serialVersionUID = 1L;
   
   
    int 
    	nCanvasWidth = 0, /* Drawing Canvas Width */
    	nCanvasHeight = 0, /* Drawing Canvas Height */
    	nDrawUpdateInterval = 0, /* Drawing Update Interval */
    	nCrossSignInterval = 0; /* Cross Sign Interval */

    private 
    TrialInfo tInfo = null; /* Current Trial Info */
    Thread animationThread = null;
    Image offScreenImage = null;
    Graphics2D offScreenGraphics2D = null;
    StopWatch watch = null;
    Color
    	bgColor = new Color(127,127,127);
    boolean blnStop = true;
 
    FixationShape
        fixShape = null;
    
    JButton
        nextButton = null;
    Vector<TrialInfo>
        vSetInfo = null;        /* A Vector of TrialInfos in current Set */
    
    LogFile
    	outFile = null;
    
    ScreenInstru
		startSetInstru = null,		/* Start Set Instruction */
		endTrialInstru = null;		/* Start Set Instruction */
    
    boolean
    	isSetStart = false;
    
    public BouncingPanel(   int canvasWidth,    	/* Canvas Width */
                            int canvasHeight,   	/* Canvas Height */
                            FixationShape fShape,   /* Fixation Shape */
                            JButton nButton,        /* Next Button */
                            LogFile logFile,		/* Log File */
                            ScreenInstru sSetInstru,/* Start Set Instruction */
                            ScreenInstru eTrialInstru) /* End Trial Instruction */
    {	
        nCanvasWidth = canvasWidth;
        nCanvasHeight = canvasHeight;
        fixShape = fShape;
        nextButton = nButton;
        outFile = logFile;
        startSetInstru = sSetInstru;
        endTrialInstru = eTrialInstru;
        
       this.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
    }

    public boolean isTrialFinished()
    {
        return blnStop;
    }// End isTrialFinished
    
    public void start(TrialInfo tInfo)
    {
        blnStop = false;
        this.tInfo = tInfo;
        nextButton.setEnabled(false);
        animationThread = new Thread(this);
        animationThread.start();
        /* Create A Watch */
        watch = new StopWatch();
        watch.start();
        return;
    }

    private void pause(long milliseconds)
    {
        if (Thread.currentThread() != null)
        {
            try
            {
                Thread.sleep(milliseconds);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        return;
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        Rectangle r = this.getBounds();
        int width = r.width, height = r.height;

        if (width <= 0 || height <= 0)
        {
            return;
        }

        // Create an offscreen image
        if (offScreenImage == null)
        {
            offScreenImage = this.createImage(width, height);
            offScreenGraphics2D = (Graphics2D) offScreenImage.getGraphics();
        }

        //Clear Previous Drawing
        clearCanvas();
        
        if(!isSetStart)
        {
        	drawCurrentScreen(startSetInstru);
        	isSetStart = !isSetStart;
        }
        
        //Drawing Cross Angle
        if(!blnStop)
        {
             //Draw Fixation Image
            fixShape.drawShape(offScreenGraphics2D,this);
            
            MovingShape mvShape = null;
            Iterator<MovingShape> iter = tInfo.getBouncingShapes().iterator();
            
            //Draw All Shapes offScreen
            while (iter.hasNext())
            {
                mvShape = iter.next();//Get Current Moving Shape
                mvShape.drawShape(offScreenGraphics2D, this);//Draw Current
                                                             // Shape
                mvShape.move(this.nCanvasWidth, this.nCanvasHeight, this);//Move
                                                                          // to
                                                                          // new
                                                                          // Position
            }//End while

            watch.end();
            long currentTime = watch.elapsedMillis();

            // Check Current Trial is Cross Trial or Not
            if (tInfo.isCrossExist())
            {

                if (currentTime >= tInfo.getCrossStartTime())
                {
                    CrossShape cShape = tInfo.getCrossShape();
                    cShape.drawShape(offScreenGraphics2D, this);
                    cShape.move(nCanvasWidth, nCanvasHeight, this);

                }
            }
            // Check Current Time Has Passed During
            if (currentTime >= tInfo.getTrialInterval())
            {
                blnStop = true;
                /* Enable NextButton */
                nextButton.setEnabled(true);
                // output WHITE HITs and BLACK HITs to LogFile
                outFile.write(tInfo.getTrialNumStr()+":\t\t");
                outFile.write(MovingShape.nBHit + "\t\t");
                outFile.write(MovingShape.nWHit + "\n");
               
                // Reinitialize Black and White Hits count
                MovingShape.nBHit = 0;
                MovingShape.nWHit = 0;
                
                
                try
				{
					Thread.sleep(1000);
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                
                // Draw End Trial Instruction
                drawCurrentScreen(endTrialInstru);
            }

        }// End if (blnStart)

        // Draw offScreen
        g.drawImage(offScreenImage, 0, 0, this);
        return;
    }

    private void clearCanvas()
	{
	    /* Draw the image offscreen */
		if(offScreenImage != null)
		{
			Rectangle
				bounds = getBounds();
			offScreenGraphics2D.setColor(bgColor);
			offScreenGraphics2D.fillRect(0,0,bounds.width,bounds.height);
		}
	}//End clearCanvas Method
    
    public void drawCurrentScreen(ScreenInstru screenInstru)
    {

        // Clear current Drawing Screen
        clearCanvas();

        Iterator<Instruction> iter = screenInstru.getScreenInstr().iterator();
        Instruction instru = null;

        while (iter.hasNext())
        {
            instru = iter.next();
            offScreenGraphics2D.setColor(instru.getColor());
            offScreenGraphics2D.setFont(instru.getFont());
            offScreenGraphics2D.drawString(instru.getInstruction(),instru.getX(), instru.getY());
       }//End while

    }// End drawCurrentScreen Method
    
    public void run()
    {
        // TODO Auto-generated method stub
        while (!blnStop)
        {
            repaint();
            pause(BouncingTest.CANVAS_UPDATE_INTERVAL);
        }//End while
        /* End of Trial */
        animationThread = null;
    }//End run

}//end class Definition

⌨️ 快捷键说明

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