legendtitle.java

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

JAVA
549
字号
    }        /**     * Sets the padding used for the legend item graphics.     *      * @param padding  the padding (<code>null</code> not permitted).     */    public void setLegendItemGraphicPadding(RectangleInsets padding) {        if (padding == null) {            throw new IllegalArgumentException("Null 'padding' argument.");           }        this.legendItemGraphicPadding = padding;        notifyListeners(new TitleChangeEvent(this));    }        /**     * Returns the item font.     *      * @return The font (never <code>null</code>).     */    public Font getItemFont() {        return this.itemFont;       }        /**     * Sets the item font.     *      * @param font  the font (<code>null</code> not permitted).     */    public void setItemFont(Font font) {        if (font == null) {            throw new IllegalArgumentException("Null 'font' argument.");           }        this.itemFont = font;        notifyListeners(new TitleChangeEvent(this));    }        /**     * Returns the padding used for the items labels.     *      * @return The padding.     */    public RectangleInsets getItemLabelPadding() {        return this.itemLabelPadding;       }        /**     * Sets the padding used for the item labels in the legend.     *      * @param padding  the padding (<code>null</code> not permitted).     */    public void setItemLabelPadding(RectangleInsets padding) {        if (padding == null) {            throw new IllegalArgumentException("Null 'padding' argument.");           }        this.itemLabelPadding = padding;        notifyListeners(new TitleChangeEvent(this));    }        /**     * Fetches the latest legend items.     */    protected void fetchLegendItems() {        this.items.clear();        RectangleEdge p = getPosition();        if (RectangleEdge.isTopOrBottom(p)) {            this.items.setArrangement(this.hLayout);           }        else {            this.items.setArrangement(this.vLayout);           }        for (int s = 0; s < this.sources.length; s++) {            LegendItemCollection legendItems = this.sources[s].getLegendItems();            if (legendItems != null) {                for (int i = 0; i < legendItems.getItemCount(); i++) {                    LegendItem item = legendItems.get(i);                    Block block = createLegendItemBlock(item);                    this.items.add(block);                }            }        }    }        /**     * Creates a legend item block.     *      * @param item  the legend item.     *      * @return The block.     */    protected Block createLegendItemBlock(LegendItem item) {        BlockContainer result = null;        LegendGraphic lg = new LegendGraphic(            item.getShape(), item.getFillPaint()        );        lg.setShapeFilled(item.isShapeFilled());        lg.setLine(item.getLine());        lg.setLineStroke(item.getLineStroke());        lg.setLinePaint(item.getLinePaint());        lg.setLineVisible(item.isLineVisible());        lg.setShapeVisible(item.isShapeVisible());        lg.setShapeOutlineVisible(item.isShapeOutlineVisible());        lg.setOutlinePaint(item.getOutlinePaint());        lg.setOutlineStroke(item.getOutlineStroke());        lg.setPadding(this.legendItemGraphicPadding);        BlockContainer legendItem = new BlockContainer(new BorderArrangement());        lg.setShapeAnchor(getLegendItemGraphicAnchor());        lg.setShapeLocation(getLegendItemGraphicLocation());        legendItem.add(lg, this.legendItemGraphicEdge);        LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont);        labelBlock.setPadding(this.itemLabelPadding);        labelBlock.setToolTipText(item.getToolTipText());        legendItem.add(labelBlock);                result = new BlockContainer(new CenterArrangement());        result.add(legendItem);                return result;    }        /**     * Returns the container that holds the legend items.     *      * @return The container for the legend items.     */    public BlockContainer getItemContainer() {        return this.items;    }    /**     * Arranges the contents of the block, within the given constraints, and      * returns the block size.     *      * @param g2  the graphics device.     * @param constraint  the constraint (<code>null</code> not permitted).     *      * @return The block size (in Java2D units, never <code>null</code>).     */    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {        Size2D result = new Size2D();        fetchLegendItems();        if (this.items.isEmpty()) {            return result;           }        BlockContainer container = this.wrapper;        if (container == null) {            container = this.items;        }        RectangleConstraint c = toContentConstraint(constraint);        Size2D size = container.arrange(g2, c);        result.height = calculateTotalHeight(size.height);        result.width = calculateTotalWidth(size.width);        return result;    }    /**     * Draws the title on a Java 2D graphics device (such as the screen or a     * printer).     *     * @param g2  the graphics device.     * @param area  the available area for the title.     */    public void draw(Graphics2D g2, Rectangle2D area) {        draw(g2, area, null);    }    /**     * Draws the block within the specified area.     *      * @param g2  the graphics device.     * @param area  the area.     * @param params  ignored (<code>null</code> permitted).     *      * @return An {@link org.jfree.chart.block.EntityBlockResult} or      *         <code>null</code>.     */    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {        Rectangle2D target = (Rectangle2D) area.clone();        target = trimMargin(target);        if (this.backgroundPaint != null) {            g2.setPaint(this.backgroundPaint);            g2.fill(target);        }        getBorder().draw(g2, target);        getBorder().getInsets().trim(target);        BlockContainer container = this.wrapper;        if (container == null) {            container = this.items;         }        target = trimPadding(target);        return container.draw(g2, target, params);       }    /**     * Sets the wrapper container for the legend.     *      * @param wrapper  the wrapper container.     */    public void setWrapper(BlockContainer wrapper) {        this.wrapper = wrapper;    }        /**     * Tests this title for equality with an arbitrary object.     *      * @param obj  the object (<code>null</code> permitted).     *      * @return A boolean.     */    public boolean equals(Object obj) {        if (obj == this) {            return true;           }        if (!(obj instanceof LegendTitle)) {            return false;           }        if (!super.equals(obj)) {            return false;           }        LegendTitle that = (LegendTitle) obj;        if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {            return false;           }        if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {            return false;           }        if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {            return false;           }        if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {            return false;           }        if (!this.itemFont.equals(that.itemFont)) {            return false;           }        if (!this.hLayout.equals(that.hLayout)) {            return false;           }        if (!this.vLayout.equals(that.vLayout)) {            return false;           }        return true;    }        /**     * 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.backgroundPaint, 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.backgroundPaint = SerialUtilities.readPaint(stream);        this.sources = new LegendItemSource[0];    }}

⌨️ 快捷键说明

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