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

📄 applet1.java

📁 学习数据结构的最好辅助工具,快速帮助你熟悉数据结构的相关技术
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;

import Demo;
//import GoThread;


public class Applet1 extends Applet implements ActionListener,Runnable 
{
	public	int			m_nStep=-1;
	public	boolean		m_bPaused	= true;
	
	public	Button		m_btnNext,m_btnReset,m_btnGo,m_btnSetting,m_btnStepInto;
	
	private TextArea	m_taSource;
	private boolean		m_bSortType	= true;
	private boolean		m_bStepInto = true;
	
	
	private String		m_sDefault="ESACFUIW";
	private String		m_sData;
	
	public	Demo		m_Demo;
	public  Thread		m_Go;

	
	
	
	
	private String m_sCode[]=
	{
		"void HeapSort ( Elem R[], int n ) {       ",	//0
		"  for ( i=n/2; i>0; --i )                 ",	//1
		"    HeapAdjust ( R, i, n );               ",	//2
	    "  for ( i=n; i>1; --i ) {                 ",   //3
	    "    R[1]←→R[i];                         ",   //4
		"    HeapAdjust(R, 1, i-1);                ",	//5
		"  }                                       ",	//6
	    "} // HeapSort                             ",	//7
		"                                          ",   //8
		"void HeapAdjust (Elem R[], int s, int m){ ",	//9
		"  rc = R[s];                              ",	//10
	    "  for ( j=2*s; j<=m; j*=2 ) {             ",   //11 
		"    if(j<m && Compare(R[j].key,R[j+1].key))",  //12
		"       ++j;                               ",	//13 
		"    if( Compare(R[j].key,rc.key)) break;  ",	//14
		"    R[s] = R[j]; s = j;                   ",	//15
		"  }                                       ",	//16
	    "  R[s] = rc;                              ",	//17
	    "} // HeapAdjust                           "	//18     
	};
	
		
	
