⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 canvashandler1.java

📁 类似于Windows上的Excel
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            int nextcoord = cellcoord + getRowColumnSize( 1, i+1 );
            if( nextcoord >= windowy2 ) {
                willRepaint = true;
                dirij = CURSOR_DOWN_RIGHT;
            }
        }
        if( willRepaint ) {
            setWindowY( i, dirij );
            return 2;
        }
        // only one cell repaint
        cursory = i;
        return 1;
    }
    
    public void setCursor( int i, int j ) {
        setCursorX( j );
        setCursorY( i );
    }
    
//#ifdef MIDP
    void paintCell(Graphics g, int I, int J, boolean selected) {
//System.out.println("paintCell "+I+" "+J);        
        int x =  getScreenCellCoordX( J );
        int y =  getScreenCellCoordY( I );

        int nx = getRowColumnSize( 0, J );
        int ny = getRowColumnSize( 1, I );

        int xx = x + nx;
        int yy = y + ny;

        boolean rightLine = true;               // right line may be not drawn when the text continues to the right empty cell

//        g.clipRect( x+1, y+1, nx, ny );
        g.clipRect( x, y, nx, ny );

        g.setColor( selected ? selectionBackColor : cellBackColor );
        g.fillRect( x, y, nx, ny );     // -1 to fill in intersection point (right-bottom) by background color (visible in selected mode)

        Cell cell = getCell( I,J );
        LispObject value = cell.value;

        if ( !(value==NIL && (cell.format&Cell.MASK_FORMULA)==Cell.FORMAT_SHOWVALUE) ) {            // not empty

            g.setColor( selected ? selectionFontColor : cellFontColor );
            
            // draw the string with proper aligment
            String ss = cell.getText();
            int sswidth = font.stringWidth(ss);
            if( value.typeNumber() == TYPE_STRING ) {
                g.drawString( ss, x + 1, y + 1, Graphics.TOP | Graphics.LEFT);
                rightLine = !sheet.isEmpty(I, J + 1) || (sswidth + 3 <= nx);
            } else {
                if (sswidth + 3 > nx) {
                    ss = POUNDS;
                }
                g.drawString(ss, xx - 2, y + 1, Graphics.TOP | Graphics.RIGHT);
            }
            
        }
        // empty
        else {
            // check if there's text from the left to paint within this cell
            int width = 0;
            for (int j = J - 1; j >= 0; j--) {
                width += getRowColumnSize( 0,j );
                cell = getCell( I,j );
                value = cell.value;
                if( value != NIL ) {                       // not empty
                    if ( value.typeNumber() == TYPE_STRING ) {
                        int ww;
                        String ss = cell.getText();
                        if ((ww = font.stringWidth(ss) + 3) > width) {
                            g.setColor(selected ? selectionFontColor : cellFontColor);
                            g.drawString(ss, x + 1 - width, y + 1, Graphics.TOP | Graphics.LEFT);
                            rightLine = !sheet.isEmpty(I, J + 1) || (ww <= width + nx);
                        }
                    }
                    break;
                    // break for any non-empty cell
                }
            }
        }

        g.setColor(selected ? gridSelectedColor : gridColor);
        // right line
        if (rightLine) {
            g.drawLine(xx-1, y, xx-1, yy - 2);
        }
        // bottom line
        if( selected ) 
            // leave 1 pixel white (intersection point)
            g.drawLine(x, yy-1, xx-2, yy-1);
        else
            g.drawLine(x, yy-1, xx-1, yy-1);

    }

    // to reduce GC
    int clipx,clipy;
    int clipdx,clipdy;

