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