001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: AbstractRenderer.java
021: Created on 21. Juni 2001, 12:32
022: */
023:
024: package de.progra.charting.render;
025:
026: import java.awt.image.BufferedImage;
027: import java.awt.Rectangle;
028: import java.awt.Image;
029: import java.awt.Graphics2D;
030: import java.awt.Dimension;
031: import java.awt.Color;
032:
033: /**
034: * The AbstractRenderer provides default implementations for the set and
035: * get methods of every Renderer. Especially it provides a default mechanism
036: * for scaling Renderer instances whose actual bounds are smaller than their
037: * preferred size. As a consequence, every Renderer instance only needs
038: * to implement paintDefault() which has to render the object from coordinates
039: * 0,0 onwards using the preferred size.
040: * @author mueller
041: * @version 1.0
042: */
043: public abstract class AbstractRenderer implements Renderer {
044:
045: Rectangle bounds = new Rectangle(0, 0, Integer.MAX_VALUE,
046: Integer.MAX_VALUE);
047:
048: /** Creates new AbstractRenderer */
049: public AbstractRenderer() {
050: }
051:
052: /** Sets the bounds the layout manager has assigned to
053: * this renderer. Those, of course, have to be
054: * considered in the rendering process.
055: * @param bounds the new bounds for the renderer.
056: */
057: public void setBounds(Rectangle bounds) {
058: this .bounds = bounds;
059: }
060:
061: /** Gets the bounds for this renderer.
062: * @return the bounds of this renderer. If <code>setBounds</code> has not
063: * been called before, the bounds computed from
064: * <code>getPreferredSize</code> is returned.
065: */
066: public Rectangle getBounds() {
067: return bounds;
068: }
069:
070: /** Renders the Object in the Graphics object. Creates a BufferedImage
071: * and the corresponding Graphics2D object to paint in. The Image is
072: * created using the preferred size. Afterwards <code>paintDefault</code>
073: * is called to perform a standard painting in the Graphics object.
074: * If the bounds and the preferred size don't match the image is
075: * scaled afterwards.
076: * @param g the Graphics2D object in which to render
077: */
078: public void render(Graphics2D g) {
079: Dimension d = getPreferredSize();
080: BufferedImage im = new BufferedImage((int) d.width,
081: (int) d.height, BufferedImage.TYPE_INT_RGB);
082: Graphics2D g2 = im.createGraphics();
083: g2.setColor(Color.white);
084: g2.fillRect(0, 0, d.width, d.height);
085: g2.setColor(Color.black);
086:
087: paintDefault(g2);
088:
089: if (d.width > getBounds().getWidth()
090: || d.height > getBounds().getHeight()) {
091: // Scale Image
092: Image scale = im.getScaledInstance((int) getBounds()
093: .getWidth(), (int) getBounds().getHeight(),
094: Image.SCALE_SMOOTH);
095:
096: g.drawImage(scale, (int) getBounds().getX(),
097: (int) getBounds().getY(), null);
098: } else
099: g.drawImage(im, (int) getBounds().getX(), (int) getBounds()
100: .getY(), null);
101: }
102:
103: /** This method is called by the paint method to do the actual painting.
104: * The painting is supposed to start at point (0,0) and the size is
105: * always the same as the preferred size. The paint method performs
106: * the possible scaling.
107: * @param g the Graphics2D object to paint in.
108: */
109: public abstract void paintDefault(Graphics2D g);
110: }
|