📄 axis.java
字号:
* @param space space already reserved.
*
* @return the height required to draw the axis.
*/
public abstract AxisSpace reserveSpace(Graphics2D g2, Plot plot,
Rectangle2D plotArea, RectangleEdge edge,
AxisSpace space);
/**
* Draws the axis on a Java 2D graphics device (such as the screen or a printer).
*
* @param g2 the graphics device.
* @param cursor the cursor location (determines where to draw the axis).
* @param plotArea the area within which the axes and plot should be drawn.
* @param dataArea the area within which the data should be drawn.
* @param edge the axis location (TOP, BOTTOM, RIGHT or LEFT).
*
* @return The new cursor position.
*/
public abstract double draw(Graphics2D g2, double cursor,
Rectangle2D plotArea, Rectangle2D dataArea,
RectangleEdge edge);
/**
* 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 cursor the cursor location.
* @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.
*/
public abstract void refreshTicks(Graphics2D g2, double cursor,
Rectangle2D plotArea,
Rectangle2D dataArea,
RectangleEdge edge);
/**
* Returns the maximum width of the ticks in the working list (that is set
* up by refreshTicks()).
*
* @param g2 the graphics device.
* @param plotArea the area within which the plot is to be drawn.
*
* @return the maximum width of the ticks in the working list.
*/
protected double getMaxTickLabelWidth(Graphics2D g2, Rectangle2D plotArea) {
double maxWidth = 0.0;
Font font = getTickLabelFont();
FontRenderContext frc = g2.getFontRenderContext();
Iterator iterator = this.ticks.iterator();
while (iterator.hasNext()) {
Tick tick = (Tick) iterator.next();
Rectangle2D labelBounds = font.getStringBounds(tick.getText(), frc);
if (labelBounds.getWidth() > maxWidth) {
maxWidth = labelBounds.getWidth();
}
}
return maxWidth;
}
/**
* Returns true if the plot is compatible with the axis.
*
* @param plot the plot.
*
* @return <code>true</code> if the plot is compatible with the axis.
*/
protected abstract boolean isCompatiblePlot(Plot plot);
/**
* 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) {
Font font = getLabelFont();
Rectangle2D bounds = font.getStringBounds(axisLabel, g2.getFontRenderContext());
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 cursor the cursor value.
* @param plotArea the plot area.
* @param dataArea the area inside the axes.
* @param edge the location of the axis.
*
* @return The width or height used for the label.
*/
protected double drawLabel(String label,
Graphics2D g2, double cursor,
Rectangle2D plotArea, Rectangle2D dataArea,
RectangleEdge edge) {
if ((label == null) || (label.equals(""))) {
return 0.0;
}
Font font = getLabelFont();
Insets insets = getLabelInsets();
g2.setFont(font);
g2.setPaint(getLabelPaint());
FontRenderContext frc = g2.getFontRenderContext();
Rectangle2D labelBounds = font.getStringBounds(label, frc);
LineMetrics lm = font.getLineMetrics(label, frc);
double used = 0.0;
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 = cursor - insets.bottom - labelBounds.getHeight() / 2.0;
RefineryUtilities.drawRotatedString(label, g2,
(float) labelx, (float) labely,
TextAnchor.CENTER, TextAnchor.CENTER,
getLabelAngle());
used = 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 = cursor + insets.top + labelBounds.getHeight() / 2.0;
RefineryUtilities.drawRotatedString(label, g2,
(float) labelx, (float) labely,
TextAnchor.CENTER, TextAnchor.CENTER,
getLabelAngle());
used = 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 = cursor - insets.right - 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);
used = 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 = cursor + 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);
used = insets.left + labelBounds.getWidth() + insets.right;
}
return used;
}
/**
* 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.equalOrBothNull(this.label, axis.label);
boolean b2 = ObjectUtils.equalOrBothNull(this.labelFont, axis.labelFont);
boolean b3 = ObjectUtils.equalOrBothNull(this.labelPaint, axis.labelPaint);
boolean b4 = ObjectUtils.equalOrBothNull(this.labelInsets, axis.labelInsets);
boolean b5 = (this.tickLabelsVisible == axis.tickLabelsVisible);
boolean b6 = ObjectUtils.equalOrBothNull(this.tickLabelFont, axis.tickLabelFont);
boolean b7 = ObjectUtils.equalOrBothNull(this.tickLabelPaint, axis.tickLabelPaint);
boolean b8 = ObjectUtils.equalOrBothNull(this.tickLabelInsets, axis.tickLabelInsets);
boolean b9 = (this.tickMarksVisible == axis.tickMarksVisible);
boolean b10 = (this.tickMarkInsideLength == axis.tickMarkInsideLength);
boolean b11 = (this.tickMarkOutsideLength == axis.tickMarkOutsideLength);
boolean b12 = ObjectUtils.equalOrBothNull(this.tickMarkPaint, axis.tickMarkPaint);
boolean b13 = ObjectUtils.equalOrBothNull(this.tickMarkStroke, axis.tickMarkStroke);
boolean b14 = (this.fixedDimension == axis.fixedDimension);
return b0 && b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8
&& b9 && b10 && b11 && b12 && b13 && b14;
}
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.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.tickMarkStroke = SerialUtilities.readStroke(stream);
this.tickMarkPaint = SerialUtilities.readPaint(stream);
this.listenerList = new EventListenerList();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -