setreader.java

来自「简单的多字符java动画,方便理解Multi-threaded 的概念.」· Java 代码 · 共 258 行

JAVA
258
字号
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;

public class SetReader
{
	public static final int BLACK_L = 0; // black L Symbol
	public static final int BLACK_T = 1; // black T Symbol
	public static final int WHITE_L = 2; // white L Symbol
	public static final int WHITE_T = 3; // white T Symbol
	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
	private Properties setProps;
	public static final int CANVAS_WIDTH = 700; /* Canvas Width */
	public static final int CANVAS_HEIGHT = 500; /* Canvas Height */
	public static final int MAX_RANDOM_X = 10; /* X Movement */
	public static final int MAX_RANDOM_Y = 10; /* Y Movement */
	Vector<TrialInfo> vTrials = null;
	//SetInfo currentSet = null;
	
    
	public SetReader(File inFile)
	{
		loadProperties(inFile);
		//listProperties(inFile);
		try
		{
			setupSetInfo();
		} catch (IOException e)
		{
			e.printStackTrace();
		} 

	} // end of LifeProperties

    
    
	private void loadProperties(File inFile)
	// load the properties from the PROP_FNM file
	{
		setProps = new Properties();
		try
		{
			FileInputStream in = new FileInputStream(inFile);
			setProps.load(in);
			in.close();
			System.out.println("Loaded properties from " + inFile.getName());
		} catch (IOException e)
		{
			System.out.println("Could not load properties from " + inFile.getName());
		}
	} // end of loadProperties()

	private void listProperties(File inFile)
	{
		if (setProps.isEmpty())
			System.out.println("No properties in " + inFile.getName());
		else
		{
			System.out.println("Properties in " + inFile.getName());
			setProps.list(System.out);
		}
	} // end of listProperties()

	/* Setup Set Infos */
	private void setupSetInfo()throws IOException
	{
		/* Setup All Trial Infos */
		vTrials = new Vector<TrialInfo>();
		
		TrialInfo
		 tInfo = null;
		tInfo = setupTrialInfo(setProps.getProperty("T1"),"T1");
		vTrials.add(tInfo);
     	tInfo = setupTrialInfo(setProps.getProperty("T2"),"T2");
		vTrials.add(tInfo);
		tInfo = setupTrialInfo(setProps.getProperty("T3"),"T3");
		vTrials.add(tInfo);
		tInfo = setupTrialInfo(setProps.getProperty("T4"),"T4");
		vTrials.add(tInfo);
		tInfo = setupTrialInfo(setProps.getProperty("T5"),"T5");
		vTrials.add(tInfo);
		
        
        
		return;
	}//End setupSetInfo
	
	public Vector<TrialInfo> getTrials()
	{
		return vTrials;
	}// End getTrials
	
