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

📄 functionview.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*====================================================================*\FunctionView.javaFunction view class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program.  If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// IMPORTSimport exception.AppException;import gui.GuiUtilities;import gui.SingleSelectionList;import java.awt.Color;import java.awt.Component;import java.awt.ComponentOrientation;import java.awt.Dimension;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.awt.geom.Point2D;import java.lang.reflect.InvocationTargetException;import java.math.BigDecimal;import java.util.ArrayList;import java.util.List;import javax.swing.Action;import javax.swing.BorderFactory;import javax.swing.Icon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.KeyStroke;import javax.swing.Popup;import javax.swing.PopupFactory;import javax.swing.SwingUtilities;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import textfield.ConstrainedTextField;import textfield.TextFieldConstants;import util.InputModifiers;import util.KeyAction;//----------------------------------------------------------------------// FUNCTION VIEW CLASSclass FunctionView    extends JPanel    implements ActionListener, ChangeListener, ListSelectionListener, MouseListener, MouseMotionListener,               MouseWheelListener{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    public static final     int MIN_FUNCTION_LIST_NUM_COLUMNS   = 8;    public static final     int MAX_FUNCTION_LIST_NUM_COLUMNS   = 80;    public static final     int MIN_FUNCTION_LIST_NUM_ROWS  = 1;    public static final     int MAX_FUNCTION_LIST_NUM_ROWS  = FunctionDocument.MAX_NUM_FUNCTIONS;    private static final    int INTERVAL_EP_NUM_COLUMNS = 22;    private static final    String  CURSOR_STR      = "Cursor:";    private static final    String  X_INTERVAL_STR  = "x:";    private static final    String  Y_INTERVAL_STR  = "y:";    private static final    String  TO_STR          = "to";    private static final    String  X_ZOOM_STR      = "x zoom:";    private static final    String  Y_ZOOM_STR      = "y zoom:";    private static final    String[]    ZOOM_FACTOR_STRS    =    {        "1.1", "1.2", "1.25", "1.5", "2", "3", "4", "5", "10"    };    private static final    String  DEFAULT_ZOOM_FACTOR_STR = "1.5";    // Commands    private interface Command    {        String  EDIT_FUNCTION           = "editFunction";        String  DELETE_FUNCTION         = "deleteFunction";        String  CONFIRM_DELETE_FUNCTION = "confirmDeleteFunction";        String  MOVE_FUNCTION           = "moveFunction";        String  SET_X_INTERVAL          = "setXInterval";        String  SET_Y_INTERVAL          = "setYInterval";        String  SELECT_X_ZOOM_FACTOR    = "selectXZoomFactor";        String  SELECT_Y_ZOOM_FACTOR    = "selectYZoomFactor";        String  SHOW_POP_UP_MENU        = "showPopUpMenu";    }    private static final    String[]    COMMAND_MAP =    {        SingleSelectionList.Command.EDIT_ELEMENT,       Command.EDIT_FUNCTION,        SingleSelectionList.Command.DELETE_ELEMENT,     Command.CONFIRM_DELETE_FUNCTION,        SingleSelectionList.Command.DELETE_EX_ELEMENT,  Command.DELETE_FUNCTION,        SingleSelectionList.Command.DRAG_ELEMENT,       Command.MOVE_FUNCTION    };    // Key actions    private static final    KeyAction.ActionMap[]   APPLET_VIEW_KEY_ACTIONS =    {        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK ),                                 FunctionDocument.Command.UNDO ),        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK ),                                 FunctionDocument.Command.REDO ),    };    private static final    KeyAction.ActionMap[]   PLOT_PANEL_KEY_ACTIONS  =    {        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, 0 ),                                 FunctionDocument.Command.SCROLL_LEFT ),        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, 0 ),                                 FunctionDocument.Command.SCROLL_RIGHT ),        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, 0 ),                                 FunctionDocument.Command.SCROLL_DOWN ),        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_UP, 0 ),                                 FunctionDocument.Command.SCROLL_UP ),        new KeyAction.ActionMap( KeyStroke.getKeyStroke( KeyEvent.VK_HOME, 0 ),                                 FunctionDocument.Command.CENTRE_ON_ORIGIN )    };//////////////////////////////////////////////////////////////////////////  Enumeration types////////////////////////////////////////////////////////////////////////    // ERROR IDENTIFIERS    private enum ErrorId        implements AppException.Id    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        INVALID_X_LOWER_ENDPOINT        ( "The lower endpoint of the x interval is invalid." ),        X_LOWER_ENDPOINT_OUT_OF_BOUNDS        ( "The lower endpoint of the x interval must be between " + PlotInterval.MIN_VALUE + " and " +            PlotInterval.MAX_VALUE + "." ),        X_LOWER_ENDPOINT_HAS_TOO_MANY_SIGNIFICANT_DIGITS        ( "The lower endpoint of the x interval must not have more than " +            PlotInterval.MAX_NUM_SIGNIFICANT_DIGITS + " significant digits." ),        INVALID_X_UPPER_ENDPOINT        ( "The upper endpoint of the x interval is invalid." ),        X_UPPER_ENDPOINT_OUT_OF_BOUNDS        ( "The upper endpoint of the x interval must be between " + PlotInterval.MIN_VALUE + " and " +            PlotInterval.MAX_VALUE + "." ),        X_UPPER_ENDPOINT_HAS_TOO_MANY_SIGNIFICANT_DIGITS        ( "The upper endpoint of the x interval must not have more than " +            PlotInterval.MAX_NUM_SIGNIFICANT_DIGITS + " significant digits." ),        X_ENDPOINTS_OUT_OF_ORDER        ( "The upper endpoint of the x interval is less than or equal to the lower endpoint." ),        INVALID_Y_LOWER_ENDPOINT        ( "The lower endpoint of the y interval is invalid." ),        Y_LOWER_ENDPOINT_OUT_OF_BOUNDS        ( "The lower endpoint of the y interval must be between " + PlotInterval.MIN_VALUE + " and " +            PlotInterval.MAX_VALUE + "." ),        Y_LOWER_ENDPOINT_HAS_TOO_MANY_SIGNIFICANT_DIGITS        ( "The lower endpoint of the y interval must not have more than " +            PlotInterval.MAX_NUM_SIGNIFICANT_DIGITS + " significant digits." ),        INVALID_Y_UPPER_ENDPOINT        ( "The upper endpoint of the y interval is invalid." ),        Y_UPPER_ENDPOINT_OUT_OF_BOUNDS        ( "The upper endpoint of the y interval must be between " + PlotInterval.MIN_VALUE + " and " +            PlotInterval.MAX_VALUE + "." ),        Y_UPPER_ENDPOINT_HAS_TOO_MANY_SIGNIFICANT_DIGITS        ( "The upper endpoint of the y interval must not have more than " +            PlotInterval.MAX_NUM_SIGNIFICANT_DIGITS + " significant digits." ),        Y_ENDPOINTS_OUT_OF_ORDER        ( "The upper endpoint of the y interval is less than or equal to the lower endpoint." );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ErrorId( String message )        {            this.message = message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : AppException.Id interface    ////////////////////////////////////////////////////////////////////        public String getMessage( )        {            return message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private String  message;    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Member classes : non-inner classes////////////////////////////////////////////////////////////////////////    // FUNCTION LIST CLASS    public static class FunctionList        extends SingleSelectionList<Function>    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    int VERTICAL_MARGIN     = 1;        private static final    int HORIZONTAL_MARGIN   = 3;        private static final    int ICON_TEXT_GAP       = 4;    ////////////////////////////////////////////////////////////////////    //  Member classes : non-inner classes    ////////////////////////////////////////////////////////////////////        // POP-UP COMPONENT CLASS        private static class PopUpComponent            extends JComponent        {        ////////////////////////////////////////////////////////////////        //  Constants        ////////////////////////////////////////////////////////////////            private static final    int HORIZONTAL_MARGIN   = ICON_TEXT_GAP;            private static final    Color   BACKGROUND_COLOUR   = new Color( 248, 212, 128 );            private static final    Color   TEXT_COLOUR         = Color.BLACK;            private static final    Color   BORDER_COLOUR       = GuiUtilities.LINE_BORDER_COLOUR;        ////////////////////////////////////////////////////////////////        //  Constructors        ////////////////////////////////////////////////////////////////            private PopUpComponent( String text,                                    int    height )            {                this.text = text;                this.height = height;                AppFont.MAIN.setFont( this );                width = 2 * HORIZONTAL_MARGIN + getFontMetrics( getFont( ) ).stringWidth( text );            }            //----------------------------------------------------------        ////////////////////////////////////////////////////////////////        //  Instance methods : overriding methods        ////////////////////////////////////////////////////////////////            public Dimension getPreferredSize( )            {                return new Dimension( width, height );            }            //----------------------------------------------------------            protected void paintComponent( Graphics gr )            {                // Fill interior                gr.setColor( BACKGROUND_COLOUR );                gr.fillRect( 0, 0, width, height );                // Draw text                FontMetrics fontMetrics = gr.getFontMetrics( );                gr.setColor( TEXT_COLOUR );                gr.drawString( text, HORIZONTAL_MARGIN,                               (height - fontMetrics.getHeight( ) + 1) / 2 + fontMetrics.getAscent( ) );                // Draw border                gr.setColor( BORDER_COLOUR );                gr.drawRect( 0, 0, width - 1, height - 1 );            }            //----------------------------------------------------------        ////////////////////////////////////////////////////////////////        //  Instance variables        ////////////////////////////////////////////////////////////////            private String  text;            private int     width;            private int     height;        }        //==============================================================    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private FunctionList( FunctionDocument document,                              int              viewableColumns,                              int              viewableRows )        {            super( viewableColumns, viewableRows, AppFont.MAIN.getFont( ), document );            setRowHeight( 2 * VERTICAL_MARGIN + Math.max( getFontMetrics( getFont( ) ).getHeight( ),                                                          FunctionListItemIcon.HEIGHT ) );            setExtraWidth( FunctionListItemIcon.WIDTH + ICON_TEXT_GAP );            setHorizontalMargin( HORIZONTAL_MARGIN );        }

⌨️ 快捷键说明

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