//    final int XX1[] = new int[2], XX2[] = new int[2];         // pt 1 and pt2 to draw lines & rectangles
//    final int _XX1[] = new int[2], _XX2[] = new int[2];       // old positions
//        
//    final int ij1[] = new int[2];
//    final int ij2[] = new int[2];
            
    public void paint(Graphics g) {
    	
    	if (sheet==null) {
    		g.drawString("please wait",canvasWidth/2,canvasHeight/2,Graphics.BASELINE|Graphics.HCENTER);
    		return;
    	}
        
        clipx = g.getClipX();
        clipy = g.getClipY();
        
        clipdx = g.getClipWidth();
        clipdy = g.getClipHeight();

//        g.setClip(0,0,canvas.getWidth(),canvas.getHeight());
        
        g.setColor(cellBackColor);
        g.fillRect(0, 0, canvasWidth, canvasHeight);
        
        g.setFont(font);
        
        /*===========================================
         * Column header
         */
        
        // will draw column/row names?
        boolean flagColumns = Reference.intersect(
            clipx, clipy,  clipx+clipdx, clipy+clipdy,
            headerWidth, 0, canvasWidth-1, headerHeight-1 );

        // redraw area in abs coords
        int wX1 = windowx1 + (clipx - headerWidth)-1;
        int wX2 = wX1 + clipdx;

        if( flagColumns ) {
            wX1 = windowx1;
        }

        // find boundaries i,j
        int j1 = -1;
        int j2 = sheet.visibleX - 1;
        int nn;

        int X = 0;
        for (int ij = 0; ij < sheet.visibleX; ij++) {
            if (X >= wX2) {
                j2 = ij - 1;
                break;
            }
            nn = getRowColumnSize( 0, ij );  // from pts to pxls
            if (j1 == -1 && (X >= wX1 || X + nn > wX1)) {
                j1 = ij;
            }
            X += nn;
        }
            
            
        // draw the header if it's not clipped
        if( flagColumns ) {

            g.setColor(headerBackColor);
            g.fillRect(headerWidth,0,canvasWidth-1,headerHeight-1);
            
            g.setColor(gridColor);
            g.drawLine(headerWidth, headerHeight, canvasWidth, headerHeight);

            g.setColor(headerFontColor);
            if( goldmode ) {
                StringBuffer sb = new StringBuffer( "GOLD? " );
                for( int i=0; i<ngoldkeys; i++ ) {
                    if( i != 0 )
                        sb.append( '-' );
                    sb.append( canvas.getKeyName( goldkeys[i] ) );
                }
                g.drawString( sb.toString(), 0, 0, Graphics.TOP | Graphics.LEFT );
            }
            else if( screenmode == MODE_NUMERIC ) {
                g.drawString( "NUM: "+formula, 0, 0, Graphics.TOP | Graphics.LEFT );
            }
            else if( screenmode == MODE_FORMULA ) {
                StringBuffer sb = new StringBuffer();
                sb.append( '[' );
                sb.append( Reference.cellAddress( cursory, cursorx, 0 ) );
                sb.append( "]: " );
                Cell cell = getCell( cursory, cursorx );
                sb.append( cell.formula.toFormulaString() );
                g.drawString( sb.toString(), 0, 0, Graphics.TOP | Graphics.LEFT );
            }
            else {

                g.setClip( headerWidth, 0, canvasWidth-headerWidth, headerHeight );

                int scale = getScreenCellCoordX( j1 )-1; // from pts to pxls
                g.setColor(gridColor);
                g.drawLine( scale, 0, scale, headerHeight );

                for (int ij = j1; ij <= j2; ij++) {
                    int scale1 = scale + getRowColumnSize( 0, ij );  // from pts to pxls
                    g.setColor(gridColor);
                    g.drawLine( scale1, 0, scale1, headerHeight );
                    g.setColor(headerFontColor);
                    g.drawString( 
                        sheet.getRowColumn( 0, ij ).getName( 0 ), 
                        (scale + scale1) / 2, 
                        headerHeight-1,
                        Graphics.BOTTOM | Graphics.HCENTER );
                    scale = scale1;
                }

                g.setClip( 0, 0, canvasWidth, canvasHeight );
            }

        }
            

        /*==============================================
         * Row headers
         */
        
        // will draw column/row names?
        boolean flagRows = Reference.intersect(
            clipx, clipy,  clipx+clipdx, clipy+clipdy,
            0, headerHeight, headerWidth-1, canvasHeight-1 );

        // redraw area in abs coords
        int wY1 = windowy1 + (clipy - headerHeight)-1;
        int wY2 = wY1 + clipdy;

        if( flagRows ) {
            wY1 = windowy1;
        }

        // find boundaries i,j
        int i1 = -1;
        int i2 = sheet.visibleY - 1;

        int Y = 0;
        for (int ij = 0; ij < sheet.visibleY; ij++) {
            if (Y >= wY2) {
                i2 = ij - 1;
                break;
            }
            nn = getRowColumnSize( 1, ij );  // from pts to pxls
            if (i1 == -1 && (Y >= wY1 || Y + nn > wY1)) {
                i1 = ij;
            }
            Y += nn;
        }
            
        // draw the header if it's not clipped
        if( flagRows ) {

            g.setColor(headerBackColor);
            g.fillRect(0,headerHeight,headerWidth-1,canvasHeight-1);
            
            g.setColor(gridColor);
            g.drawLine( headerWidth, headerHeight, headerWidth, canvasHeight );

            g.setClip( 0, headerHeight, headerWidth, canvasHeight-headerHeight );

            int scale = getScreenCellCoordY( i1 )-1; // from pts to pxls
            g.drawLine( 0, scale, headerWidth, scale );

            for (int ij = i1; ij <= i2; ij++) {
                int scale1 = scale + getRowColumnSize( 1, ij );  // from pts to pxls
                g.setColor(gridColor);
                g.drawLine( 0, scale1, headerWidth, scale1 );
                g.setColor(headerFontColor);
                g.drawString( 
                    sheet.getRowColumn( 1, ij ).getName( 1 ), 
                    headerWidth-1,
                    scale1-1, 
                    Graphics.BOTTOM | Graphics.RIGHT );
                scale = scale1;
            }

            g.setClip( 0, 0, canvasWidth, canvasHeight );

        }
        
        boolean drawCells = Reference.intersect(
                clipx, clipy, clipx + clipdx, clipy + clipdy,
                headerWidth+1, headerHeight+1, canvasWidth, canvasHeight );

        if( drawCells ) {
            g.setClip( headerWidth+1, headerHeight+1, canvasWidth-headerWidth-1, canvasHeight-headerHeight-1 );
            for (int i = i1; i <= i2; i++) {
                for (int j = j2; j >= j1; j--) {
                    // to intersect with in clipRect()
                    boolean active = 
                        i == cursory && j == cursorx;
                    boolean selected = 
                        j >= selectionx1 && j <= selectionx2 &&
                        i >= selectiony1 && i <= selectiony2;
                    g.setClip( headerWidth+1, headerHeight+1, canvasWidth-headerWidth, canvasHeight-headerHeight );
                    paintCell( g, i, j, active ^ selected );
                }
            }
        }
    }