	private void addComponent(Component c,int x,int y, int width,int height)
	{
		c.setLocation (x,y);
		c.setSize (width,height);
		add (c);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource ()==m_btnSetting)
		{
			Frame		m_Frame=new Frame("setting test");
			SettingDlg	m_Dlg=new SettingDlg (m_Frame,m_sData,m_bSortType);
			m_Dlg.setDefault (m_sDefault);			
			m_Dlg.setLocation (320,240);
			m_Dlg.setVisible (true);
			m_bPaused = true;			
			
			if(m_Dlg.IsUpdate()){
				m_sData		=m_Dlg.getData ();
				m_bSortType	=m_Dlg.getSortType ();				
				m_nStep=-1;				
				showSource();
				m_Demo.reset ();
				m_Demo.setData(m_sData,m_bSortType,m_bStepInto);													
				
			}			
		}
		else if(e.getSource ()==m_btnNext)
		{
			if(m_nStep==-1)
			{	
				m_Demo.reset();
				m_Demo.setData (m_sData,m_bSortType,false);							
			}
			m_Demo.setStepIn (false);
			m_nStep=m_Demo.proceed (m_nStep);			
			showSource();			
		}
		else if(e.getSource ()==m_btnReset)
		{
			m_nStep=-1;			
			m_Demo.reset();				
			showSource();
			//m_Go.suspend ();
			m_Go.stop ();
			m_Go = new Thread (this);
			m_Go.start ();
		}
		else if(e.getSource ()==m_btnStepInto)
		{
			
			if(m_nStep==-1)
			{	
				m_Demo.reset();
				m_Demo.setData (m_sData,m_bSortType,true);							
			}
			m_Demo.setStepIn (true);
			m_nStep=m_Demo.proceed (m_nStep);			
			showSource();			
			
		}
		else if(e.getSource ()==m_btnGo)
		{
			if(m_nStep==-1)
			{				
				m_Demo.reset ();
				m_Demo.setData (m_sData,m_bSortType,true);				
			}
			if(m_bPaused)
			{
				m_Go.resume ();
				m_btnGo.setLabel ("暂停执行");
				m_btnNext.setEnabled (false);
				m_btnReset.setEnabled (false);
				m_btnSetting.setEnabled(false );
				m_btnStepInto.setEnabled (false);
				m_bPaused=false;
			}else{
				m_Go.suspend ();
				m_btnGo.setLabel ("连续执行");
				m_btnNext.setEnabled (true);
				m_btnReset.setEnabled (true);
				m_btnSetting.setEnabled (true);
				m_btnStepInto.setEnabled (true);
				m_bPaused=true;
			}
		
		}

	}
	
	
	public void setSource()
	{			
		m_taSource.setText("");
		for(int i=0;i<m_sCode.length ;i++)
			m_taSource.append (m_sCode[i]+"\r\n");						
		
	}
	public void showSource()
	{
		if(m_nStep==-1)
		{
			m_taSource.select (0,0);
			return;		
		}
		int nStart=0;
		int nEnd=0;
		int step=m_nStep;
		for(int i=0;i<step;i++)
			nStart+=m_sCode[i].length()+1;
		nEnd=nStart+m_sCode[step].length ()+1;
		m_taSource.select(nStart,nEnd);	
		
			
	}
						   
	public void init()
	{
		
		m_Demo			=new Demo ();
		
		m_taSource		=new TextArea(20,50);
		m_taSource.		setEditable(false);
		m_taSource.		setBackground( Color.white );
		
		setSource ();
		
		m_btnNext		=new Button ("单步执行");
		m_btnGo			=new Button ("连续执行");
		m_btnReset		=new Button ("重新开始");		
		m_btnSetting	=new Button ("排序设置");
		m_btnStepInto	=new Button ("跟踪子程");
		
		
		m_btnNext.		addActionListener(this);
		m_btnGo.		addActionListener (this);
		m_btnReset.		addActionListener (this);		
		m_btnSetting.	addActionListener(this);
		m_btnStepInto.	addActionListener (this);
		
		int nHeight		=20;
		int nWidth		=15;
		
		setLayout(null);
		addComponent(m_taSource,20,20,320,240);
		
		addComponent(m_Demo,340,20,380,280);
		
		addComponent(m_btnReset,	20,	320,nWidth*5,nHeight);
		addComponent(m_btnNext,		120,320,nWidth*5,nHeight);		
		addComponent(m_btnStepInto,	250,320,nWidth*5,nHeight);
		addComponent(m_btnGo,		350,320,nWidth*5,nHeight);
		addComponent(m_btnSetting,	450,320,nWidth*5,nHeight);
		
		
		m_sData			=m_sDefault;
		m_nStep			=-1;
		m_bSortType		=true;
		
		m_Go			=new Thread (this);
		
		m_Go.start ();
		//m_btnStepIn.setEnabled(false);
		//m_Go			=new GoThread (this);
		//m_Go.start ();				
		m_Demo.reset ();
		
	}
	public void run()
	{
		
		m_Go.suspend ();
		do
		{
			
			if((m_nStep = m_Demo.proceed(m_nStep)) == -1)
			{
				m_btnGo.setLabel("连续执行");
				m_bPaused = true;
				m_btnNext.setEnabled(true);
				m_btnReset.setEnabled(true);
				m_btnSetting .setEnabled (true);
				m_btnStepInto.setEnabled (true);
				showSource();
				m_Go.suspend ();
			}
			showSource();
			try
			{
				m_Go.sleep (800);
			}
			catch(InterruptedException exception)
			{
				System.err.println("Exception:" + exception.toString());
			}			
			
		}while(true);
	}

	public class SettingDlg extends Dialog implements ActionListener,ItemListener,WindowListener
	{
		private Button		m_btnRandom,m_btnOk,m_btnCancel,m_btnDefault;	
		private Label		m_lblMsg;
		private Checkbox	m_Radio1,m_Radio2;
		private CheckboxGroup
							m_Group=new CheckboxGroup ();
		
		private Label		m_lblSortType;
		private TextField	m_tfData;	
	
		private boolean		m_bSortType;
		private boolean		m_bUpdate=false;
		private String		m_sData;
		private String		m_sDefault;
							
		SettingDlg(Frame hostFrame,String sData,boolean bSortType)
		{	
		
				super(hostFrame,"排序设置",true);
			
				setSize(300,210);
				setResizable(false);
				setLayout(null);
			
				m_sData		=sData;
				m_sDefault	=sData;
				m_bSortType	=bSortType;
				m_bUpdate	=false;
					
				m_lblMsg	  = new Label ("输入数据(少于9个字符):");
				m_lblSortType = new Label ("排序类型:");
			
				m_tfData	=new TextField(m_sDefault);
				m_Radio1	=new Checkbox ("正向排序",m_Group,m_bSortType);
				m_Radio2	=new Checkbox ("逆向排序",m_Group,!m_bSortType);
				m_btnRandom	=new Button ("随机生成");
				m_btnOk		=new Button("确 定");
				m_btnCancel	=new Button ("取 消");
				m_btnDefault=new Button ("默 认");	
			
				m_btnOk.		addActionListener (this);
				m_btnCancel.	addActionListener (this);
				m_btnDefault.	addActionListener (this);
				m_btnRandom.	addActionListener (this);
			
				int nHeight	=20;
				int nWidth	=15;
			
				addComponent(m_lblMsg,20,40,12*nWidth,nHeight);
				addComponent(m_tfData,45,60,10*nWidth,nHeight);			
				addComponent(m_btnRandom,45+10*nWidth+5,60,5*nWidth,nHeight);
			
			
				addComponent(m_lblSortType,20,90,4*nWidth,nHeight);						
				addComponent(m_Radio1,50,120,6*nWidth,nHeight);
				addComponent(m_Radio2,50+6*nWidth+30,120,6*nWidth,nHeight);
			
				m_Radio1.addItemListener (this);
				m_Radio2.addItemListener (this);
			
				addComponent(m_btnOk,		20,			160,	5*nWidth,	nHeight);
				addComponent(m_btnCancel,20+5*nWidth+10,	160,	5*nWidth,	nHeight);
				addComponent(m_btnDefault,20+10*nWidth+20,	160,	5*nWidth,	nHeight);
				addWindowListener(this);
			
				pack();					
		}
		public void setDefault(String sData)
		{
			m_sDefault=sData;
		}
		private void addComponent(Component c,int x,int y,int width,int height)
		{
			c.setLocation (x,y);
			c.setSize (width,height);		
			add(c);
		}
		private String randGenData()
		{
			String table="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";		
			String str="";
			int l=table.length ();
		
			for(int i=0;i<8;i++)
			{	
				int index=(int)(40*Math.random ());
				str=str+table.charAt (index);
			}
			return str;			
		}
		public void actionPerformed(ActionEvent e)
		{		
			if(e.getSource ()==m_btnOk)
			{			
				String str=m_tfData.getText ();		
				if(str.length()>0)							
				{				
					if(str.length ()>9)
						str=str.substring (0,8);
					m_sData=str;
					m_bUpdate=true;
					hide();
				}			
			}
			else if(e.getSource ()==m_btnCancel)
			{		
				m_bUpdate=false;
				hide();
			}
			else if(e.getSource ()==m_btnRandom)
			{
				m_sData=randGenData();
				m_tfData.setText (m_sData);			
			}
			else if(e.getSource ()==m_btnDefault)			
			{
				m_bUpdate=true;
				m_sData=m_sDefault;			
				m_tfData.setText (m_sData);
				m_bSortType=true;
				m_Radio1.setState (true);		
			}					
		}	
		public void itemStateChanged(ItemEvent e)
		{
			if(e.getSource() ==m_Radio1&&e.getStateChange ()==e.SELECTED)
				m_bSortType=true;
			else if(e.getSource ()==m_Radio2&&e.getStateChange ()==e.SELECTED )
				m_bSortType=false;			
		}
	
		public boolean IsUpdate()
		{
			return m_bUpdate;
		}
		public void windowActivated(WindowEvent e)
		{
		
		}
		public void windowClosed(WindowEvent e)
		{
		}
		public void windowClosing(WindowEvent e)
		{
			hide();
		}
		public void windowDeactivated(WindowEvent e)
		{
		}
		public void windowDeiconified(WindowEvent e)
		{
		}
		public void windowIconified(WindowEvent e)
		{
		}
		public void windowOpened(WindowEvent e)
		{
		}
		public String getData()
		{
			return m_sData;
		}
		public boolean getSortType()
		{
			return m_bSortType;
		}
	
		public void  setSortType(boolean bool)
		{	
			m_bSortType=bool;
		}			
	}

}

⌨️ 快捷键说明

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