java簡易編輯器

 https://blog.csdn.net/dongze2/article/details/103654771 這裏給出了實現一個編輯器所需要的數據結構。下面採用line span的數據結構來實現一個簡易的編輯器。

實現後的編輯器具有的功能如下:1、輸入和刪除字符 2、正常的換行和鼠標響應 3、正常的上下左右鍵控制4、保存功能。

很多功能還沒有實現,計劃在以後實現的包括1、鼠標拖拽選擇文本 2、文本的複製粘貼 3、對某一個具體編程語言的高亮,以及語法糾錯 4、滾動條 5、對中文的支持

採用了Swing編寫程序,在寫之前看到好多人說Swing過時了之類的,一度衝動的想學一下javaFX,後來想到了沒有什麼框架能一直存在,主要的是算法。於是還是用Swing寫了。

整體結構很簡單,一個JFrame內置一個自定義的JComponent,最終效果:

 沒錯,我準備做一個verilog編輯器。

首先讓程序在屏幕中間顯示

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	int screen_width=(int)screenSize.getWidth();
	int screen_height=(int)screenSize.getHeight();
	this.setBounds(screen_width>>2, screen_height>>2, screen_width>>1, screen_height>>1);

所有的文字按照line span結構存在一個linkedlist當中。當打開一個文件時候,我們需要將文件讀入

		line_number=new LinkedList<>();
		//if the file is given;
		if(string!=null)
		{
			//read the file to the linkedlist
			file_name=string;
			File f= new File(string);
			String str;
			if(f.exists())
			{
				try {
					 BufferedReader reader=new BufferedReader(new FileReader(f));
					while((str=reader.readLine())!=null) line_number.add(str);
					reader.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}else
			{
				try {
					f.createNewFile();
					line_number.add("");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}else//if is a new page
		{
			line_number.add("");
		}

光標由一個線程控制,每隔一段時間就閃一下,當然,移動光標時候要讓他常亮

		Runnable change_cursor_state=new Runnable() {
			
			@Override
			public void run() {
				int i=0;
				while(true)
				{
					try {
						Thread.sleep(1);
						if(lock)
						{
							//if move the cursor,recount the delay time
							lock=false;
							i=0;
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					i++;
					if(i>300)
					{
						//change the state of the cursor
						i=0;
						if(cursor_state) cursor_state=false;
						else cursor_state=true;
						MainComponent.this.repaint();
					}
					
					
					//MainComponent.this.validate();
					
				}
				
			}
		};

編輯器被設計爲可以滾動的狀態,滾動時的偏移值需要被保存下來,同時光標相對空間的位置也要被記錄下來,兩者相加可以得到光標相對於文本的絕對位置。由於目前這個編輯器只支持英文,同時字體採用了等寬的宋體,所以控件可通過直接的單擊計算出字符的相對位置。相對位置在大多數情況下不變,只有在編輯器大小改變的時候可能改變。每次編輯器大小改變的時候。都要重新計算一些必要的參數

	@Override
	public void invalidate() {
		//System.out.println("invalidate");
		//char length and width
		char_height=getGraphics().getFontMetrics().getHeight();	
		char_width=getGraphics().getFontMetrics().charWidth('w');
		//editor length and width
		bound_rect=getBounds();
		//how many chars can show at height and length
		height_char_number=bound_rect.height/char_height;
		width_char_number=bound_rect.width/char_width;
		super.invalidate();
	}

繪製的時候,根據屏幕的偏移值進行繪製。不在空間內的文本可以不去繪製。

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.BLACK);
		g.fillRect(bound_rect.x, bound_rect.y, bound_rect.width, bound_rect.height);
		//System.out.println("paint");
		g.setFont(new Font("宋體", Font.PLAIN, 18));
		g.setColor(Color.WHITE);
		int y=-show_height;
		int x=-show_width;
		for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
		{
			String string=iterator.next();
			y++;
			//if the data is on the screen
			if(y>0)
			{		
				g.drawString(string, x*char_width, y*char_height);
				
			}
		}
		//draw the cursor
		if(cursor_state)
		{
			g.drawLine(char_width*cursor_width, cursor_height*char_height+2, char_width*cursor_width, cursor_height*char_height+char_height+2);
		}

	}
	

然後是對鼠標和鍵盤的監聽,光標的位置是相對位置,鼠標單擊的也是相對位置,但是鍵盤的按鍵輸入需要在絕對位置輸入,只要做好了轉換,便很簡單了

@Override
			public void mouseClicked(MouseEvent e) {
//				System.out.println(e.getX()/char_width+","+e.getY()/char_height);
				//which used to decide the cursor's position and assure cursor not overboard
				int temp_height=e.getY()/char_height+show_height;
				if(temp_height>line_number.size()-1) temp_height=line_number.size()-1;
				int temp_width=e.getX()/char_width+show_width;
				int compare_width=line_number.get(temp_height).length();
				//according absolute position to calculate the relative position
				if(temp_width>compare_width)
				{
					if(compare_width>width_char_number-1)
					{
						cursor_width=width_char_number-1;
						show_width=compare_width-cursor_width;
					}else
					{
						show_width=0;
						cursor_width=compare_width;
					}
				}else
				{
					cursor_width=temp_width-show_width;
				}					
				cursor_height=temp_height-show_height;
				up_down=false;
				cursor_state=true;
				lock=true;
				repaint();
				
			}

最後文件需要保存,根據打開的是新文件還是存在的文件,分別進行不同的操作

public void save_file()
	{
		if(file_name!=null)
		{
			try {
				BufferedWriter writer=new BufferedWriter(new FileWriter(new File(file_name)));
				for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
				{
					String string=iterator.next();
					writer.write(string);
					writer.newLine();
				}
				writer.flush();
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else
		{
			JFileChooser chooser=new JFileChooser();
			int returnVal=chooser.showSaveDialog(new JPanel());
			if(returnVal==JFileChooser.APPROVE_OPTION) {
				file_name=chooser.getSelectedFile().getPath();
				File file=new File(file_name);
				if(file.exists()) file.delete();
				try {
					file.createNewFile();
					BufferedWriter writer=new BufferedWriter(new FileWriter(file));
					for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
					{
						String string=iterator.next();
						writer.write(string);
						writer.newLine();
					}
					writer.flush();
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

至此,簡易的文本編輯器便實現了。

下載地址:https://download.csdn.net/download/dongze2/12047702

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章