	/* Setup TrialInfo Infos */
	private TrialInfo setupTrialInfo(String strT1,String strTrialNum) throws IOException
	{

		
		int nCount = 0;
		Image crossImg = null, img = null;

		/* Setup Cross Image */
		crossImg = ImageIO.read(new File(setProps.getProperty("Cross")));
		
		/* Set Trials Infos */
		int[] testInfo = null;
		Vector<MovingShape> vMVShapes = new Vector<MovingShape>();
		MovingShape mvShape = null;
		CrossShape crossShape = null;
		TrialInfo tInfo = null;

		testInfo = parseTrialInfo(strT1);

		/* Initialize Black L Image */
		for (nCount = 0; nCount < testInfo[0]; nCount++)
		{
			img = ImageIO.read(new File(setProps.getProperty("BlackL")));
			mvShape = new MovingShape(img);
			// Setup Symbol Type
			mvShape.setType(BLACK_L);
			vMVShapes.add(mvShape);
		}// End for

		/* Initialize Black T Image */
		for (nCount = 0; nCount < testInfo[1]; nCount++)
		{
			img = ImageIO.read(new File(setProps.getProperty("BlackT")));
			mvShape = new MovingShape(img);
			// Setup Symbol Type
			mvShape.setType(BLACK_T);
			vMVShapes.add(mvShape);
		}// End for

		/* Initialize White L Image */
		for (nCount = 0; nCount < testInfo[2]; nCount++)
		{
			img = ImageIO.read(new File(setProps.getProperty("WhiteL")));
			mvShape = new MovingShape(img);
			// Setup Symbol Type
			mvShape.setType(WHITE_L);
			vMVShapes.add(mvShape);
		}// End for

		/* Initialize White T Image */
		for (nCount = 0; nCount < testInfo[3]; nCount++)
		{
			img = ImageIO.read(new File(setProps.getProperty("WhiteT")));
			mvShape = new MovingShape(img);
			// Setup Symbol Type
			mvShape.setType(WHITE_T);
			vMVShapes.add(mvShape);
		}// End for

		/* Setup Each Shape Start Locations */
		setupEachShape(vMVShapes);

		/* Create A Trial Info */
		tInfo = new TrialInfo(vMVShapes, TOTAL_TRIAL_TIME,strTrialNum);

		/* Set CrossTrial Flag */
		if (testInfo[4] != 0)
		{

			/* Cross Trial */
			int nCrossDeltaX = CANVAS_WIDTH
					/ (int) (CROSS_INTERVAL / CANVAS_UPDATE_INTERVAL);
			int nCrossDeltaY = 0;
			crossShape = new CrossShape(CANVAS_WIDTH - img.getWidth(null),
					CANVAS_HEIGHT / 2, crossImg, nCrossDeltaX, nCrossDeltaY);
			tInfo.setCrossExist(true);
			tInfo.setCrossShape(crossShape);
			tInfo.setCrossStartTime(CROSS_START_TIME);
		}// End if

		return tInfo;

	}// End setupExperiment Method

	/* Setup Each Moving Shape */
	private void setupEachShape(Vector<MovingShape> vMVShapes)
	{
		Iterator<MovingShape> iterShape = vMVShapes.iterator();
		Random rand = new Random();
		int x = 0, y = 0, deltaX = 0, deltaY = 0;
		MovingShape mvShape = null;
		while (iterShape.hasNext())
		{
			mvShape = (MovingShape) iterShape.next();
			// setup initial deltaX and deltaY
			deltaX = 1 + rand.nextInt(MAX_RANDOM_X);
			deltaY = 1 + rand.nextInt(MAX_RANDOM_Y);
			mvShape.setDeltaX(deltaX);
			mvShape.setDeltaY(deltaY);
			// initial Postion of the Shape
			x = rand.nextInt(CANVAS_WIDTH - mvShape.getImage().getWidth(null));
			y = rand.nextInt(CANVAS_HEIGHT - mvShape.getImage().getHeight(null));
			mvShape.setX(x);
			mvShape.setY(y);
		}// End while
		return;

	}// End setupEachShape

	private int[] parseTrialInfo(String strTrialInfo)
	{
		String[] strInfos = strTrialInfo.split(" ");

        
		int[] numInfos = new int[strInfos.length];

		for (int i = 0; i < numInfos.length; i++)
		{
			
			numInfos[i] = Integer.parseInt(strInfos[i]);
			
		}// End for

            
        
		return numInfos;

	}// End setTrialInfo Method

    
    public int getSetNum()
    {
        int
            setNum = -1;
        setNum = Integer.parseInt(setProps.getProperty("SetNumber"));
        return setNum;
    }//End getSetNum
    
	public static void main(String[] args)
	{
        String
            strFileName = "Set1.txt",
            strFilePath = "Input_Files/";
        
        File
            inFile = new File(strFilePath + strFileName);
                      
		new SetReader(inFile);
             
        
		return;
	}// End main Method

}// End SetReader Class Definition

⌨️ 快捷键说明

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