Java Terminal Implementation

Source: Internet
Author: User
Import Java. AWT. *; import javax. swing. *; import Java. AWT. event. *; import Java. AWT. font. texthitinfo; import Java. AWT. im. inputmethodrequests; import Java. text. attributedcharacteriterator; import Java. text. attributedcharacteriterator. attribute; import Java. util. arraylist; import Java. util. list; public class testterminal extends jframe implements policykeylistener {private mydisplay disp = NULL; private datamodel dm = NULL; Public testterminal () {This. setsize (300,200); this. setdefaclocloseoperation (windowconstants. dispose_on_close); this. setcontentpane (getjcontentpane (); this. settitle ("jframe"); this. setvisible (true);} private jpanel getjcontentpane () {jpanel jcontentpane = NULL; If (jcontentpane = NULL) {jcontentpane = new jpanel (); jcontentpane. setlayout (New borderlayout (); DM = new datamodel (); disp = new mydisplay (DM); Disp. setpolicykeylistener (this); jcontentpane. add (New jscrollpane (disp), borderlayout. center);} return jcontentpane;} public static void main (string ARGs []) {New testterminal ();} public void every Y (keyevent e) {try {int c = E. getkeychar (); If (C = 7) {toolkit. getdefatooltoolkit (). beep (); return;} If (C = 8) {DM. setcursorcol (DM. getcursorcol ()-1); Disp. repaint (); return;} If (C = 13) {return;} If (C = 10) {DM. setcursorrow (DM. getcursorrow () + 1); DM. setcursorcol (0); DM. addnewline (DM. getcursorrow (); Disp. updatescrollbar ("down"); Disp. repaint (); return;} DM. writechar (char) C); DM. setcursorcol (DM. getcursorcol () + 1); Disp. repaint ();} catch (exception dx) {dx. printstacktrace () ;}} class mydisplay extends jpanel implements keylistener, listener, componentlistener, inputmethodlistener, listener, scrollable {Private Static final long serialversionuid = 1l; private datamodel dm = NULL; private jscrollbar scrollbar = new jscrollbar (jscrollbar. vertical); Private color fcolor = color. white; private color bcolor = color. black; private int topvalue = 0; private Boolean isdiaplayinputkey = false; private notifykeylistener keylistnerer = NULL; Public mydisplay (datamodel DM) {This. dm = DM; this. setfocusable (true); this. addkeylistener (this); this. setlayout (New borderlayout (); this. addcomponentlistener (this); this. add (scrollbar, borderlayout. east); scrollbar. addadjustmentlistener (this); enableinputmethods (true); addinputmethodlistener (this);} public inputmethodrequests listener () {return this;} public void setpolicykeylistener (policykeylistener listener) {This. keylistnerer = listener;} public void keypressed (keyevent e) {If (isdiaplayinputkey = false) {This. keylistnerer. Y (E); return;} switch (E. getkeycode () {Case keyevent. vk_back_space: If (DM. getcursorcol () = 0) {toolkit. getdefatooltoolkit (). beep (); return;} DM. setcursorcol (DM. getcursorcol ()-1); DM. deletechar (DM. getcursorrow (), DM. getcursorcol (); break; Case keyevent. vk_enter: DM. setcursorrow (DM. getcursorrow () + 1); DM. setcursorcol (0); DM. addnewline (DM. getcursorrow (); this. updatescrollbar ("down"); break; Case keyevent. vk_left: If (DM. getcursorcol () = 0) {toolkit. getdefatooltoolkit (). beep (); return;} DM. setcursorcol (DM. getcursorcol ()-1); break; Case keyevent. vk_right: If (DM. getcursorcol () = DM. rowlength (DM. getcursorrow () {toolkit. getdefatooltoolkit (). beep (); return;} DM. setcursorcol (DM. getcursorcol () + 1); break; Case keyevent. vk_up: If (DM. getcursorrow () = 0) {toolkit. getdefatooltoolkit (). beep (); return;} DM. setcursorrow (DM. getcursorrow ()-1); If (DM. getcursorcol ()> DM. rowlength (DM. getcursorrow () {DM. setcursorcol (DM. rowlength (DM. getcursorrow ();} This. updatescrollbar ("up"); break; Case keyevent. vk_down: If (DM. getcursorrow () = DM. rowsize ()-1) {toolkit. getdefatooltoolkit (). beep (); return;} DM. setcursorrow (DM. getcursorrow () + 1); If (DM. getcursorcol ()> DM. rowlength (DM. getcursorrow () {DM. setcursorcol (DM. rowlength (DM. getcursorrow ();} This. updatescrollbar ("down"); break; Case keyevent. vk_home: DM. setcursorcol (0); break; Case keyevent. vk_end: DM. setcursorcol (DM. rowlength (DM. getcursorrow (); break; Case keyevent. vk_delete: If (DM. getcursorcol () = DM. rowlength (DM. getcursorrow () {toolkit. getdefatooltoolkit (). beep (); return;} DM. deletechar (DM. getcursorrow (), DM. getcursorcol (); break; Case keyevent. vk_f1: Case keyevent. vk_f2: Case keyevent. vk_f3: Case keyevent. vk_f4: Case keyevent. vk_f5: Case keyevent. vk_f6: Case keyevent. vk_f7: Case keyevent. vk_f8: Case keyevent. vk_f9: Case keyevent. vk_f10: Case keyevent. vk_f11: Case keyevent. vk_f12: Case keyevent. vk_tab: Case keyevent. vk_escape: Case keyevent. vk_page_up: Case keyevent. vk_page_down: Case keyevent. vk_shift: Case keyevent. vk_caps_lock: break; default: DM. insertchar (E. getkeychar (); DM. setcursorcol (DM. getcursorcol () + 1);} This. updateui ();} public void paintcomponent (Graphics g) {G. setcolor (bcolor); G. fillrect (0, 0, this. getwidth (), this. getheight (); G. setcolor (fcolor); fontmetrics fm = This. getgraphics (). getfontmetrics (); int offsety = FM. charwidth (& apos; yellow & apos;); For (int row = 0; row
 
  
This. getheight () {scrollbar. setenabled (true);} else {scrollbar. setenabled (false);} scrollbar. setminimum (0); scrollbar. setmaximum (DM. rowsize ()-newrows); If (Org! = NULL & ORG. equals ("down") {If (DM. getcursorrow ()> = (this. getheight ()/FM. getheight () {scrollbar. setvalue (scrollbar. getvalue () + 1) ;}} if (Org! = NULL & ORG. Equals ("up") {If (DM. getcursorrow ()
  
   
This. cursorcol) {sb. deletecharat (this. cursorcol);} sb. insert (this. cursorcol, c);} public void insertchar (char c) {If (lines. size () = This. cursorrow) {lines. add (New stringbuffer ();} stringbuffer sb = (stringbuffer) lines. get (this. cursorrow); sb. insert (this. cursorcol, c);} public void addnewline (INT row) {This. lines. add (row, new stringbuffer ();} public void deletechar (INT row, int col) {stringbuffer sb = (stringbuffer) lines. get (ROW); sb. deletecharat (COL);} public int rowsize () {return lines. size ();} public int rowlength (INT row) {return (stringbuffer) lines. get (ROW )). length () ;}} interface policykeylistener {public void Policy (keyevent E );}
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.