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

📄 startframe.java

📁 多线程活动球java 程序,便于理解Multi-Threaded 的功能.
💻 JAVA
字号:
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;

public class StartFrame extends JFrame implements ActionListener
{

    private static final long serialVersionUID = 1L;

    String[] fileNameStrings = null;

    JComboBox fileNameList = null;

    JTextField subjtxt = null;

    JButton startButton = null;

    //Vector<Data> vTestSet = null;

    Vector<BouncingTrial> vTrials = null;

    LogFile log = null;

    String fileName = null, inputDir = "Input_Files/", outputName = "";

    public void actionPerformed(ActionEvent e)
    {

        if (e.getSource() == fileNameList)
        {
            fileName = (String) fileNameList.getSelectedItem();
            System.out.println(fileName);
        } else if (e.getSource() == subjtxt)
        {
            outputName = subjtxt.getText();
            if (outputName.length() == 0) /* Empty String */
            {
                /* ReInput the Text */
                JOptionPane.showMessageDialog(this,
                        "Please input a subject ID.");
                subjtxt.requestFocus();

            } else
            {
                /* Create A Output FileName */
                outputName = outputName + ".dat";
                System.out.println(outputName);
            }

        } else if (e.getSource() == startButton)
        {
            /* Read Input Files to create Test Trials */
            outputName = subjtxt.getText();
            
            if (outputName.length() == 0) /* Empty String */
            {
                JOptionPane.showMessageDialog(this,
                        "Please input a subject ID.");
                /* ReInput the Text */
                subjtxt.requestFocus();
            } else
            {
               
                /* Start Reading a File */
                File inputFile = new File(inputDir + fileName);
          
                try
				{
					setupTest(inputFile);
				} catch (IOException e1)
				{
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
                
				/*
				 * Get Canvas width, height, background Color
				 */
				BouncingTrial
					oTrial = vTrials.firstElement();
				int
					nCanvasWidth = oTrial.getWidth(),
					nCanvasHeight = oTrial.getHeight();
				
				Color
					bgColor = oTrial.getBgColor();
				
                     
                   
                BouncingBallFrame frame = new BouncingBallFrame(
                								"Selective Looking Application",	// Applicaiton Title
                                                outputName,							// Output Name
                                                vTrials,							// A set of Trials
                                                nCanvasWidth,						// Canvas Width
                                                nCanvasHeight,						// Canvas Height
                                                bgColor);							// Canvas background Color
                
                          
                frame.setLocation(100,100);
                //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setSize(nCanvasWidth ,nCanvasHeight+20);
                frame.setVisible(true);
             
                
            }
        }
        return;
    }
    
    
    private void setupTest(File inputFile) throws IOException
    {
    		// Create A Trial
    		vTrials = new  Vector<BouncingTrial>();
    	
    		// Create A Buffered Reader
    		BufferedReader
				reader = new BufferedReader(new FileReader(inputFile));
			
			String
				line = reader.readLine();	// skip the comments
			line = reader.readLine();
			
			BouncingTrial
				oTrial = null;
			
			while(line != null)
			{
				oTrial = parseEachLine(line);
				// 	Interprate Each Line and Create A Trial
				vTrials.add(oTrial);
				// Read Next Line until end of line reached
				line = reader.readLine();
			}//End while
			
	   	
    	
    	
       return;
    }//End setupTest Method
    
    private BouncingTrial parseEachLine(String line)
    {
    	String
    		regx = "\\s+";
    	
    	String[]
    		parameters = line.split(regx);
     	
    	
    	
    	int
    	nWidth,					// Canvas Width
    	nHeight,				// Canvas Height
        nBottomY = 0,			// Y Boundary for Ball to Disappear
        inRadius,				// Initial Radius
        endRadius,				// End Radius
        frameDuration,			// Frame Duration in milliseconds
        nDeltaDis,				// Incremental Distance
        nDev =0 ,               // Deviation in Degree
        nTrialType = -1;        //  left or right or straight
        
    Point
    	p1,
    	p2;
    	
    Color 
    	ballColor,				// Ball Color
    	bgColor;				// Background Color
    	
    	// Get Trial Type
    
    	
    
    	if(	parameters[0].equalsIgnoreCase("LEFT"))
    	{
    		nTrialType = BouncingTrial.LEFT_TRIAL;
    	}
    	else if(	parameters[0].equalsIgnoreCase("RIGHT"))
    	{
    		nTrialType = BouncingTrial.RIGHT_TRIAL;
    	}
    	else if(	parameters[0].equalsIgnoreCase("STRAIGHT"))
    	{
    		nTrialType = BouncingTrial.STRAIGHT_TRIAL;
    	}
    	
    	
    	 
    	// Get Ange Deviation
    	nDev = Integer.parseInt(parameters[1]);
    	
    	
    	
    	
    	// Get Initial Radius
    	inRadius  = Integer.parseInt(parameters[2]);
    	
    	// Get End Radius
    	endRadius  = Integer.parseInt(parameters[3]);
    	
    	// Get Ball Color
    	int
    		red = Integer.parseInt(parameters[4]),
    		green = Integer.parseInt(parameters[5]),
    		blue = Integer.parseInt(parameters[6]);
    	ballColor = new Color(red, green, blue);
    
    	// Get Background Color
    	red = Integer.parseInt(parameters[7]);
		green = Integer.parseInt(parameters[8]);
		blue = Integer.parseInt(parameters[9]);
    	bgColor = new Color(red, green, blue);
    	
    	// Get Frame Duration
    	frameDuration = Integer.parseInt(parameters[10]);
    	
    	// Get Incremental Distance
    	nDeltaDis = Integer.parseInt(parameters[11]);
    	
    	// Get Width
    	nWidth = Integer.parseInt(parameters[12]);
    	
    	// Get Height
    	nHeight = Integer.parseInt(parameters[13]);
    	
    	// Get Bottom Y
    	nBottomY = nHeight - 1*endRadius;
    	
    	// Get Point 1 and Point 2
    	p1 = new Point(nWidth/2,20);
        p2 = new Point(nWidth/2,nHeight/2);
    	
        // Create A Trial
        BouncingTrial 
        		oTrial = new BouncingTrial(nTrialType,     // Left or Right Trial
                nDev,           // Deviation in Angle
                inRadius,		// Initial Radius
                endRadius,		// End Radius
                ballColor,	// Ball Color
                bgColor,		// Background Color
               frameDuration,	// Frame Duration in milliseconds
               nDeltaDis,		// Incremental Distance
                nWidth,			// Canvas Width
                nHeight,		// Canvas Height
                p1,           // Starting Point
                p2,           // Middle Point
                nBottomY);    // Y Boundary at Bottom
        
            
    	
    	return oTrial;
    }// End parseEachLine
    
    public StartFrame(String sTitle)
    {
        super(sTitle);
        setStartGUI();
    }

    /* Setup Graphic User Interface */
    public void setStartGUI()
    {
        Container container = getContentPane();
        /* Set GridBagLayout */
        container.setLayout(new GridBagLayout());
        /* set JLabel for Subject ID */
        JLabel subjlbl = new JLabel("Subject ID"), fileNamelbl = new JLabel(
                "Test File Name");

        subjtxt = new JTextField(20);
        subjtxt.addActionListener(this);

        //Create the combo box, select item at index 0.
        fileNameStrings = readFiles(inputDir, ".txt");
        fileNameList = new JComboBox(fileNameStrings);
        fileNameList.setSelectedIndex(0);
        fileName = fileNameStrings[0];
        fileNameList.addActionListener(this);

        startButton = new JButton("Start Experiment");
        startButton.addActionListener(this);

        /* Add Component to the Frame */
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        container.add(subjlbl, c);

        c.gridx = 1;
        c.gridy = 0;
        container.add(subjtxt, c);

        c.gridx = 0;
        c.gridy = 1;
        container.add(fileNamelbl, c);

        c.gridx = 1;
        c.gridy = 1;
        container.add(fileNameList, c);

        c.weightx = 0.0;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 2;
        container.add(startButton, c);

    }//End setStartGUI Method

    

    public static String[] readFiles(String strDir, /* File Directory */
    String strEx) /* File Extension */
    {
        String dir = new String(strDir);
        File fDir = new File(dir);

        MyFileFilter myFileFilter = new MyFileFilter(strEx);

        String[] files = fDir.list(myFileFilter);

        return files;
    }//End readFiles

    public static void main(String[] args)
    {
        Toolkit tk = Toolkit.getDefaultToolkit();

        Dimension d = tk.getScreenSize();
        int 
            x = (int) d.getWidth() / 2 - 700 / 2, 
            y = (int) d.getHeight() / 2 - 500 / 2;
        
        StartFrame
            frame = new StartFrame("Invigilance Application");
        
        /* Set Frame Look */
        frame.setLocation(x, y);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(500, 500);
        frame.pack();
        frame.setVisible(true);
    }//End main

}//End Class Definition


⌨️ 快捷键说明

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