chartpanel.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,880 行 · 第 1/5 页

JAVA
1,880
字号
    /**     * Returns the flag that controls whether or not the offscreen buffer     * needs to be refreshed.     *      * @return A boolean.     */    public boolean getRefreshBuffer() {        return this.refreshBuffer;    }        /**     * Sets the refresh buffer flag.  This flag is used to avoid unnecessary     * redrawing of the chart when the offscreen image buffer is used.     *     * @param flag  <code>true</code> indicate, that the buffer should be      *              refreshed.     */    public void setRefreshBuffer(boolean flag) {        this.refreshBuffer = flag;    }    /**     * Paints the component by drawing the chart to fill the entire component,     * but allowing for the insets (which will be non-zero if a border has been     * set for this component).  To increase performance (at the expense of     * memory), an off-screen buffer image can be used.     *     * @param g  the graphics device for drawing on.     */    public void paintComponent(Graphics g) {        super.paintComponent(g);        if (this.chart == null) {            return;        }        Graphics2D g2 = (Graphics2D) g.create();        // first determine the size of the chart rendering area...        Dimension size = getSize();        Insets insets = getInsets();        Rectangle2D available = new Rectangle2D.Double(            insets.left, insets.top,            size.getWidth() - insets.left - insets.right,            size.getHeight() - insets.top - insets.bottom        );        // work out if scaling is required...        boolean scale = false;        double drawWidth = available.getWidth();        double drawHeight = available.getHeight();        this.scaleX = 1.0;        this.scaleY = 1.0;        if (drawWidth < this.minimumDrawWidth) {            this.scaleX = drawWidth / this.minimumDrawWidth;            drawWidth = this.minimumDrawWidth;            scale = true;        }        else if (drawWidth > this.maximumDrawWidth) {            this.scaleX = drawWidth / this.maximumDrawWidth;            drawWidth = this.maximumDrawWidth;            scale = true;        }        if (drawHeight < this.minimumDrawHeight) {            this.scaleY = drawHeight / this.minimumDrawHeight;            drawHeight = this.minimumDrawHeight;            scale = true;        }        else if (drawHeight > this.maximumDrawHeight) {            this.scaleY = drawHeight / this.maximumDrawHeight;            drawHeight = this.maximumDrawHeight;            scale = true;        }        Rectangle2D chartArea = new Rectangle2D.Double(            0.0, 0.0, drawWidth, drawHeight        );        // are we using the chart buffer?        if (this.useBuffer) {            // do we need to resize the buffer?            if ((this.chartBuffer == null)                     || (this.chartBufferWidth != available.getWidth())                    || (this.chartBufferHeight != available.getHeight())            ) {                this.chartBufferWidth = (int) available.getWidth();                this.chartBufferHeight = (int) available.getHeight();                this.chartBuffer = createImage(                    this.chartBufferWidth, this.chartBufferHeight                );                this.refreshBuffer = true;            }            // do we need to redraw the buffer?            if (this.refreshBuffer) {                Rectangle2D bufferArea = new Rectangle2D.Double(                    0, 0, this.chartBufferWidth, this.chartBufferHeight                );                Graphics2D bufferG2                     = (Graphics2D) this.chartBuffer.getGraphics();                if (scale) {                    AffineTransform saved = bufferG2.getTransform();                    AffineTransform st = AffineTransform.getScaleInstance(                        this.scaleX, this.scaleY                    );                    bufferG2.transform(st);                    this.chart.draw(                        bufferG2, chartArea, this.anchor, this.info                    );                    bufferG2.setTransform(saved);                }                else {                    this.chart.draw(                        bufferG2, bufferArea, this.anchor, this.info                    );                }                this.refreshBuffer = false;            }            // zap the buffer onto the panel...            g2.drawImage(this.chartBuffer, insets.left, insets.right, this);        }        // or redrawing the chart every time...        else {            AffineTransform saved = g2.getTransform();            g2.translate(insets.left, insets.top);            if (scale) {                AffineTransform st = AffineTransform.getScaleInstance(                    this.scaleX, this.scaleY                );                g2.transform(st);            }            this.chart.draw(g2, chartArea, this.anchor, this.info);            g2.setTransform(saved);        }        this.anchor = null;        this.verticalTraceLine = null;        this.horizontalTraceLine = null;    }    /**     * Receives notification of changes to the chart, and redraws the chart.     *     * @param event  details of the chart change event.     */    public void chartChanged(ChartChangeEvent event) {        this.refreshBuffer = true;        repaint();    }    /**     * Receives notification of a chart progress event.     *     * @param event  the event.     */    public void chartProgress(ChartProgressEvent event) {        // does nothing - override if necessary    }    /**     * Handles action events generated by the popup menu.     *     * @param event  the event.     */    public void actionPerformed(ActionEvent event) {        String command = event.getActionCommand();        if (command.equals(PROPERTIES_COMMAND)) {            attemptEditChartProperties();        }        else if (command.equals(SAVE_COMMAND)) {            try {                doSaveAs();            }            catch (IOException e) {                e.printStackTrace();            }        }        else if (command.equals(PRINT_COMMAND)) {            createChartPrintJob();        }        else if (command.equals(ZOOM_IN_BOTH_COMMAND)) {            zoomInBoth(this.zoomPoint.getX(), this.zoomPoint.getY());        }        else if (command.equals(ZOOM_IN_DOMAIN_COMMAND)) {            zoomInDomain(this.zoomPoint.getX(), this.zoomPoint.getY());        }        else if (command.equals(ZOOM_IN_RANGE_COMMAND)) {            zoomInRange(this.zoomPoint.getX(), this.zoomPoint.getY());        }        else if (command.equals(ZOOM_OUT_BOTH_COMMAND)) {            zoomOutBoth(this.zoomPoint.getX(), this.zoomPoint.getY());        }        else if (command.equals(ZOOM_OUT_DOMAIN_COMMAND)) {            zoomOutDomain(this.zoomPoint.getX(), this.zoomPoint.getY());        }        else if (command.equals(ZOOM_OUT_RANGE_COMMAND)) {            zoomOutRange(this.zoomPoint.getX(), this.zoomPoint.getY());        }        else if (command.equals(ZOOM_RESET_BOTH_COMMAND)) {            restoreAutoBounds();        }        else if (command.equals(ZOOM_RESET_DOMAIN_COMMAND)) {            restoreAutoDomainBounds();        }        else if (command.equals(ZOOM_RESET_RANGE_COMMAND)) {            restoreAutoRangeBounds();        }    }    /**     * Handles a 'mouse entered' event. This method changes the tooltip delays     * of ToolTipManager.sharedInstance() to the possibly different values set      * for this chart panel.      *     * @param e  the mouse event.     */    public void mouseEntered(MouseEvent e) {        if (!this.ownToolTipDelaysActive) {            ToolTipManager ttm = ToolTipManager.sharedInstance();                        this.originalToolTipInitialDelay = ttm.getInitialDelay();            ttm.setInitialDelay(this.ownToolTipInitialDelay);                this.originalToolTipReshowDelay = ttm.getReshowDelay();            ttm.setReshowDelay(this.ownToolTipReshowDelay);                        this.originalToolTipDismissDelay = ttm.getDismissDelay();            ttm.setDismissDelay(this.ownToolTipDismissDelay);                this.ownToolTipDelaysActive = true;        }    }    /**     * Handles a 'mouse exited' event. This method resets the tooltip delays of     * ToolTipManager.sharedInstance() to their     * original values in effect before mouseEntered()     *     * @param e  the mouse event.     */    public void mouseExited(MouseEvent e) {        if (this.ownToolTipDelaysActive) {            // restore original tooltip dealys             ToolTipManager ttm = ToolTipManager.sharedInstance();                   ttm.setInitialDelay(this.originalToolTipInitialDelay);            ttm.setReshowDelay(this.originalToolTipReshowDelay);            ttm.setDismissDelay(this.originalToolTipDismissDelay);            this.ownToolTipDelaysActive = false;        }    }    /**     * Handles a 'mouse pressed' event.     * <P>     * This event is the popup trigger on Unix/Linux.  For Windows, the popup     * trigger is the 'mouse released' event.     *     * @param e  The mouse event.     */    public void mousePressed(MouseEvent e) {        if (this.zoomRectangle == null) {            Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());            if (screenDataArea != null) {                this.zoomPoint = getPointInRectangle(                    e.getX(), e.getY(), screenDataArea                );            }            else {                this.zoomPoint = null;            }            if (e.isPopupTrigger()) {                if (this.popup != null) {                    displayPopupMenu(e.getX(), e.getY());                }            }        }    }        /**     * Returns a point based on (x, y) but constrained to be within the bounds     * of the given rectangle.  This method could be moved to JCommon.     *      * @param x  the x-coordinate.     * @param y  the y-coordinate.     * @param area  the rectangle (<code>null</code> not permitted).     *      * @return A point within the rectangle.     */    private Point getPointInRectangle(int x, int y, Rectangle2D area) {        x = (int) Math.max(            Math.ceil(area.getMinX()), Math.min(x, Math.floor(area.getMaxX()))        );           y = (int) Math.max(            Math.ceil(area.getMinY()), Math.min(y, Math.floor(area.getMaxY()))        );        return new Point(x, y);    }    /**     * Handles a 'mouse dragged' event.     *     * @param e  the mouse event.     */    public void mouseDragged(MouseEvent e) {        // if the popup menu has already been triggered, then ignore dragging...        if (this.popup != null && this.popup.isShowing()) {            return;        }        // if no initial zoom point was set, ignore dragging...        if (this.zoomPoint == null) {            return;        }        Graphics2D g2 = (Graphics2D) getGraphics();        // use XOR to erase the previous zoom rectangle (if any)...        g2.setXORMode(java.awt.Color.gray);        if (this.zoomRectangle != null) {            if (this.fillZoomRectangle) {                g2.fill(this.zoomRectangle);            }            else {                g2.draw(this.zoomRectangle);            }        }        boolean hZoom = false;        boolean vZoom = false;        if (this.orientation == PlotOrientation.HORIZONTAL) {            hZoom = this.rangeZoomable;            vZoom = this.domainZoomable;        }        else {            hZoom = this.domainZoomable;                          vZoom = this.rangeZoomable;        }        Rectangle2D scaledDataArea = getScreenDataArea(            (int) this.zoomPoint.getX(), (int) this.zoomPoint.getY()        );        if (hZoom && vZoom) {            // selected rectangle shouldn't extend outside the data area...            double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());            double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());            this.zoomRectangle = new Rectangle2D.Double(                this.zoomPoint.getX(), this.zoomPoint.getY(),                xmax - this.zoomPoint.getX(), ymax - this.zoomPoint.getY()            );        }        else if (hZoom) {            double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());            this.zoomRectangle = new Rectangle2D.Double(                this.zoomPoint.getX(), scaledDataArea.getMinY(),                xmax - this.zoomPoint.getX(), scaledDataArea.getHeight()            );        }        else if (vZoom) {            double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());            this.zoomRectangle = new Rectangle2D.Double(                scaledDataArea.getMinX(), this.zoomPoint.getY(),                scaledDataArea.getWidth(), ymax - this.zoomPoint.getY()

⌨️ 快捷键说明

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