gridsimgui.java

来自「一个非常著名的网格模拟器,能够运行网格调度算法!」· Java 代码 · 共 672 行 · 第 1/2 页

JAVA
672
字号
/************************************************************************* Author: Nithiapidary Muthuvelu, Junyang Liu and Nay Lin Soe* Date: 30 June 2004** The objective of this program (which consists of 3 java files including* this one, is to group a number of gridlets with small MIPS ratings into * a gridlet group according to the MIPS rating of a specific Grid resource,* and send the gridlet group to that Grid resource for the execution purpose.** This class takes input parameters from the user, creates virtual resources* and invoke "Test" class for simulation.*************************************************************************/   import javax.swing.*;   import java.awt.*;   import java.awt.event.*;   import java.util.regex.*;   import java.util.*;   import java.io.*;   import gridsim.*;	import eduni.simjava.*;    public class GridSimGUI extends JFrame implements ActionListener, ItemListener   {		//--------------------------------------------------    	// Attributes for GUI		//--------------------------------------------------      private  final String   FRAME_TITLE				= "Gridlet Coupler";      private  final int      FRAME_WIDTH         	= 752;      private  final int      FRAME_HEIGHT        	= 680;      private  final int      H_SEPARATION        	= 15;      private  final int      V_SEPARATION        	= 15;      private  final int      LABEL_WIDTH         	= 110;      private  final int      LABEL_HEIGHT        	= 25;          private  final int      TEXT_FIELD_WIDTH    	= 50;      private  final int      TEXT_FIELD_HEIGHT   	= LABEL_HEIGHT;      private  final int      BUTTON_WIDTH        	= 100;      private  final int      BUTTON_HEIGHT       	= 30;      private  final int      RESOURCE_WIDTH      	= 4 * LABEL_WIDTH;      private  final int      RESOURCE_HEIGHT     	= 5 * LABEL_HEIGHT;      private  final int      TEXT_AREA_WIDTH     	= 6 * LABEL_WIDTH + 65;      private  final int      TEXT_AREA_HEIGHT    	= 400;         private  final String[] DEVIATE_ITEMS		 	= new String[]       																{"0", "10", "20", "30", "40", "50"};      private  final String[] GROUPING_TEXT		 	= new String[] {"Group", "Ungroup"};       private  final String	DEFAULT_RESOURCE_FILE= "Resource.dat";														      private  final String   VALID_INTEGER_INPUT	= "[1-9][0-9]*";      private 	final	String	VALID_DOUBLE_INPUT	= "([1-9][0-9]*([.][0-9]+)?|[0][.][0-9]+)";         private	JLabel 			labelResource;      private 	JPanel 			panelResource;      private  JTextField     textFieldGridlets;          private  JTextField     textFieldAverageMI;        private  JTextField     textFieldOverhead;        private  JTextField		textFieldGranTime;           private  JTextArea     	textAreaOutput;         private 	JRadioButton[]	radButGrouping;      private  JComboBox		comboDeviatePercentage;      private  JCheckBox[]		checkBoxResource;         private  JButton        buttonCalculate;      private  JButton        buttonExit;				private 	JMenuItem		menuCalculate;		private 	JMenuItem		menuChangeFile;		         private  Container      contentPane;         private  ResourceFile 	resourceFile;   		//--------------------------------------------------		// Attributes for gridsim		//--------------------------------------------------      private 	String			fileName = DEFAULT_RESOURCE_FILE;      private  int 				numOfGridlets;      private  double        	averageMI;      private  int            deviatePercentage;      private  int            granTime;      private  boolean        grouping;      private  int            overhead;      		//***************************************************************		// Constructor:		// Read the file for resource list		// Draw GUI components		//***************************************************************      public GridSimGUI()      {         try         {            resourceFile = new ResourceFile(fileName);         }			         catch (FileNotFoundException exception)	         {            JOptionPane.showMessageDialog(null                  								, "The file " + fileName + " was not found."                  								, "File Not Found"                  								, JOptionPane.ERROR_MESSAGE                  								, null);         }			         catch (IOException exception)					         {            JOptionPane.showMessageDialog(null, exception, "File Reading Problem"                  								, JOptionPane.ERROR_MESSAGE, null);         }	                  catch(NoSuchElementException exception)          {            JOptionPane.showMessageDialog(null                  								, "Some information seems to be missing in "                   									+ "the resource file \""+ fileName                   									+ "\".\nChange of resource file unsuccessful."                  							  	, "Missing Information"                  								, JOptionPane.ERROR_MESSAGE                  								, null);         }			         catch(NumberFormatException exception)         {            JOptionPane.showMessageDialog(null         	        								, "Some information seems not to be in correct format in "                   									+ "the resource file \""+ fileName                   									+ "\".\nChange of resource file unsuccessful."                  								, "Wrong Format"                  								, JOptionPane.ERROR_MESSAGE                  								, null);         }      			   	         finally         {            initializeFrame();            addComponents();			           }      }   	    	/************************************************************     	* The main method that creates the frame    	*************************************************************/      public static void main(String[] args)      {         GridSimGUI frame = new GridSimGUI();         frame.setVisible(true);      }   		//***************************************************************		// Draw GUI components on the frame		//***************************************************************      private void addComponents()      {         createMenu();      				//-------------			// labels			//------------         JLabel labelGridlets             = new JLabel("Number of Gridlets", SwingConstants.RIGHT);         JLabel labelAverageMI            = new JLabel("Average MI", SwingConstants.RIGHT);          JLabel labelGranTime             = new JLabel("Granularity time (s)", SwingConstants.RIGHT);         JLabel labelOverhead             = new JLabel("Overhead time for grouping (s)", SwingConstants.RIGHT);           JLabel labelDeviatePercentage    = new JLabel("Deviate %", SwingConstants.RIGHT);         labelResource              		= new JLabel("Resource List (\"" + fileName + "\")", SwingConstants.LEFT);         			//------------------------------------			// text fields, combo box and buttons			//------------------------------------         textFieldGridlets       = new JTextField();         textFieldAverageMI      = new JTextField();         textFieldGranTime       = new JTextField();         textFieldOverhead       = new JTextField();            textAreaOutput          = new JTextArea();                  comboDeviatePercentage  = new JComboBox(DEVIATE_ITEMS);         buttonCalculate         = new JButton("Calculate");         buttonExit              = new JButton("Exit");       				//---------------------			// Resource list pane			//---------------------         JScrollPane    scrollPaneOutput = new JScrollPane(textAreaOutput);               panelResource = new JPanel();         JScrollPane scrollPaneResource = new JScrollPane(panelResource);         panelResource.setLayout(new BoxLayout(panelResource, BoxLayout.Y_AXIS));         createResourceList();                 		  	//---------------------			// Grouping Option pane			//---------------------         JPanel panelGrouping = new JPanel(new GridLayout(0, 1));         panelGrouping.setBorder(BorderFactory.createTitledBorder("Gridlet grouping"));         ButtonGroup butGrpGrouping = new ButtonGroup();         radButGrouping = new JRadioButton[GROUPING_TEXT.length];      	         for(int i = 0; i < radButGrouping.length; i++)         {            radButGrouping[i] = new JRadioButton(GROUPING_TEXT[i]);            panelGrouping.add(radButGrouping[i]);            butGrpGrouping.add(radButGrouping[i]);         }               radButGrouping[0].setSelected(true);							      	//---------------------------------------      	// setting components' size and position			//---------------------------------------         labelGridlets.setBounds(10, 20, LABEL_WIDTH, LABEL_HEIGHT);         textFieldGridlets.setBounds(20 + LABEL_WIDTH, 20, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);               labelAverageMI.setBounds(30 + LABEL_WIDTH + TEXT_FIELD_WIDTH, 20, LABEL_WIDTH, LABEL_HEIGHT);         textFieldAverageMI.setBounds(40 + 2 * LABEL_WIDTH + TEXT_FIELD_WIDTH, 20, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);               labelDeviatePercentage.setBounds(50 + 2 * LABEL_WIDTH + 2 * TEXT_FIELD_WIDTH, 20, LABEL_WIDTH, LABEL_HEIGHT);         comboDeviatePercentage.setBounds(60 + 3 * LABEL_WIDTH + 2 * TEXT_FIELD_WIDTH, 20, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);	               labelGranTime.setBounds(70 + 3 * LABEL_WIDTH + 3 * TEXT_FIELD_WIDTH, 20, LABEL_WIDTH, LABEL_HEIGHT);         textFieldGranTime.setBounds(80 + 4 * LABEL_WIDTH + 3 * TEXT_FIELD_WIDTH, 20, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);               labelResource.setBounds(10, 30 + LABEL_HEIGHT, 3 * LABEL_WIDTH, LABEL_HEIGHT);          scrollPaneResource.setBounds(10, 30 + 2 * LABEL_HEIGHT, RESOURCE_WIDTH, RESOURCE_HEIGHT);	                 panelGrouping.setBounds(30 + RESOURCE_WIDTH, 30 + 2 * LABEL_HEIGHT, LABEL_WIDTH, 10 + 3 * LABEL_HEIGHT);               buttonCalculate.setBounds(60 + RESOURCE_WIDTH + LABEL_WIDTH, 10 + 3 * LABEL_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);         buttonExit.setBounds(60 + RESOURCE_WIDTH + LABEL_WIDTH, 30 + 4 * LABEL_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT);               labelOverhead.setBounds(470, 55 + 5 * LABEL_HEIGHT, 8 * LABEL_WIDTH / 5, LABEL_HEIGHT);         textFieldOverhead.setBounds(660, 55 + 5 * LABEL_HEIGHT, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT);               scrollPaneOutput.setBounds(10, 15 + 3 * LABEL_HEIGHT+ RESOURCE_HEIGHT, TEXT_AREA_WIDTH, TEXT_AREA_HEIGHT);	           textAreaOutput.setEditable(false);               buttonCalculate.addActionListener(this);         buttonExit.addActionListener(this);      			//------------------------------------------------      	// adding components to the Frame (contentPane)			//------------------------------------------------         contentPane.add(labelGridlets);         contentPane.add(labelAverageMI);         contentPane.add(labelGranTime);         contentPane.add(labelResource);                  contentPane.add(labelOverhead);         contentPane.add(labelDeviatePercentage);         contentPane.add(comboDeviatePercentage);         contentPane.add(textFieldGridlets);         contentPane.add(textFieldAverageMI);         contentPane.add(textFieldGranTime);         contentPane.add(textFieldOverhead);         contentPane.add(panelGrouping);         contentPane.add(scrollPaneOutput);          contentPane.add(scrollPaneResource);          contentPane.add(buttonCalculate);         contentPane.add(buttonExit);      }   		//***************************************************************		// Set frame attributes		//***************************************************************      private void initializeFrame()      {         contentPane = getContentPane();         contentPane.setLayout(null);               setTitle(FRAME_TITLE);         setSize(FRAME_WIDTH, FRAME_HEIGHT);                 setDefaultCloseOperation(EXIT_ON_CLOSE);      }   		//***************************************************************		// Create JMenu on the Frame		//***************************************************************      private void createMenu()      {         JMenuBar menuBar = new JMenuBar();         JMenu	menuCommand = new JMenu("Command");         JMenuItem item;       	         item = new JMenuItem("Calculate");         item.addActionListener(this);         menuCommand.add(item);			menuCalculate = item;      	         item = new JMenuItem("Set Resource File");         item.addActionListener(this);         menuCommand.add(item);			menuChangeFile = item;      	         menuCommand.addSeparator();         item = new JMenuItem("Exit");         item.addActionListener(this);         menuCommand.add(item);      	         menuBar.add(menuCommand);      	         setJMenuBar(menuBar);      }      		//***************************************************************		// In the resource panel, actually create checkboxes		// of resources		//***************************************************************      private void createResourceList()      {         checkBoxResource = new JCheckBox[resourceFile.numberOfResources()];	           panelResource.removeAll();         for(int i = 0; i < resourceFile.numberOfResources(); i++)         {            checkBoxResource[i] = new JCheckBox(readLine(i));            checkBoxResource[i].addItemListener(this);            panelResource.add(checkBoxResource[i]);         }       }   			   	//***************************************************************		// Read the line specified by i in the resource file		//***************************************************************      private String readLine(int i)      {         String caption = "\"" + resourceFile.getNameOfResource(i) + "\"";               int MIPS = resourceFile.getMIPSofPEOfResource(i) * resourceFile.getMachinesOfResource(i) * resourceFile.getPEOfResource(i);         caption += ", " + String.valueOf(MIPS) + " MIPS";         caption += ", " + String.valueOf(resourceFile.getArchitectureOfResource(i));         caption += ", " + String.valueOf(resourceFile.getOSOfResource(i));         caption += ", cost : " + String.valueOf(resourceFile.getCostOfResource(i));                  return caption;      }   

⌨️ 快捷键说明

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