telnetcanvas.java

来自「j2me学习 简单例子」· Java 代码 · 共 818 行 · 第 1/2 页

JAVA
818
字号
                    if ( argbuf[0] == 0 ) 
                    {
                        cursor = cursor + columns;
                    }
                    else
                    {
                        cursor = cursor + columns * getArgument( 0 );
                    }
                    break;
                
                case 'C': // cursor forward by x
                    if ( argbuf[0] == 0 ) 
                    {
                        cursor = cursor + 1;
                    }
                    else
                    {
                        cursor = cursor + getArgument( 0 );
                    }
                    break;
                
                case 'D': // cursor backward by x
                    if ( argbuf[0] == 0 ) 
                    {
                        cursor = cursor - 1;
                    }
                    else
                    {
                        cursor = cursor - getArgument( 0 );
                    }
                    break;
                
                case 'd': // cursor to row x (preserve column position)
                    if ( argbuf[0] > 0 ) 
                    {
                        cursor = bound - ((rows-getArgument( 0 )+1)*columns) + ( cursor % columns );
                    }
                    break;
                
                case 'G': // cursor to column x (preserve row position)
                    if ( argbuf[0] > 0 ) 
                    {
                        cursor = cursor - ( cursor % columns ) + getArgument( 0 );
                    }
                    break;
                
                case '@': // insert x blank spaces
                    arg = getArgument( 0 );
                    for ( int i = cursor + columns-(cursor%columns); i > cursor+arg; i-- )
                    {
                        buffer[i] = buffer[i-arg];
                    }
                    for ( int i = cursor; i < cursor+arg; i++ )
                    {
                        buffer[i] = ' ';
                    }
                    break;
                
                case 'L': // insert x blank lines (not from cursor?)
                    arg = getArgument( 0 ) * columns;
                    {
                        int origin = cursor - (cursor%columns);
                        for ( int i = bound; i >= origin+arg; i-- )
                        {
                            buffer[i] = buffer[i-arg];
                        }
                        for ( int i = origin; i < origin+arg; i++ )
                        {
                            buffer[i] = 0;
                        }
                    }
                    break;
                
                case 'M': // delete x lines from cursor
                    arg = getArgument( 0 ) * columns;
                    for ( int i = cursor; i < bound; i++ )
                    {
                        if ( i < cursor + arg )
                        {
                            buffer[i] = buffer[i+arg];
                        }
                        else
                        {
                            buffer[i] = 0;
                        }
                    }
                    break;
                
                case 'P': // delete x characters from cursor
                    arg = getArgument( 0 );
                    for ( int i = cursor; i%columns!=0; i++ )
                    {
                        if ( i < cursor + arg )
                        {
                            buffer[i] = buffer[i+arg];
                        }
                        else
                        {
                            buffer[i] = 0;
                        }
                    }
                    break;
                
                case 's': // save cursor position
                    savedCursor = cursor;
                    break;

                case 'u': // restore cursor position
                    cursor = savedCursor;
                    break;
                
                case 'J': // clear region
                    if ( argbuf[0] > 0 ) arg = getArgument( 0 );
                    switch ( arg )
                    {
                        case 1: // from beginning of screen to cursor
                            for ( int i = bound - rows * columns; i <= cursor; i++ )
                            {
                                buffer[i] = 0;
                            }
                            break;
                        case 2: // clear all screen
                            cursor = bound - rows * columns;
                            for ( int i = cursor; i < bound; i++ )
                            {
                                buffer[i] = 0;
                            }
                            break;
                        default: // from cursor to end of screen
                            for ( int i = cursor; i < bound; i++ )
                            {
                                buffer[i] = 0;
                            }
                            break;
                    }
                    break;
                
                case 'K': // erase rest of line
                    if ( argbuf[0] > 0 ) arg = getArgument( 0 );
                    switch ( arg )
                    {
                        case 1: // from beginning of line to cursor
                            buffer[cursor] = 0;
                            for ( int i = cursor - (cursor % columns); i < cursor; i++ )
                            {
                                buffer[i] = 0;
                            }
                            break;
                        case 2: // clear all line
                            buffer[cursor - (cursor % columns)] = 0;
                            for ( int i = cursor - (cursor % columns) + 1; i % columns != 0; i++ )
                            {
                                buffer[i] = 0;
                            }
                            break;
                        default: // from cursor to end of line
                            buffer[cursor] = 0;
                            for ( int i = cursor+1; i % columns != 0; i++ )
                            {
                                buffer[i] = 0;
                            }
                            break;
                    }
                    break;
                
                case 'm': // set graphics mode
                    int argc = getArgumentCount();
                    for ( int i = 0; i < argc; i++ )
                    {
                        highlight = getArgument( i ) > 0;
                    }
                    break;
                
                case 'h': // set emulation option
                    System.err.println( "h: emulation mode not supported" 
                        + " : " + new String( argbuf, 1, argbuf[0] ) );
                    break;
                
                case 'l': // unset emulation option
                    System.err.println( "l: reset emulation mode not supported" 
                        + " : " + new String( argbuf, 1, argbuf[0] ) );
                    break;
                
                case 'p': // define keyboard mappings
                    System.err.println( "p: keyboard mappings not supported" 
                        + " : " + new String( argbuf, 1, argbuf[0] ) );
                    break;
                    
                default:
                    System.err.println( "unsupported command: " + (char) command 
                        + " : " + new String( argbuf, 1, argbuf[0] ) );
                    
            }
        }
        catch ( Throwable t ) 
        {
            // probably a parse exception or wrong number of args
            System.err.println( "Error in processCommand: " );
            t.printStackTrace();
        }
    }

    /**
    * Returns the argument at the specified index from 
    * the argument buffer, throwing an IndexOutOfBounds
    * if there are not enough arguments, or NumberFormatException
    * if the individual argument is not parseable as an int.
    */
    private int getArgument( int index ) 
        throws IndexOutOfBoundsException, NumberFormatException
    {
        int a = 1, b = 1; 
        int c = argbuf[0]+1;
        for ( int i = 0; i < index; i++ )
        {
            while ( argbuf[a] != ';' )
            {
                a++;
                if ( a >= c ) throw new IndexOutOfBoundsException();
            }
            a++;
        }
        b = a+1;
        while ( b < c && argbuf[b] != ';' )
        {
            b++;
        }
        return Integer.parseInt( new String( argbuf, a, b-a ) );
    }
    
    private int getArgumentCount()
    {
        if ( argbuf[0] == 0 ) return 0;
        
        int result = 1;
        for ( int i = 1; i < argbuf[0]; i++ )
        {
            if ( argbuf[i] == ';' ) result++;
        }
        return result;
    }
    
    public void paint( Graphics g )
    {
        // clear screen
        g.setGrayScale( 0 ); // black
        g.fillRect( 0, 0, getWidth(), getHeight() );
    
        // draw content from buffer
        g.setGrayScale( 255 ); // white
        g.setFont( font );
        
        int i;
        byte b;
        
        for ( int y = 0; y < rows; y++ )
        {
            for ( int x = 0; x < columns; x++ )
            {
                i = (y+scrollY)*columns+(x+scrollX);
                if ( i < buffer.length )
                {
                    b = buffer[i];
                    if ( b != 0 )
                    {
                        g.drawChar( (char) b, 
                            insetX + x*fontWidth, insetY + y*fontHeight, 
                            g.TOP | g.LEFT );
                    }
                    if ( cursor == i )
                    {
                        g.drawRect( 
                            insetX + x*fontWidth, insetY + y*fontHeight,
                            fontWidth, fontHeight );
                    }
                }
            }
        }
    }
    
    public void keyPressed( int keyCode )
    {
        switch ( getGameAction( keyCode ) )
        {
            case LEFT:
                // move cursor left one column
                move[2] = 'D';
                send( move );
                break;
            case RIGHT:
                // move cursor right one column
                move[2] = 'C';
                send( move );
                break;
            case DOWN:
                if ( scrolling )
                {
                    // scroll down one row
                    scrollY++;
                    if ( scrollY > calcLastVisibleScreen() )
                    {
                        scrollY = calcLastVisibleScreen();
                    }
                    repaint();
                }
                else 
                {
                    // move cursor down one row
                    move[2] = 'B';
                    send( move );
                }
                break;
            case UP:
                if ( scrolling )
                {
                    // scroll up one row
                    scrollY--;
                    if ( scrollY < 0 ) scrollY = 0;
                    repaint();
                }
                else 
                {
                    // move cursor down one row
                    move[2] = 'A';
                    send( move );
                }
                break;
            case FIRE:
                // send a line feed:
                send( (byte) '\n' );
                break;
            default:
                // send code directly
                send( (byte) keyCode );
        }
    }

    public void keyRepeated( int keyCode )
    {
        switch ( getGameAction( keyCode ) )
        {
            case LEFT:
                // move cursor left one column
                move[2] = 'D';
                //receive( move );
                send( move );
                repaint();
                break;
            case RIGHT:
                // move cursor right one column
                move[2] = 'C';
                //receive( move );
                send( move );
                repaint();
                break;
            case DOWN:
                if ( scrolling )
                {
                    // scroll down by half a screen
                    scrollY += rows/2;
                    if ( scrollY > calcLastVisibleScreen() )
                    {
                        scrollY = calcLastVisibleScreen();
                    }
                }
                else 
                {
                    // move cursor down one row
                    move[2] = 'B';
                    //receive( move );
                    send( move );
                }
                repaint();
                break;
            case UP:
                if ( scrolling )
                {
                    // scroll up by half a screen
                    scrollY -= rows/2;
                    if ( scrollY < 0 ) scrollY = 0;
                }
                else 
                {
                    // move cursor down one row
                    move[2] = 'A';
                    //receive( move );
                    send( move );
                }
                repaint();
                break;
            default:
                // echo to screen
                //receive( (byte) keyCode );
                // send to output
                send( (byte) keyCode );
        }
    }
    
    /**
    * For clarity: calculates the row offset so we don't
    * scroll below the most recent visible input.
    */
    private int calcLastVisibleScreen()
    {
        return Math.max( 0, bound / columns - rows );
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?