会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
天之南
java 实现 Terminal
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 NotifyKeyListener{ private MyDisplay disp = null; private DataModel dm = null; public TestTerminal(){ this.setSize(300, 200); this.setDefaultCloseOperation(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.setNotifyKeyListener(this); jContentPane.add(new JScrollPane(disp), BorderLayout.CENTER); } return jContentPane; } public static void main(String args[]){ new TestTerminal(); } public void notify(KeyEvent e) { try{ int c = e.getKeyChar(); if(c==7){ Toolkit.getDefaultToolkit().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,AdjustmentListener,ComponentListener,InputMethodListener, InputMethodRequests ,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 getInputMethodRequests() { return this; } public void setNotifyKeyListener(NotifyKeyListener listener){ this.keylistnerer = listener; } public void keyPressed(KeyEvent e) { if(isDiaplayInputKey == false){ this.keylistnerer.notify(e); return; } switch(e.getKeyCode()){ case KeyEvent.VK_BACK_SPACE: if(dm.getCursorCol()==0){ Toolkit.getDefaultToolkit().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.getDefaultToolkit().beep(); return; } dm.setCursorCol(dm.getCursorCol()-1); break; case KeyEvent.VK_RIGHT: if(dm.getCursorCol()==dm.rowLength(dm.getCursorRow())){ Toolkit.getDefaultToolkit().beep(); return; } dm.setCursorCol(dm.getCursorCol()+1); break; case KeyEvent.VK_UP: if(dm.getCursorRow()==0){ Toolkit.getDefaultToolkit().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.getDefaultToolkit().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.getDefaultToolkit().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('黄'); for(int row=0;row<DM.ROWSIZE();ROW++){ if(rowwidth fm.getHeight(); newrows="this.getHeight()" int * rowwidth="dm.rowSize()" fm="this.getGraphics().getFontMetrics(getFont());" FontMetrics } return; if(this.getGraphics()="=null){" org){ updateScrollbar(String void public offsety+="fm.getHeight();" g.setColor(fcolor); g.drawLine(offsetx,offsety,offsetx+5,offsety); g.setColor(dm.getCursorColor()); row="=dm.getCursorRow()){" && if(cursor_flag="=false" offsetx+="fm.charWidth(c);" cursor_flag="false;" g.drawLine(offsetx,offsety,offsetx+6,offsety); ){ col="0;col<dm.rowLength(row);col++){" if(row="=dm.getCursorRow()" offsety); offsetx, 1, 0, char[]{c}, g.drawChars(new col); c="dm.getChar(row," char for(int boolean continue; scrollbar.isEnabled()){ if(row<topValue offsetx="0;">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()<SCROLLBAR.GETVALUE()){ class="" int } void public row c="text.first();" char for(int boolean if(sb.length() sb="new" StringBuffer StringBuffer()); lines.add(new if(lines.size()="=this.cursorRow){" c){ writeChar(char sb.charAt(col); return col){ , getChar(int this.cursorRow="cursorRow;" { cursorRow) setCursorRow(int cursorRow; getCursorRow() this.cursorColor="cursorColor;" cursorColor) setCursorColor(Color cursorColor; getCursorColor() Color this.cursorCol="cursorCol;" cursorCol) setCursorCol(int cursorCol; getCursorCol() DataModel(){ cursorColor="Color.red;" private cursorCol="0;" cursorRow="0;" ArrayList(); lines="new" List DataModel 10; direction) orientation, visibleRect, getScrollableUnitIncrement(Rectangle true; getScrollableTracksViewportWidth() getScrollableTracksViewportHeight() getScrollableBlockIncrement(Rectangle getPreferredSize(); getPreferredScrollableViewportSize() Dimension null; attributes) getSelectedText(Attribute[] AttributedCharacterIterator y) x, getLocationOffset(int TextHitInfo 2; getInsertPositionOffset() getCommittedTextLength() attributes){ Attribute[] endIndex, beginIndex, getCommittedText(int cancelLatestCommittedText(Attribute[] {} event) caretPositionChanged(InputMethodEvent e) componentHidden(ComponentEvent componentMoved(ComponentEvent componentShown(ComponentEvent keyReleased(KeyEvent keyTyped(KeyEvent Rectangle(wpix,hpix,0,0); new hpix+="this.dm.getCursorRow()*this.getGraphics().getFontMetrics().getHeight();" i)); wpix+="this.getGraphics().getFontMetrics().charWidth(dm.getChar(this.dm.getCursorRow()," i="0;i<len;i++){" len="event.getCommittedCharacterCount();" hpix="this.getLocationOnScreen().y;" wpix="this.getLocationOnScreen().x;" offset) getTextLocation(TextHitInfo Rectangle this.repaint(); dm.setCursorCol(dm.getCursorCol()+1); dm.insertChar(sb.charAt(i)); sb.append(c); !="null)" (text if StringBuffer(); text="event.getText();" inputMethodTextChanged(InputMethodEvent topValue="e.getValue();" adjustmentValueChanged(AdjustmentEvent this.updateScrollbar(null); componentResized(ComponentEvent scrollbar.setValue(scrollbar.getValue()-1);>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 NotifyKeyListener{ public void notify(KeyEvent e); }
posted on
2008-07-18 12:00
天之南
阅读(
782
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
导航
博客园
首页
联系
订阅
管理
公告