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

📄 axis.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param edge  the axis location (<code>null</code> not permitted).
     * @param plotState  collects information about the plot (<code>null</code> permitted).
     * 
     * @return the axis state (never <code>null</code>).
     */
    public abstract AxisState draw(Graphics2D g2, 
                                   double cursor,
                                   Rectangle2D plotArea, 
                                   Rectangle2D dataArea,
                                   RectangleEdge edge,
                                   PlotRenderingInfo plotState);

    /**
     * Calculates the positions of the ticks for the axis, storing the results
     * in the tick list (ready for drawing).
     *
     * @param g2  the graphics device.
     * @param state  the axis state.
     * @param plotArea  the area within which the axes and plot should be drawn.
     * @param dataArea  the area inside the axes.
     * @param edge  the edge on which the axis is located.
     * 
     * @return The list of ticks.
     */
    public abstract List refreshTicks(Graphics2D g2, 
                                      AxisState state,
                                      Rectangle2D plotArea,
                                      Rectangle2D dataArea,
                                      RectangleEdge edge);

    /**
     * Registers an object for notification of changes to the axis.
     *
     * @param listener  the object that is being registered.
     */
    public void addChangeListener(AxisChangeListener listener) {
        this.listenerList.add(AxisChangeListener.class, listener);
    }

    /**
     * Deregisters an object for notification of changes to the axis.
     *
     * @param listener  the object to deregister.
     */
    public void removeChangeListener(AxisChangeListener listener) {
        this.listenerList.remove(AxisChangeListener.class, listener);
    }

    /**
     * Notifies all registered listeners that the axis has changed.
     * The AxisChangeEvent provides information about the change.
     *
     * @param event  information about the change to the axis.
     */
    protected void notifyListeners(AxisChangeEvent event) {

        Object[] listeners = this.listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            if (listeners[i] == AxisChangeListener.class) {
                ((AxisChangeListener) listeners[i + 1]).axisChanged(event);
            }
        }

    }

    /**
     * Returns a rectangle that encloses the axis label.  This is typically used for layout
     * purposes (it gives the maximum dimensions of the label).
     *
     * @param g2  the graphics device.
     * @param edge  the edge of the plot area along which the axis is measuring.
     *
     * @return The enclosing rectangle.
     */
    protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {

        // calculate the width of the axis label...
        Rectangle2D result = new Rectangle2D.Double();
        String axisLabel = getLabel();
        if (axisLabel != null) {
            FontMetrics fm = g2.getFontMetrics(getLabelFont());
            Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
            Insets insets = getLabelInsets();
            bounds.setRect(bounds.getX(), bounds.getY(),
                           bounds.getWidth() + insets.left + insets.right,
                           bounds.getHeight() + insets.top + insets.bottom);
            double angle = getLabelAngle();
            if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
                angle = angle - Math.PI / 2.0;
            }
            double x = bounds.getCenterX();
            double y = bounds.getCenterY();
            AffineTransform transformer = AffineTransform.getRotateInstance(angle, x, y);
            Shape labelBounds = transformer.createTransformedShape(bounds);
            result = labelBounds.getBounds2D();
        }

        return result;

    }

    /**
     * Draws the axis label.
     *
     * @param label  the label text.
     * @param g2  the graphics device.
     * @param plotArea  the plot area.
     * @param dataArea  the area inside the axes.
     * @param edge  the location of the axis.
     * @param state  the axis state (<code>null</code> not permitted).
     *
     * @return Information about the axis.
     */
    protected AxisState drawLabel(String label,
                                  Graphics2D g2, 
                                  Rectangle2D plotArea, 
                                  Rectangle2D dataArea,
                                  RectangleEdge edge, 
                                  AxisState state) {

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Entering drawLabel() method, cursor = " + state.getCursor());
        }

        // it is unlikely that 'state' will be null, but check anyway...
        if (state == null) {
            throw new IllegalArgumentException("Axis.drawLabel: null state not permitted.");
        }
        
        if ((label == null) || (label.equals(""))) {
            return state;
        }

        Font font = getLabelFont();
        Insets insets = getLabelInsets();
        g2.setFont(font);
        g2.setPaint(getLabelPaint());
        FontMetrics fm = g2.getFontMetrics();
        Rectangle2D labelBounds = TextUtilities.getTextBounds(label, g2, fm);

        if (edge == RectangleEdge.TOP) {

            AffineTransform t = AffineTransform.getRotateInstance(
                getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY()
            );
            Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
            labelBounds = rotatedLabelBounds.getBounds2D();
            double labelx = dataArea.getCenterX();
            double labely = state.getCursor() - insets.bottom - labelBounds.getHeight() / 2.0;
            RefineryUtilities.drawRotatedString(
                label, g2, (float) labelx, (float) labely,
                TextAnchor.CENTER, TextAnchor.CENTER, getLabelAngle()
            );
            state.cursorUp(insets.top + labelBounds.getHeight() + insets.bottom);

        }
        else if (edge == RectangleEdge.BOTTOM) {

            AffineTransform t = AffineTransform.getRotateInstance(
                getLabelAngle(), labelBounds.getCenterX(), labelBounds.getCenterY()
            );
            Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
            labelBounds = rotatedLabelBounds.getBounds2D();
            double labelx = dataArea.getCenterX();
            double labely = state.getCursor() + insets.top + labelBounds.getHeight() / 2.0;
            RefineryUtilities.drawRotatedString(
                label, g2, (float) labelx, (float) labely,
                TextAnchor.CENTER, TextAnchor.CENTER, getLabelAngle()
            );
            state.cursorDown(insets.top + labelBounds.getHeight() + insets.bottom);

        }
        else if (edge == RectangleEdge.LEFT) {

            AffineTransform t = AffineTransform.getRotateInstance(
                getLabelAngle() - Math.PI / 2.0,
                labelBounds.getCenterX(), labelBounds.getCenterY()
            );
            Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
            labelBounds = rotatedLabelBounds.getBounds2D();
            double labelx = state.getCursor() - insets.right - labelBounds.getWidth() / 2.0;
            double labely = dataArea.getCenterY();
            RefineryUtilities.drawRotatedString(
                label, g2, (float) labelx, (float) labely, TextAnchor.CENTER, 
                TextAnchor.CENTER, getLabelAngle() - Math.PI / 2.0
            );
            state.cursorLeft(insets.left + labelBounds.getWidth() + insets.right);
        }
        else if (edge == RectangleEdge.RIGHT) {

            AffineTransform t = AffineTransform.getRotateInstance(
                getLabelAngle() + Math.PI / 2.0, labelBounds.getCenterX(), labelBounds.getCenterY()
            );
            Shape rotatedLabelBounds = t.createTransformedShape(labelBounds);
            labelBounds = rotatedLabelBounds.getBounds2D();
            double labelx = state.getCursor() + insets.left + labelBounds.getWidth() / 2.0;
            double labely = dataArea.getY() + dataArea.getHeight() / 2.0;
            RefineryUtilities.drawRotatedString(
                label, g2, (float) labelx, (float) labely,
                TextAnchor.CENTER, TextAnchor.CENTER,
                getLabelAngle() + Math.PI / 2.0
            );
            state.cursorRight(insets.left + labelBounds.getWidth() + insets.right);

        }

        return state;

    }

    /**
     * Draws an axis line at the current cursor position and edge.
     * 
     * @param g2  the graphics device.
     * @param cursor  the cursor position.
     * @param dataArea  the data area.
     * @param edge  the edge.
     */
    protected void drawAxisLine(Graphics2D g2, double cursor,
            Rectangle2D dataArea, RectangleEdge edge) {
        
        Line2D axisLine = null;
        if (edge == RectangleEdge.TOP) {
            axisLine = new Line2D.Double(dataArea.getX(), cursor, dataArea.getMaxX(), cursor);  
        }
        else if (edge == RectangleEdge.BOTTOM) {
            axisLine = new Line2D.Double(dataArea.getX(), cursor, dataArea.getMaxX(), cursor);  
        }
        else if (edge == RectangleEdge.LEFT) {
            axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, dataArea.getMaxY());  
        }
        else if (edge == RectangleEdge.RIGHT) {
            axisLine = new Line2D.Double(cursor, dataArea.getY(), cursor, dataArea.getMaxY());  
        }
        g2.setPaint(this.axisLinePaint);
        g2.setStroke(this.axisLineStroke);
        g2.draw(axisLine);
        
    }

    /**
     * Returns a clone of the axis.
     * 
     * @return A clone.
     * 
     * @throws CloneNotSupportedException if some component of the axis does not support cloning.
     */
    public Object clone() throws CloneNotSupportedException {
        
        Axis clone = (Axis) super.clone();
        
        // boolean visible;
        // String label;
        // Font labelFont
        // Paint labelPaint;
        if (this.labelInsets != null) {
            clone.labelInsets = (Insets) this.labelInsets.clone();
        }
        // double labelAngle;
        // boolean tickLabelsVisible;
        // Font tickLabelFont;
        // Paint tickLabelPaint;
        if (this.tickLabelInsets != null) {
            clone.tickLabelInsets = (Insets) this.tickLabelInsets.clone();
        }
        // boolean tickMarksVisible;
        // float tickMarkInsideLength;
        // float tickMarkOutsideLength;
        // Stroke tickMarkStroke;
        // Paint tickMarkPaint;
        // List ticks;
        // double fixedDimension;
        
        // It's up to the plot which clones up to restore the correct references
        clone.plot = null;        
        clone.listenerList = new EventListenerList();

        return clone;
            
    }
    
    /**
     * Tests this axis for equality with another object.
     *
     * @param obj  the object.
     *
     * @return <code>true</code> or <code>false</code>.
     */
    public boolean equals(Object obj) {

        if (obj == null) {
            return false;
        }

        if (obj == this) {
            return true;
        }

        if (obj instanceof Axis) {
            Axis axis = (Axis) obj;

            boolean b0 = (this.visible == axis.visible);

            boolean b1 = ObjectUtils.equal(this.label, axis.label);
            boolean b2 = ObjectUtils.equal(this.labelFont, axis.labelFont);
            boolean b3 = ObjectUtils.equal(this.labelPaint, axis.labelPaint);
            boolean b4 = ObjectUtils.equal(this.labelInsets, axis.labelInsets);
            boolean b5 = (Math.abs(this.labelAngle - axis.labelAngle) < 0.0000001);
    
            boolean b6 = (this.axisLineVisible == axis.axisLineVisible);
            boolean b7 = ObjectUtils.equal(this.axisLineStroke, axis.axisLineStroke);
            boolean b8 = ObjectUtils.equal(this.axisLinePaint, axis.axisLinePaint);
            
            boolean b9 = (this.tickLabelsVisible == axis.tickLabelsVisible);
            boolean b10 = ObjectUtils.equal(this.tickLabelFont, axis.tickLabelFont);
            boolean b11 = ObjectUtils.equal(this.tickLabelPaint, axis.tickLabelPaint);
            boolean b12 = ObjectUtils.equal(this.tickLabelInsets, axis.tickLabelInsets);

            boolean b13 = (this.tickMarksVisible == axis.tickMarksVisible);
            boolean b14 = (Math.abs(this.tickMarkInsideLength - axis.tickMarkInsideLength)
                           < 0.000001);
            boolean b15 = (Math.abs(this.tickMarkOutsideLength - axis.tickMarkOutsideLength) 
                           < 0.000001);

            boolean b16 = ObjectUtils.equal(this.tickMarkPaint, axis.tickMarkPaint);
            boolean b17 = ObjectUtils.equal(this.tickMarkStroke, axis.tickMarkStroke);

            boolean b18 = (Math.abs(this.fixedDimension - axis.fixedDimension) < 0.000001);

            return b0 && b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8
                   && b9 && b10 && b11 && b12 && b13 && b14 && b15 && b16 && b17 && b18;

        }

        return false;

    }

    /**
     * Provides serialization support.
     *
     * @param stream  the output stream.
     *
     * @throws IOException  if there is an I/O error.
     */
    private void writeObject(ObjectOutputStream stream) throws IOException {
        stream.defaultWriteObject();
        SerialUtilities.writePaint(this.labelPaint, stream);
        SerialUtilities.writePaint(this.tickLabelPaint, stream);
        SerialUtilities.writeStroke(this.axisLineStroke, stream);
        SerialUtilities.writePaint(this.axisLinePaint, stream);
        SerialUtilities.writeStroke(this.tickMarkStroke, stream);
        SerialUtilities.writePaint(this.tickMarkPaint, stream);
    }

    /**
     * Provides serialization support.
     *
     * @param stream  the input stream.
     *
     * @throws IOException  if there is an I/O error.
     * @throws ClassNotFoundException  if there is a classpath problem.
     */
    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        this.labelPaint = SerialUtilities.readPaint(stream);
        this.tickLabelPaint = SerialUtilities.readPaint(stream);
        this.axisLineStroke = SerialUtilities.readStroke(stream);
        this.axisLinePaint = SerialUtilities.readPaint(stream);
        this.tickMarkStroke = SerialUtilities.readStroke(stream);
        this.tickMarkPaint = SerialUtilities.readPaint(stream);
        this.listenerList = new EventListenerList();
    }

}

⌨️ 快捷键说明

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