//#endif
    
// from pts to pxls, in abs coordinates (not screen)
    int getRowColumnSize( int axis, int ij ) {
        return sheet.getRowColumn( axis, ij ).width_height * fontSize[axis] / Sheet.fontDivider;
    }
    
    // in abs coordinates (not screen)
    int getCellCoord( int axis, int ij ) {
        return sheet.coord( axis, ij ) * fontSize[axis] / Sheet.fontDivider;
    }

    int getScreenCellCoordX( int j ) {
        return getCellCoord( 0, j ) - windowx1 + headerWidth+1;
    }
    int getScreenCellCoordY( int  i ) {
        return getCellCoord( 1, i ) - windowy1 + headerHeight+1;
    }
    
    Cell getCell( int i, int j ) {
        return sheet.getCell( i,j );
    }
        
    LispObject getCellValue( int i, int j ) {
        return sheet.getCellValue( i,j );
    }
    
    /**
     * Finds row/column by screen coordinate
     * @param axis 0/1 x/y
     * @param xy screen coordinate
     * @param maxvalue max returned value (limit for row/column values)
     * @return cell's row/column
     */
    public int findCellByCoord( int axis, int xy ) {
        int Y = 0;
        xy += axis == Sheet.AXIS_X ? windowx1-headerWidth : windowy1-headerHeight;
        xy = (xy*Sheet.fontDivider)/fontSize[axis];
        Hashtable tb = axis == Sheet.AXIS_X ? sheet.columns : sheet.rows;
        int default_size = sheet.defaultWidthHeight[axis];
        int max_rowcol = axis == Sheet.AXIS_X ? sheet.visibleX : sheet.visibleY;
        for( int i=0; i<max_rowcol; i++ ) {
            RowColumn rc = (RowColumn) tb.get( new Integer(i) );
            if( rc != null )
                Y += rc.width_height;
            else
                Y += default_size;
            if(xy<Y)
                return i;
        }
        return max_rowcol;
    }
    
    void sheetInitialize() {
        setWindowX( 0, CURSOR_DOWN_RIGHT );
        setWindowY( 0, CURSOR_DOWN_RIGHT );

        setCursorX( 0 );
        setCursorY( 0 );
        
        selectionx1 = selectiony1 = 1000;
        selectionx2 = selectiony2 = -1;
    }
    
    static void setDisplay( Displayable disp ) {
//#ifdef MIDP
        canvasHandler.display.setCurrent( disp );
//#endif        
    }
        
    static void setCanvas() {
//#ifdef MIDP
        canvasHandler.display.setCurrent( canvasHandler.canvas );
//#endif        
    }
    
    String getAppProperty( String key ) {
        String ss = midlet.getAppProperty(key);
//#ifdef DESKTOP
    	if(ss == null)
    		return System.getProperty(key);
//#endif
        return ss;
    }
    
}

⌨️ 快捷键说明

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