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

📄 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 
{
	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;
	private GoThread	m_Go;
	
	
	
	
	private String m_sCode[]=
	{
		"void QuickSort(Elem R[], int n)                 ",		//0
        "{                                               ",		//1
        "    QSort(R, 1, n);                             ",		//2
        "}// QuickSort                                   ",		//3
		
		"                                                ",		//4	
		
		"void QSort (Elem R[], int low, int high)            ",		//5
	    "{                                                   ",		//6
	    "    if(low < high)                                  ",		//7
        "    {                                               ",		//8
	    "       pivotloc = Partition(R, low, high);          ",		//9
        "       QSort(R, low, pivotloc-1);                   ",		//10	
        "       QSort(R, pivotloc+1, high);                  ",		//11
        "    }                                               ",		//12
        "} // QSort                                          ",		//13
		
		"                                                    ",		//14
		
		"int  Partition (Elem R[], int low, int high)        ",		//15	
		"{                                                   ",		//16
	    "    R[0] = R[low];                                  ",		//17
  	    "    pivotkey = R[low].key;                          ",		//18	
 	    "    while(low<high)                                 ",		//19
        "    {                                               ",		//20
	    "       while(low<high&&Compare(pivotkey,R[high].key)",		//21
        "       --high;                                      ",		//22
 	    "       R[low] = R[high];                            ",		//23
		"       while(low<high&&Compare(R[low].key,pivotkey) ",		//24	
	    "       ++low;                                       ",		//25
	    "       R[high] = R[low];                            ",		//26
 	    "    }                                               ",		//27
	    "    R[low] = R[0];                                  ",		//28	
	    "    return low;                                     ",		//29
	    "} // Partition                                      "		//30	  
     
	};
	
		
	
	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);
			
			if(m_Dlg.IsUpdate()){
				m_sData		=m_Dlg.getData ();
				m_bSortType	=m_Dlg.getSortType ();
				m_bPaused	=true;
				m_nStep=-1;				
				showSource();
				m_Demo.reset ();										
			
				m_Go=new GoThread (this);
				m_Go.start ();	
				
			}			
		}
		else if(e.getSource ()==m_btnNext)
		{
			m_bStepInto = false;
			if(m_nStep==-1)
			{				
				m_Demo.setData (m_sData,m_bSortType,m_bStepInto);			
			}
			m_Demo.setStep (m_bStepInto);
			m_nStep=m_Demo.proceed (m_nStep);			
			showSource();			
		}
		else if(e.getSource ()==m_btnStepInto)
		{
			m_bStepInto = true;
			if(m_nStep==-1)
				m_Demo.setData(m_sData,m_bSortType,m_bStepInto);
			m_Demo.setStep (m_bStepInto);
			m_nStep = m_Demo.proceed (m_nStep);
			showSource();		
		
		}
		else if(e.getSource ()==m_btnReset)
		{
			m_nStep=-1;			
			m_Demo.reset();				
			showSource();
			m_Go=new GoThread (this);
			m_Go.start ();				
		}
		else if(e.getSource ()==m_btnGo)
		{
			if(m_nStep==-1)
			{				
				m_Demo.setData (m_sData,m_bSortType,m_bStepInto);			
			}
			if(m_bPaused)
			{
				m_Go.resume ();
				m_btnGo.setLabel ("暂停执行");
				m_btnNext.setEnabled (false);
				m_btnReset.setEnabled (false);
				m_btnStepInto.setEnabled (false);
				m_btnSetting.setEnabled(false );
				m_bPaused=false;
			}
			else
			{
				m_Go.suspend ();
				m_btnGo.setLabel ("连续执行");
				m_btnNext.setEnabled (true);
				m_btnReset.setEnabled (true);
				m_btnStepInto.setEnabled (true);
				m_btnSetting.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,320,320,nWidth*5,nHeight);
		addComponent(m_btnGo,	420,320,nWidth*5,nHeight);
		
		addComponent(m_btnSetting,520,320,nWidth*5,nHeight);
		
		
		m_sData			=m_sDefault;
		m_nStep			=-1;
		m_bSortType		=true;
		m_bStepInto		=true;
		
		//m_btnStepIn.setEnabled(false);
		m_Go			=new GoThread (this);
		m_Go.start ();				
		
	}

	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 ()>10)
						str=str.substring (0,10);
					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 + -