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

📄 functionview.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        public void mousePressed( MouseEvent event )        {            super.mousePressed( event );            int rowHeight = getRowHeight( );            int index = event.getY( ) / rowHeight;            if ( SwingUtilities.isLeftMouseButton( event ) && (index >= 0) && (index < getNumElements( )) )            {                String str = getElement( index ).toString( );                if ( getFontMetrics( getFont( ) ).stringWidth( str ) > getColumns( ) * getColumnWidth( ) )                {                    PopUpComponent popUpComponent = new PopUpComponent( str, rowHeight + 2 );                    int popUpWidth = popUpComponent.getPreferredSize( ).width;                    boolean leftToRight = getComponentOrientation( ).isLeftToRight( );                    int x = leftToRight                                ? HORIZONTAL_MARGIN + FunctionListItemIcon.WIDTH                                : getWidth( ) - popUpWidth - HORIZONTAL_MARGIN - FunctionListItemIcon.WIDTH;                    Point location = new Point( x, index * rowHeight - 1 );                    SwingUtilities.convertPointToScreen( location, this );                    Rectangle screen = GuiUtilities.getScreenBounds( this );                    x = leftToRight ? Math.min( location.x, screen.x + screen.width - popUpWidth )                                    : Math.max( screen.x, location.x );                    popUp = PopupFactory.getSharedInstance( ).getPopup( this, popUpComponent, x,                                                                        location.y );                    popUp.show( );                }            }        }        //--------------------------------------------------------------        public void mouseReleased( MouseEvent event )        {            super.mouseReleased( event );            if ( SwingUtilities.isLeftMouseButton( event ) )                hidePopUp( );        }        //--------------------------------------------------------------        public void mouseDragged( MouseEvent event )        {            super.mouseDragged( event );            if ( isDragging( ) )                hidePopUp( );        }        //--------------------------------------------------------------        protected void drawElement( Graphics gr,                                    int      index )        {            // Get function            Function function = getElement( index );            // Draw icon            boolean leftToRight = getComponentOrientation( ).isLeftToRight( );            Icon icon = new FunctionListItemIcon( function.getColour( ), function.isVisible( ) );            int x = leftToRight ? getHorizontalMargin( )                                : getWidth( ) - getHorizontalMargin( ) - icon.getIconWidth( );            int rowHeight = getRowHeight( );            int rowY = index * rowHeight;            int y = rowY + (rowHeight - icon.getIconHeight( )) / 2;            icon.paintIcon( this, gr, x, y );            // Draw text            String text = function.toString( );            FontMetrics fontMetrics = gr.getFontMetrics( );            int textWidth = fontMetrics.stringWidth( text );            int maxTextWidth = getColumns( ) * getColumnWidth( );            if ( textWidth > maxTextWidth )            {                maxTextWidth -= fontMetrics.stringWidth( AppConstants.ELLIPSIS_STR );                char[] chars = text.toCharArray( );                int length = chars.length;                while ( (length > 0) && (textWidth > maxTextWidth) )                    textWidth -= fontMetrics.charWidth( chars[--length] );                text = new String( chars, 0, length ) + AppConstants.ELLIPSIS_STR;            }            x += leftToRight ? icon.getIconWidth( ) + ICON_TEXT_GAP                             : -(ICON_TEXT_GAP + fontMetrics.stringWidth( text ));            y = rowY + (rowHeight - fontMetrics.getHeight( ) + 1) / 2 + fontMetrics.getAscent( );            gr.setColor( getForeground( ) );            gr.drawString( text, x, y );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods    ////////////////////////////////////////////////////////////////////        private void hidePopUp( )        {            if ( popUp != null )            {                popUp.hide( );                popUp = null;            }        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private Popup   popUp;    }    //==================================================================    // FUNCTION LIST ITEM ICON CLASS    private static class FunctionListItemIcon        implements Icon    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    int WIDTH   = 20;        private static final    int HEIGHT  = 12;        private static final    Color   BORDER_COLOUR   = Color.BLACK;    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private FunctionListItemIcon( Color   colour,                                      boolean visible )        {            this.colour = colour;            this.visible = visible;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : Icon interface    ////////////////////////////////////////////////////////////////////        public int getIconWidth( )        {            return WIDTH;        }        //--------------------------------------------------------------        public int getIconHeight( )        {            return HEIGHT;        }        //--------------------------------------------------------------        public void paintIcon( Component component,                               Graphics  gr,                               int       x,                               int       y )        {            if ( visible )            {                gr.setColor( BORDER_COLOUR );                gr.drawRect( x, y, WIDTH - 1, HEIGHT - 1 );                gr.setColor( colour );                gr.fillRect( x + 1, y + 1, WIDTH - 2, HEIGHT - 2 );            }        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private Color   colour;        private boolean visible;    }    //==================================================================    // FUNCTION BUTTON CLASS    private static class FunctionButton        extends JButton    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    Insets  APPLICATION_MARGINS = new Insets( 2, 8, 2, 8 );        private static final    Insets  APPLET_MARGINS      = new Insets( 2, 6, 2, 6 );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private FunctionButton( Action action )        {            super( action );            AppFont.MAIN.setFont( this );            setMargin( App.getInstance( ).isApplet( ) ? APPLET_MARGINS : APPLICATION_MARGINS );            if ( instances == null )                instances = new ArrayList<FunctionButton>( );            instances.add( this );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Class methods    ////////////////////////////////////////////////////////////////////        public static void reset( )        {            if ( instances != null )                instances.clear( );        }        //--------------------------------------------------------------        public static void updateWidth( )        {            int maxWidth = 0;            for ( FunctionButton button : instances )            {                int width = button.getPreferredSize( ).width;                if ( maxWidth < width )                    maxWidth = width;            }            for ( FunctionButton button : instances )                button.setPreferredSize( new Dimension( maxWidth, button.getPreferredSize( ).height ) );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Class variables    ////////////////////////////////////////////////////////////////////        private static  List<FunctionButton>    instances;    }    //==================================================================    // SCROLL BUTTON CLASS    private static class ScrollButton        extends JButton    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    Insets  MARGINS = new Insets( 1, 1, 1, 1 );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ScrollButton( Action action )        {            super( action );            setMargin( MARGINS );        }        //--------------------------------------------------------------    }    //==================================================================    // ZOOM BUTTON CLASS    private static class ZoomButton        extends JButton    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    Insets  MARGINS = new Insets( 1, 1, 1, 1 );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ZoomButton( Action action )        {            super( action );            setMargin( MARGINS );        }        //--------------------------------------------------------------    }    //==================================================================    // COORDINATES FIELD CLASS    private static class CoordinatesField        extends JComponent    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    int NUM_ROWS    = 2;        private static final    int TOP_MARGIN      = 3;        private static final    int BOTTOM_MARGIN   = TOP_MARGIN;        private static final    int LEADING_MARGIN  = 5;        private static final    int TRAILING_MARGIN = LEADING_MARGIN;        private static final    int GAP             = 2 * (LEADING_MARGIN - 1);        private static final    Color   BACKGROUND_COLOUR       = new Color( 208, 224, 224 );        private static final    Color   FOREGROUND_COLOUR       = Color.BLACK;        private static final    Color   X_Y_BACKGROUND_COLOUR   = new Color( 192, 208, 208 );        private static final    Color   BORDER_COLOUR           = GuiUtilities.LINE_BORDER_COLOUR;        private static final    String  BASE_STR    = "-.E-000";    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private CoordinatesField( )        {            AppFont.TEXT_FIELD.setFont( this );            FontMetrics fontMetrics = getFontMetrics( getFont( ) );            charWidth = Math.max( GuiUtilities.getCharWidth( 'x', fontMetrics ),                                  GuiUtilities.getCharWidth( 'y', fontMetrics ) );            char[] zeros = Util.createCharArray( '0', PlotInterval.MAX_NUM_SIGNIFICANT_DIGITS );            int maxStrWidth = fontMetrics.stringWidth( BASE_STR ) +                                                        fontMetrics.charsWidth( zeros, 0, zeros.length );            width = LEADING_MARGIN + charWidth + GAP + maxStrWidth + TRAILING_MARGIN;            height = TOP_MARGIN + NUM_ROWS * fontMetrics.getHeight( ) + BOTTOM_MARGIN;            setOpaque( true );        }        //--------------------------------------------------------------

⌨️ 快捷键说明

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