📄 plotpanel.java
字号:
gr.fillRect( rect.x, rect.y, rect.width, rect.height ); // Draw plot background gr.setColor( config.getPlotBackgroundColour( ) ); gr.fillRect( plotRect.x, plotRect.y, plotRect.width, plotRect.height ); // Draw x grid lines and scale markings boolean showGrid = config.isPlotShowGrid( ); int numFractionDigits = config.getPlotNumFractionDigits( ); IntegerRange fixedPointExponentRange = config.getPlotFixedPointExponentRange( ); boolean applyFixedExponent = !config.isPlotNormaliseScientificNotation( ); FontMetrics fontMetrics = gr.getFontMetrics( ); int fontAscent = fontMetrics.getAscent( ); int fontHeight = fontMetrics.getHeight( ); int divisionOffset = xParams.divisionOffset; int strEndX = 0; int yAxisX = -1; String prevStr = null; while ( divisionOffset < plotRect.width ) { int x = plotRect.x + divisionOffset; double value = xParams.start + (double)divisionOffset * xParams.pixelDelta; String str = PlotInterval.doubleToString( value, numFractionDigits, fixedPointExponentRange, applyFixedExponent, xParams.intervalExponent ); if ( Math.abs( value ) < 0.5 * xParams.pixelDelta ) { str = ZERO_STR; yAxisX = x; } else { if ( showGrid ) { gr.setColor( config.getPlotGridColour( ) ); gr.drawLine( x, plotRect.y, x, plotRect.y + plotRect.height - 1 ); } } gr.setColor( config.getPlotScaleColour( ) ); gr.drawLine( x, plotRect.y + plotRect.height, x, plotRect.y + plotRect.height + SCALE_LINE_LENGTH - 1 ); if ( !str.equals( prevStr ) ) { int strWidth = fontMetrics.stringWidth( str ); int strX = x - strWidth / 2; if ( strX >= strEndX ) { int strY = plotRect.y + plotRect.height + SCALE_LINE_LENGTH + X_SCALE_TOP_MARGIN + fontAscent; gr.drawString( str, strX, strY ); strEndX = strX + strWidth + X_SCALE_LEFT_MARGIN; } } prevStr = str; divisionOffset += xParams.pixelsPerDivision; } // Draw y grid lines and scale markings divisionOffset = yParams.divisionOffset; int xAxisY = -1; prevStr = null; while ( divisionOffset < plotRect.height ) { int y = plotRect.y + (plotRect.height - 1) - divisionOffset; double value = yParams.start + (double)divisionOffset * yParams.pixelDelta; String str = PlotInterval.doubleToString( value, numFractionDigits, fixedPointExponentRange, applyFixedExponent, yParams.intervalExponent ); if ( Math.abs( value ) < 0.5 * yParams.pixelDelta ) { str = ZERO_STR; xAxisY = y; } else { if ( showGrid ) { gr.setColor( config.getPlotGridColour( ) ); gr.drawLine( plotRect.x, y, plotRect.x + plotRect.width - 1, y ); } } gr.setColor( config.getPlotScaleColour( ) ); gr.drawLine( plotRect.x - SCALE_LINE_LENGTH, y, plotRect.x - 1, y ); if ( !str.equals( prevStr ) ) { String yStr = TextUtilities.getWidthLimitedString( str, fontMetrics, maxYScaleStrWidth, true ); int strX = plotRect.x - (fontMetrics.stringWidth( yStr ) + Y_SCALE_LEFT_MARGIN + SCALE_LINE_LENGTH); int strY = y + fontAscent / 2; gr.drawString( yStr, strX, strY ); } prevStr = str; divisionOffset += yParams.pixelsPerDivision; } // Draw axes gr.setColor( config.getPlotAxisColour( ) ); if ( yAxisX >= 0 ) gr.drawLine( yAxisX, plotRect.y, yAxisX, plotRect.y + plotRect.height - 1 ); if ( xAxisY >= 0 ) gr.drawLine( plotRect.x, xAxisY, plotRect.x + plotRect.width - 1, xAxisY ); // Plot functions Graphics plotGr = gr.create( plotRect.x, plotRect.y, plotRect.width, plotRect.height ); for ( int i = document.getNumFunctions( ) - 1; i >= 0; --i ) { Function function = document.getFunction( i ); if ( function.isVisible( ) ) { plotGr.setColor( function.getColour( ) ); drawExpression( plotGr, function.getExpression( ), 0, plotRect.width ); } } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public Point2D.Double pointToCoords( Point point ) { Point2D.Double coords = null; if ( (point.x >= plotRect.x) && (point.x < plotRect.x + plotRect.width) && (point.y >= plotRect.y) && (point.y < plotRect.y + plotRect.height) ) coords = new Point2D.Double( xParams.start + (double)(point.x - plotRect.x) * xParams.pixelDelta, yParams.start + (double)(plotRect.y + plotRect.height - 1 - point.y) * yParams.pixelDelta ); return coords; } //------------------------------------------------------------------ public double getXScrollIncrement( ) { return ( xParams.divisionDelta * SCROLL_FACTOR ); } //------------------------------------------------------------------ public double getXScrollIncrement( int pixels ) { return ( (double)((pixels << 1) / xParams.pixelsPerDivision) * getXScrollIncrement( ) ); } //------------------------------------------------------------------ public double getYScrollIncrement( ) { return ( yParams.divisionDelta * SCROLL_FACTOR ); } //------------------------------------------------------------------ public double getYScrollIncrement( int pixels ) { return ( (double)((pixels << 1) / yParams.pixelsPerDivision) * getYScrollIncrement( ) ); } //------------------------------------------------------------------ public void setXInterval( PlotInterval interval ) { PlotParams newXParams = new PlotParams( interval, plotRect.width ); if ( !xParams.equals( newXParams ) ) { xParams = newXParams; repaint( ); fireStateChanged( ); } } //------------------------------------------------------------------ public void setYInterval( PlotInterval interval ) { PlotParams newYParams = new PlotParams( interval, plotRect.height ); if ( !yParams.equals( newYParams ) ) { yParams = newYParams; repaint( ); fireStateChanged( ); } } //------------------------------------------------------------------ public void setIntervals( PlotInterval xInterval, PlotInterval yInterval ) { PlotParams newXParams = new PlotParams( xInterval, plotRect.width ); PlotParams newYParams = new PlotParams( yInterval, plotRect.height ); if ( !xParams.equals( newXParams ) || !yParams.equals( newYParams ) ) { xParams = newXParams; yParams = newYParams; repaint( ); fireStateChanged( ); } } //------------------------------------------------------------------ public void setCursor( boolean move ) { setCursor( Cursor.getPredefinedCursor( move ? Cursor.MOVE_CURSOR : Cursor.CROSSHAIR_CURSOR ) ); } //------------------------------------------------------------------ public void drawPanel( Graphics gr ) { gr.setFont( getFont( ) ); gr.setClip( 0, 0, panelWidth, panelHeight ); drawingImage = true; paintComponent( gr ); drawingImage = false; } //------------------------------------------------------------------ public void addChangeListener( ChangeListener listener ) { changeListeners.add( listener ); } //------------------------------------------------------------------ private void fireStateChanged( ) { for ( int i = changeListeners.size( ) - 1; i >= 0; --i ) { if ( changeEvent == null ) changeEvent = new ChangeEvent( this ); changeListeners.get( i ).stateChanged( changeEvent ); } } //------------------------------------------------------------------ private void drawExpression( Graphics gr, Expression expression, int startX, int endX ) { final long Y_LOWER_LIMIT = Integer.MIN_VALUE >> 1; final long Y_UPPER_LIMIT = Integer.MAX_VALUE >> 1; double prevX = 0.0; double prevY = Double.NaN; int prevPlotY = 0; for ( int plotX = startX - 1; plotX <= endX; ++plotX ) { double x = xParams.start + (double)plotX * xParams.pixelDelta; double y = expression.evaluate( x ); if ( !Double.isNaN( y ) ) { int plotY = (int)Math.min( Math.max( Y_LOWER_LIMIT, Math.round( (y - yParams.start) / yParams.pixelDelta ) ), Y_UPPER_LIMIT ); if ( !Double.isNaN( prevY ) ) { double medianY = expression.evaluate( 0.5 * (prevX + x) ); if ( ((medianY >= prevY) && (medianY <= y)) || ((medianY >= y) && (medianY <= prevY)) || (y == prevY) ) gr.drawLine( plotX - 1, (plotRect.height - 1) - prevPlotY, plotX, (plotRect.height - 1) - plotY ); } prevPlotY = plotY; } prevX = x; prevY = y; } } //------------------------------------------------------------------ private void setBorder( boolean focused ) { if ( focused ) setBorder( BorderFactory. createMatteBorder( borderInsets.top, borderInsets.left, borderInsets.bottom, borderInsets.right, AppConfig.getInstance( ).getPlotFocusedBorderColour( ) ) ); else GuiUtilities.setPaddedLineBorder( this, 1 ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private FunctionDocument document; private int panelWidth; private int panelHeight; private int maxYScaleStrWidth; private Insets borderInsets; private Rectangle plotRect; private PlotParams xParams; private PlotParams yParams; private List<ChangeListener> changeListeners; private ChangeEvent changeEvent; private boolean drawingImage;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -